17
$\begingroup$

GitHub doesn't auto-detect and color Mathematica code in its markdown. Can we make it do so?

This may be trivial, but many README files don't seem to have it.

$\endgroup$
2
  • 1
    $\begingroup$ My README files actually have highlighting ;). $\endgroup$
    – jkuczm
    Aug 16, 2017 at 19:51
  • $\begingroup$ @jkuczm Good to know. I'll add that as a note. (I must have missed the purple on the Get) $\endgroup$
    – b3m2a1
    Aug 16, 2017 at 19:53

1 Answer 1

17
$\begingroup$

It seems to have been possible at least since March 16th 2016 and probably earlier (thanks to jkuczm) I found it here (cannot remember how I ended up there). Here's another example using it.

If we look at how that works, though, we see this:

```Mathematica
Manipulate[
Module[{
r1=ClassVRadius[t1],
r2=ClassVRadius[t2],
r3=ClassVRadius[t3],
r4=ClassVRadius[t4],
r5=ClassVRadius[t5],
r6=ClassVRadius[t6],
r7=ClassVRadius[t7]},
Graphics3D[{
{ColorData["B...
etc.
```

And so just sticking your code in a block like that headed by Mathematica does it. I suppose this should have been obvious. But maybe it's good to have it here for those of use who only use GitHub sparingly.

I used this trick here. Admittedly, the highlighting is poorly implemented and the colors are garish, but it's fun, at the very least. And maybe GitHub will improve it some day.

One caveat: As Szabolcs notes, this is not necessarily assured to work on other Markdown-using sites.

$\endgroup$
10
  • $\begingroup$ It may be good to note that only fenced code blocks support this, and fenced code blocks are not standard Markdown (hence may not render properly if the project is moved to a different hosting than GitHub). [Also, I do not think this QA is off topic at all.] $\endgroup$
    – Szabolcs
    Aug 16, 2017 at 7:54
  • 1
    $\begingroup$ @Szabolcs They are standard: spec.commonmark.org/0.28/#fenced-code-blocks $\endgroup$
    – Riking
    Aug 16, 2017 at 17:48
  • $\begingroup$ @Riking There seem to be disagreements and no proper standard ... Once I was told that some tool didn't support fenced code blocks because "it's not standard" and I believed it. Python markdown also doesn't support it, and the extension to add support did not play well with the syntax colouring extension I once tried to use. So as you say, my statement about the standard was incorrect. But using this still have the risk I mentioned. All implementations support indented code blocks, but several don't support fenced ones (e.g. the one used here on SE). $\endgroup$
    – Szabolcs
    Aug 16, 2017 at 19:49
  • $\begingroup$ @b3m2a1 Sorry I accidentally linked to highlight.js, while it seems that GitHub uses Linguist $\endgroup$
    – jkuczm
    Aug 16, 2017 at 20:05
  • 1
    $\begingroup$ @JohnJosephM.Carrasco interesting. Alternatively you can also use halirutan's prettify.js plugin. That's what I do and I think what Szabolcs does. $\endgroup$
    – b3m2a1
    Aug 17, 2017 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.