249

After investigating, I've found mathjax can do this. But when I write some example in my markdown file, it doesn't show the correct equations:

I have added this in the head of markdown file:

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=default"></script>

And type the mathjax statement:

\(E=mc^2\),$$x_{1,2} = \frac{-b \pm \sqrt{b^2-4ac}}{2b}.$$

But github shows nothing for the math symbols! Please help me, thanks! Tell me how to show math symbols in github markdown pages.

phuclv
  • 37,963
  • 15
  • 156
  • 475
alexunder
  • 2,793
  • 3
  • 15
  • 18

18 Answers18

199

But github show nothing for the math symbols! please help me, thanks!

GitHub markdown parsing is performed by the SunDown (ex libUpSkirt) library.

The motto of the library is "Standards compliant, fast, secure markdown processing library in C". The important word being "secure" there, considering your question :).

Indeed, allowing javascript to be executed would be a bit off of the MarkDown standard text-to-HTML contract.

Moreover, everything that looks like a HTML tag is either escaped or stripped out.

Tell me how to show math symbols in general github markdown.

Your best bet would be to find a website similar to yuml.me which can generate on-the-fly images from by parsing the provided URL querystring.

Update

I've found some sites providing users with such service: codedogs.com (no longer seems to support embedding) or iTex2Img. You may want to try them out. Of course, others may exist and some Google-fu will help you find them.

given the following markdown syntax

![equation](http://www.sciweavers.org/tex2img.php?eq=1%2Bsin%28mc%5E2%29&bc=White&fc=Black&im=jpg&fs=12&ff=arev&edit=)

it will display the following image

equation

Note: In order for the image to be properly displayed, you'll have to ensure the querystring part of the url is percent encoded. You can easily find online tools to help you with that task, such as www.url-encode-decode.com

nulltoken
  • 64,429
  • 20
  • 138
  • 130
  • 6
    @nultoken, thanks for your helpful answer. I used the iTex2Img website you referenced above and I added two formulae to my documentation. I've got an issue that when I open the documentation page on GitHub, both formulae are shown the same while they are actually different. Would you happen to know why it happens? – Sam Feb 17 '14 at 04:13
  • 3
    http://mathurl.com/ is also a noteworthy website if you have to generate the image. – Martin Thoma Apr 14 '15 at 05:41
  • 2
    This doesn't seem to work on github though. The image does not render. Anyone know why? – OganM Sep 23 '15 at 01:16
  • 1
    @OganM The syntax has changed. The link has been fixed and tested in GitHub as well – nulltoken Sep 24 '15 at 21:56
  • 1
    The iTex2Img site doesn't seem to consistently work for me. The image doesn't seem to render on Github. – timbram Apr 07 '16 at 18:31
  • @timbram it seems iTex2Img caches ever unique url, and returns the cache. So the first time it works, and returns as the correct Content-Type image/png, but the cached version has a Content-Type of "text/html; charset=utf-8". While the bytes are correct, Github realizes this does not appear to be an image, and rejects it. Any idea how to fix that? – Andy Oct 31 '16 at 21:10
  • Image-based approach breaks when used in non-white color scheme (e.g. with Dark Reader). – Ruslan Jul 19 '21 at 09:12
189

Markdown supports inline HTML. Inline HTML can be used for both quick and simple inline equations and, with and external tool, more complex rendering.

Quick and Simple Inline

For quick and simple inline items use HTML ampersand entity codes. An example that combines this idea with subscript text in markdown is: hθ(x) = θo x + θ1x, the code for which follows.

    h<sub>&theta;</sub>(x) = &theta;<sub>o</sub> x + &theta;<sub>1</sub>x

HTML ampersand entity codes for common math symbols can be found here. Codes for Greek letters here. An extensive list html entity codes to Unicode characters can be found here.

While this approach has limitations it works in practically all markdown and does not require any external libraries.

Complex Scalable Inline Rendering with LaTeX and Codecogs

If your needs are greater use an external LaTeX renderer like CodeCogs. Create an equation with CodeCogs editor. Choose svg for rendering and HTML for the embed code. Svg renders well on resize. HTML allows LaTeX to be easily read when you are looking at the source. Copy the embed code from the bottom of the page and paste it into your markdown.

<img src="https://latex.codecogs.com/svg.latex?\Large&space;x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}" title="\Large x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}" />

Expressed in markdown becomes

![\Large x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}](https://latex.codecogs.com/svg.latex?\Large&space;x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}) 

\Large x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}

