Many answerers create stunning visualizations to augment their answers. Numerical analysis is often leveraged to come to results. Both are using programming knowledge as a piece of the mathematician's toolkit to explore a question. Yet, Mathematics Stack Exchange doesn't seem to encourage posting the actual code. I'm asking whether we ought to revisit that idea.

Yes, yes, yes - I understand that coding is not the focus of this site. I am well aware of (read: reliant on) the other Exchanges out there for programming purposes. I'm not proposing Mathematics Stack Exchange be used as a Q&A for coding practices.

Can I frame when I'm thinking? Okay, consider this. When I compare my potato-quality visuals on this question to robjohn's beautiful answer, I find myself thinking "how in the world did they script that?" I could struggle with the task for a while. It could be fun. If I needed assistance, I could ask the answerer in the comments or even create a secondary question, probably on another exchange. But that seems to fragment the question and demand an unnecessary expenditure of man-hours to reproduce the result.

Stepping away from visualizations, I'd wager that posts with numerical analysis do tend to post code more frequently, but even then, it's not a requirement common practice or guided by any encouragement of which I'm aware.

I feel this is unfortunate, as insight into the programs could augment and expedite user understanding, even if scripted in varying languages and even if, years from now, the answer may use deprecated code. Providing a way to augment answers with code would only further the potential of an answer.

With the focus being mathematics and readability, code introduction could be viewed as supplementary. It'd be nice to have collapsible paste-bins for code blocks, for instance.

The idea, again, isn't to push code onto people, but to reduce the opacity of and increase the accessibility of answers. It'd only make our users stronger.

What are your thoughts?

  • $\begingroup$ @hardmath Thanks. Yes, the word requirement was too strict. Encouraged, as in the title, would be what I believe is best. A solution without code could be a fine solution, yet a body of work without code is opaque to reproduction. Do you believe offering that resource would detract from a solution? $\endgroup$
    – zahbaz
    Commented May 8, 2020 at 17:07
  • $\begingroup$ An ability to reproduce results is important, and sometimes this hinges on using code. There have been answers I posted that benefited by providing the short programs used to solve problems. If my programming were so elaborate that code folding was important, I'd be more inclined to use a github repository. The difficult case for me is when numerical methods are done with a quick-and-dirty spreadsheet. Although these can be "shared" on Google Docs, etc., I have an aversion to exposing such unpolished efforts. $\endgroup$
    – hardmath
    Commented May 8, 2020 at 17:29
  • $\begingroup$ "how in the world did they script that?" maybe a large image with large downscale then, or svg. But the versions deprecating is a big thing, yes. I use tio.run (the codegolf SE thing) if I want to post some reproducible program results in comments, it uses both markdown, source compression and ascii-base encoding (not sure base64 or base85), but don't think it's the best option) $\endgroup$ Commented May 17, 2020 at 0:19
  • $\begingroup$ Producing an image or simply a result may require 1/ commercial software (Maple for instance) 2/ modifications by hand that are not easy to render by code (possibly with commercial software, such as Illustrator) 3/ a specific OS or machine (enough RAM) 4/ Access to a specific (and possibly commercial) web service 5/ time (not often the problem here though) 6/ Knowledge of the language used. Reproducibility is never perfect. $\endgroup$ Commented May 18, 2020 at 9:47
  • $\begingroup$ And I also fear that if we can give code for the answer, someone will feel the need to post another answer with code in another language, even though it really should not be the main concern here. Possibly with good intentions: giving an equivalent with free software, for instance. $\endgroup$ Commented May 18, 2020 at 9:47
  • 1
    $\begingroup$ @Jean-ClaudeArbaut 1) Reproducibility is never perfect - so discourage it? How does that help? 2) If the code was supplementary as it should be (i.e. hidden in link or collapsible), do you feel that could alleviate the temptation for a copycat answer? $\endgroup$
    – zahbaz
    Commented May 19, 2020 at 6:55
  • $\begingroup$ @zahbaz I just wanted to emphasize that the proposition does not have only advantages, IMHO. You say I "discourage" reproducibility: I don't think so, I try to point out its limits. I still think there are pros and cons. However, with the principle that a result without proof has no value here, and that code can be considered a proof, then yes, we should have the code with the plots. As long as plots (or anything produced with the help of a program) are part of a mathematical answer, they should be verifiable too. $\endgroup$ Commented May 19, 2020 at 7:24
  • $\begingroup$ @Jean-ClaudeArbaut Thanks. You bring up good points, Jean-Claude. $\endgroup$
    – zahbaz
    Commented May 19, 2020 at 18:20

