Sunday 12 April 2020

Adding latex code in Github markdown files

Enter your latex forumula in markdown file within $ symbols.

Eg.
INPUT.md:
________________________________________________
   #### Annual effective interest rate
 $r = (\frac{X_T}{X_0})^\frac{1}{T} - 1$
 In other words: $X_T = X_0(1+r)^T$
________________________________________________


First install readme2tex:

   sudo pip install readme2tex

Convert the file to display latex formulae using the command:
   python3 -m readme2tex --nocdn --output OUTPUT.md INPUT.md

This will automatically create a directory svgs inside the same directory containing the images generated for the latex formulae.
However Github doesn't allow to render svg files inside a markdown due to some security reasons.
So, lets convert these svg files to png format.

Install imagemagick
   sudo apt install imagemagick

Then convert all svg files to png files using the command:
   for file in *.svg; do convert -density 1200 $file ${file%svg}png; done

You can also do the above using inkscape
   for file in *.svg; do inkscape $file -w 1200 --export-png=${file%svg}png; done

Open the file OUTPUT.md and replace ".svg" to ".png" everywhere.
The OUTPUT.md should look something like:
________________________________________________
   #### Annual effective interest rate

   <img src="svgs/09abd6964a4a7789fa914edf5d65a0ca.png?invert_in_darkmode" align=middle width=107.73658500000002pt height=31.359569999999987pt/>

   In other words: <img src="svgs/69f4ffcb7136df5381543f273dc01b7a.png?invert_in_darkmode" align=middle width=125.38779000000001pt height=27.656969999999987pt/>
 ________________________________________________

You are done.


No comments:

Post a Comment