This combines this answer and this answer.

GitHub support only somtimes worked using the above raw html syntax for readable LaTeX for me. If the above does not work for you another option is to instead choose URL Encoded rendering and use that output to manually create a link like:

\Large x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}

![\Large x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}](https://latex.codecogs.com/svg.latex?x%3D%5Cfrac%7B-b%5Cpm%5Csqrt%7Bb%5E2-4ac%7D%7D%7B2a%7D)

This manually incorporates LaTex in the alt image text and uses an encoded URL for rendering on GitHub.

Multi-line Rendering

If you need multi-line rendering check out this answer.

SpeedCoder5
  • 8,188
  • 6
  • 33
  • 34
  • 1
    this worked great. I had to convert a jupyter notebook (.ipynb) to `.md` and the equations were essentially multiline latex code. – Marc Maxmeister Jun 17 '19 at 18:21
  • Yes it works in jupyter notebook too, and, while not directly related, [handcalcs](https://github.com/connorferster/handcalcs) is a library to render Python calculation code automatically in Latex `pip install handcalcs` – SpeedCoder5 Sep 02 '20 at 15:39
  • 1
    This should be the accepted answer. For me, `R-1` correctly rendered R^-1. – tash Nov 11 '20 at 19:59
  • 3
    The SVG (and any other picture-based) approach breaks when used in non-white color scheme (e.g. with Dark Reader). – Ruslan Jul 19 '21 at 09:11
  • @Ruslan yes the codecogs picture-based approach does only render dark text. A manual process to try could be, after the render, download the image, edit it manually to invert the colors, then upload the inverted image to github and embed it in the markdown as an image. There is an open source VSCode extension project that helps convert a standard LaTeX math equation to a remote or local SVG: https://github.com/TeamMeow/vscode-math-to-image. Perhaps someone may one day add a Dark Reader color scheme to it. – SpeedCoder5 Jul 27 '21 at 03:30
  • 1
    @Ruslan Indeed, this is true, despite the ability to define background colors. I have set my image to have white background, but the SVG rendering still shows it as transparent (difficult to read in a dark mode GitHub). – Fuhrmanator Sep 03 '21 at 19:32
  • 1
    @Ruslan, good catch. This can be solved by inverting the image using the CSS: `filter: invert(100%)`; – Viktor Soroka Dec 31 '21 at 19:12
34

It ’s 2020 now, let me summarize the progress of the mathematical formula rendering support of source code repository hosts.

GitHub & Bitbucket

GitHub and Bitbucket still do not support the rendering of mathematical formulas, whether it is the default delimiters or other.

Bitbucket Cloud / BCLOUD-11192 -- Add LaTeX Support in MarkDown Documents (BB-12552)

GitHub / markup -- Rendering math equations

GitHub / markup -- Support latex

GitHub Community Forum -- [FEATURE REQUEST] LaTeX Math in Markdown

talk.commonmark.org -- Can math formula added to the markdown

GitHub has hardly made any substantial progress in recent years.

GitLab

GitLab is already supported, but not the most common way. It uses its own delimiter.

This math is inline $`a^2+b^2=c^2`$.

This is on a separate line

```math
a^2+b^2=c^2
```

GitLab Flavored Markdown -- Math

Who supports the universal delimiters?

A Markdown parser used by Hugo

Other ways to render

Community
  • 1
  • 1
Xuesong Ye
  • 505
  • 5
  • 13
27

It is officially supported since May 2022 (and inline expression since May 2023, see at the end):

Render mathematical expressions in Markdown

You can now use LaTeX style syntax to render math expressions within Markdown inline (using $ delimiters) or in blocks (using $$ delimiters).

Writing expressions as blocks

To add math as a multiline block displayed separately from surrounding text, start a new line and delimit the expression with two dollar symbols $$.

**The Cauchy-Schwarz Inequality**
$$\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)$$

Displayed math markdown rendering

Writing inline expressions

To include a math expression inline with your text, delimit the expression with a dollar symbol $.

This sentence uses `$` delimiters to show math inline: $\sqrt{3x-1}+(1+x)^2$

Inline math markdown rendering

GitHub's math rendering capability uses MathJax; an open source, JavaScript-based display engine.
MathJax supports a wide range of LaTeX macros and a number of useful accessibility extensions.
For more information, see the MathJax documentation and the MathJax Accessibility Extensions documentation.

Some users have previously used a workaround to generate images of mathematical expressions through API requests.
Images generated this way will remain viewable, but this technique will no longer work.
Going forward, expressions should be written directly in Markdown using LaTeX syntax as described above.

For more information about authoring content with advanced formatting, see Working with advanced formatting in the GitHub documentation.


This is still beta, and criticised.
See "Math on GitHub: The Good, the Bad and the Ugly" from Nico Schlömer.
The syntax introduces:

  • Competing Markdown and math renderer
  • A math block hard to interpret

As noted by brc-dd in the comments:

June 2022:

Fenced block syntax for mathematical expressions

Users can now delineate mathematical expressions using ```math fenced code block syntax in addition to the already supported delimiters.
Two dollar sign $$ delimiters are not required if this method is used.

**Here is some math!**

```math
\sqrt{3}
.```

becomes:

math-expression-as-a-fenced-code-block -- https://i0.wp.com/user-images.githubusercontent.com/7219923/175109268-ee329b99-c2dc-4589-a5be-0a4eee31d916.png?ssl=1

Read more about working with advanced formatting.


New delimiter syntax for inline mathematical expressions (May. 2023)

We are introducing a new method for incorporating LaTeX-based mathematical expressions inline within Markdown.
In addition to the existing delimiters, we now support delimiting LaTeX-style math syntax with dollar signs and backticks (for example, $\sqrt{3}$).
This new syntax is especially useful if the mathematical expressions you're writing contain characters that overlap with Markdown syntax.

To learn more about using mathematical expressions within Markdown on GitHub, check out "Writing mathematical expressions" in the GitHub Docs.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Another update on this: https://github.blog/changelog/2022-06-28-fenced-block-syntax-for-mathematical-expressions/ – brc-dd Jun 28 '22 at 17:17
  • 2
    @brc-dd Thank you. I have included your comment and the relevant link content in the answer. – VonC Jun 28 '22 at 17:56
  • Yay, the great day of technology advancement has come! Although I find it odds that SpaceX have launched several reusable rocket flights (https://en.wikipedia.org/wiki/SpaceX#Summary_of_achievements) before developers can finally write mathematical equations directly on GitHub. – Quan Bui Oct 18 '22 at 03:33
22

Another possibility is to rely on GitHub's own notebook renderer. This even works right here in SO.

To render x_{1,2} = \frac{-b \pm \sqrt{b^2-4ac}}{2b} use the following HTML img tag:

<img src="https://render.githubusercontent.com/render/math?math=x_{1,2} = \frac{-b \pm \sqrt{b^2-4ac}}{2b}">

Live Demo:

What's great about this approach is that you can edit your formula directly in Markdown and the preview will update accordingly.
You can try it out by editing this answer. (Just throw away your edits if they don't add to the answer ;))

Source: https://gist.github.com/a-rodin/fef3f543412d6e1ec5b6cf55bf197d7b

Thomas Kainrad
  • 2,542
  • 21
  • 26
  • 9
    Anyone using this method, because `+` is an escape character in URI, use `%2B` to render `+`. – Jishan Aug 09 '21 at 19:34
16

If just wanted to show math in the browser for yourself, you could try the Chrome extension GitHub with MathJax. It's quite convenient.

nn0p
  • 1,189
  • 12
  • 28
  • 1
    there's an error with icon16.png when I try to install – Homero Esmeraldo Apr 12 '19 at 02:43
  • @HomeroEsmeraldo You can manually install MathJax extension. 1. Clone the repository: https://github.com/orsharir/github-mathjax.git 2. Enable Developer mode in chrome extensions. 3. Drag 'github-mathjax.crx' file to chrome extensions. – rizz Oct 20 '20 at 13:52
16

One other work-around is to use jupyter notebooks and use the markdown mode in cells to render equations.

Basic stuff seems to work perfectly, like centered equations

\begin{equation}
...
\end{equation}

or inline equations

$ \sum_{\forall i}{x_i^{2}} $

Although, one of the functions that I really wanted did not render at all in github was \mbox{}, which was a bummer. But, all in all this has been the most successful way of rendering equations on github.

chahuja
  • 307
  • 2
  • 3
  • 3
    Thank you for this answer, but there is this issue: I managed to use jupyter notebook in markdown mode and the equation appears OK. However, now how can you do to transfer this jupyter markdown to the `readme.md` on GitHub? – DavidC. May 29 '17 at 13:22
  • 2
    Equations do not render directly on Markdown files. The equations would pretty much have to be images to be on the `readme.md`. However, you could always just put `file.ipynb` on github and github renders it well. – hexicle Dec 22 '17 at 15:38
6

While GitHub won't interpret the MathJax formulas, you can automatically generate a new Markdown document with the formulae replaced by images.

I suggest you look at the GitHub app TeXify:

GitHub App that looks in your pushes for files with extension *.tex.md and renders it's TeX expressions as SVG images

How it works (from the source repository):

Whenever you push TeXify will run and seach for *.tex.md files in your last commit. For each one of those it'll run readme2tex which will take LaTeX expressions enclosed between dollar signs, convert it to plain SVG images, and then save the output into a .md extension file (That means that a file named README.tex.md will be processed and the output will be saved as README.md). After that, the output file and the new SVG images are then commited and pushed back to your repo.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
4

I use the below mentioned process to convert equations to markdown. This works very well for me. Its very simple!!

Let's say, I want to represent matrix multiplication equation

Step 1:

Get the script for your formulae from here - https://csrgxtu.github.io/2015/03/20/Writing-Mathematic-Fomulars-in-Markdown/

My example: I wanted to represent Z(i,j)=X(i,k) * Y(k, j); k=1 to n into a summation formulae.

enter image description here

Referencing the website, the script needed was => Z_i_j=\sum_{k=1}^{10} X_i_k * Y_k_j

Step 2:

Use URL encoder - https://www.urlencoder.org/ to convert the script to a valid url

My example:

enter image description here

Step 3:

Use this website to generate the image by copy-pasting the output from Step 2 in the "eq" request parameter - http://www.sciweavers.org/tex2img.php?eq=<b><i>paste-output-here</i></b>&bc=White&fc=Black&im=jpg&fs=12&ff=arev&edit=

- My example:
http://www.sciweavers.org/tex2img.php?eq=Z_i_j=\sum_{k=1}^{10}%20X_i_k%20*%20Y_k_j&bc=White&fc=Black&im=jpg&fs=12&ff=arev&edit=

enter image description here

Step 4:

Reference image using markdown syntax - ![alt text](enter url here)

- Copy this in your markdown and you are good to go:

![Z(i,j)=X(i,k) * Y(k, j); k=1 to n](http://www.sciweavers.org/tex2img.php?eq=Z_i_j%3D%5Csum_%7Bi%3D1%7D%5E%7B10%7D%20X_i_k%20%2A%20Y_k_j&bc=White&fc=Black&im=jpg&fs=12&ff=arev&edit=)

Image below is the output of markdown. Hurray!!


enter image description here

Dexter
  • 1,621
  • 3
  • 18
  • 38
4

I just released a little Chrome extension, xhub, that lets you use LaTeX math (and more) in GitHub pages.

enter image description here

Pros:

  • You don't have to set up anything in your repo, just use math in your Markdown (sytax from GitLab):
    Some display math:
    ```math
    e^{i\pi} + 1 = 0
    ```
    and some inline math, $`a^2 + b^2 = c^2`$.
    
  • It works on light and dark backgrounds alike.
  • You can copy-and-paste the math

Cons:

  • You have to install a browser extension once.
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
2

There is good solution for your problem - use TeXify github plugin (mentioned by Tom Hale answer - but I developed his answer in given link below) - more details about this github plugin and explanation why this is good approach you can find in that answer.

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
1

I used the following in the head of mark down file

<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js? 
config=TeX-MML-AM_CHTML"
</script>

Then typed the following mathjax statement
$$x_{1,2} = \frac{-b \pm \sqrt{b^2-4ac}}{2b}.$$
It worked for me

1

A "quick and dirty" solution is to maintain a standard .md file using standard TeX equations, e.g. _README.md. When you are satisfied, pass the entire file through Pandoc to convert from standard Markdown to Markdown (Github flavour), and copy the output to README.md.

You can do this online for a quick turnaround, or install/configure Pandoc locally.

Brian
  • 115
  • 8
1

Mathcha is a sophisticated mathematics editor, but it can be used to render individual equations and save them as pure html, which you can then add to your documents as inline html OR you can save as SVG and insert as an image. https://www.mathcha.io/

Ezward
  • 17,327
  • 6
  • 24
  • 32
1

You can embed your LaTeX in an image URL rendered by render.githubusercontent.com such as this one:

<img src="https://render.githubusercontent.com/render/math?math={x + y}">

which will render like this:

Which you'll notice is missing the + sign. To fix that you can URL encode the plus sigh as %2b or URL encode the entire equation, which will render like so:

Unfortunately this will always render in black, so you'll want to use this GitHub specific trick to render white text for users using dark mode and black text to users using light mode, by including the equation once with the #gh-light-mode-only and again with the LaTeX comand \color{white} and the #gh-dark-mode-only tag:

<img src="https://render.githubusercontent.com/render/math?math={x - y}#gh-light-mode-only">
<img src="https://render.githubusercontent.com/render/math?math={\color{white}x - y}#gh-dark-mode-only">

which will display this to light mode users:

and display this to dark mode users:

Jthorpe
  • 9,756
  • 2
  • 49
  • 64
1

Now since May 2022, Github accept LATEX directly into Markdown, the only thing to do is to put the LATEX code inside $$$$ on your markdown

One more thing, you can colorize the math using the {\color{nameColor}text} on markdown

$${\color{red}\sum\limits_{\color{lightblue}i=0}^{\color{orange}n} {\color{pink}i}} = \frac{\color{pink}n!}{\color{lightblue}k!(n-k)!}$$

Example in a picture:

Colorized Sum

$$\sum\limits_{i=0}^n i^2$$ create the sum:

Normal Sum

F4NT0
  • 773
  • 7
  • 16
0

Regarding tex→image conversion, the tool LaTeXiT produces much higher quality output. I believe it is standard in most TeX distributions but you can certainly find it online if you don't already have it. All you need to do is put it in the TeX, drag the image to your desktop, then drag from your desktop to an image hosting site (I use imgur).

fredcallaway
  • 1,438
  • 12
  • 7
  • It should be noted that LaTeXiT seems to **only** work under MacOS (although that is not stated explicitly in the website). – ƒacu.- Nov 19 '14 at 08:24
0

TeXify is no longer working. Check my repo readme2tex-action on how to create Github actions.

  • Add action.yml file to your repo at .github/workflows/action.yml.
  • Change branch main name if it is necessary.
Tur Le
  • 1