2 Answers 2


This is a great idea.

Some implementations are written in just a few lines of code in Python/MATLAB/Mathematica. One can share the code using a pair of ``` around the code without much distraction:

# insert my code
# here

If it is done in desmos, a snapshot may be more appropriate.

For scripts that are long, I agree that "It'd be nice to have collapsible paste-bins for code blocks." There was actually a (declined) feature request on meta.stackexchange.com long time ago: Please add the ability to fold blocks of code in questions and answers

One can do this on Stack Overflow: https://meta.stackoverflow.com/questions/302078/collapsible-code-markup But it does not seem to work for maths.SE yet.

For the moment, @joriki chooses to share his code on GitHub:

I like combining math and computers. I've written lots of Java code for answers on this site. I used to post individual classes as gists, but now I've started a GitHub repository for all my math code. Feel free to use it!

This is also a terrific option!

  • $\begingroup$ I post lots of figures without code, but if and when someone requests code, then I do. Seems pretty straightforward. $\endgroup$ Commented May 13, 2020 at 5:51
  • 1
    $\begingroup$ GitHub repository is not such a good option. Although GitHub is now run by Microsoft and thus not too likely to disappear, the repositories themselves can be subject to cleanup by their owners (for whatever reasons) and thus become unavailable in the long term. $\endgroup$
    – Ruslan
    Commented May 14, 2020 at 12:27
  • $\begingroup$ @DavidG.Stork Perhaps. That's kind of you to provide that level of commitment! I would expect most answerers may not keep this information filed away for any lengthy duration or may not be bothered to provide that information a year down the line. Allowing unobtrusive incorporation of code into an answer gives an opportunity to document the information immediately. It also prevents the need for anyone asking in the first place: a deficit of people asking doesn't imply a deficit of people wanting to know :) $\endgroup$
    – zahbaz
    Commented May 19, 2020 at 7:04

For the purposes of reproduction, with the aim of avoiding cluttering the mathematical post with program code, and until the collapsible code markup finally arrives at Math.SE, you always have the option of hiding the code in a Markdown comment.

After adding this comment you can also include the link to the Markdown source of your post for easy access. This can be done in several steps:

  1. Post the answer without the link
  2. Get your post id (the first number after /a/ in the share link)
  3. Put this id into https://math.stackexchange.com/posts/YOUR_POST_ID/revisions instead of YOUR_POST_ID (appropriately change the domain name if it's not Math.SE)
  4. In the page to which the resulting link points, get the address of the source, and this is your final link to post.

The result will look like the following link to this post's Markdown source: source is here at the end. You can even avoid having the post look edited if you manage to do this in the edit grace period.

There are limitations:

  • The code must fit into the character limit though, in addition to the answer body.
  • Markdown treats > as a special character, ending the comment on it and thus leaking the rest of the code into the actual rendered HTML page.

The latter limitation can be circumvented by using base64 as I do in this post, and posting the command to decode the resulting string back into human-readable form. Not sure if this command will work out of the box for Windows users (it can be done using Cygwin or maybe even WSL though), but for Unices this should be trivial. Note that the decoding command should avoid the > character (see how I hacked around this using cp).

  • $\begingroup$ Clever. I'm unsure if hiding results in the markdown is the way to go, but I like this hack. $\endgroup$
    – zahbaz
    Commented May 19, 2020 at 6:41
  • 1
    $\begingroup$ The one time I had a big piece of code worth posting, I did something similar to this, instead writing the code in a post normally, waiting for the 5 minute edit grace period to expire, then removing the code and putting a link to the revision that contained it. $\endgroup$ Commented May 20, 2020 at 16:27
  • $\begingroup$ @MiloBrandt actually, your approach is much better indeed: in a full-fledged revision you can get rid of the tricks with the limitations of Markdown and make the source easy to read directly with code blocks. $\endgroup$
    – Ruslan
    Commented May 20, 2020 at 16:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .