1

When I receive a Markdown file, I can open it in a markdown editor like React on Linux or Markdown Monster on Windows etc. However - such editors do not support rendering of LaTeX embedded in the Markdown as inline math (i.e. with single-dollar signs) - and I'm faced with the task of rendering such documents.

Obviously this is supported on some web-based systems - like the one here on TeX.SX . But I am in a terminal with a command shell; or am possibly writing a script, to do this. I'm on a Linux system.

The rendered output format should be PDF, HTML with MathML, HTML with rendered images, or anything else that's reasonable. Preferably I should be able to choose among these output type options.

10
  • most systems leavd the tex as tex and arrange to add mathjax script call to the page Commented Apr 18, 2021 at 16:19
  • @DavidCarlisle: Isn't "MathJax" something that runs on web servers? Commented Apr 18, 2021 at 16:54
  • well you can host it locally on the filesystem as well but normally yes, but wouldn't you say the same about "html with mathml" what would you do with that if not put it on a server? Commented Apr 18, 2021 at 17:00
  • @DavidCarlisle: Open it with a browser? Commented Apr 18, 2021 at 17:03
  • 1
    @einpoklum - Are you aware of pandoc? Commented Apr 18, 2021 at 17:49

4 Answers 4

4

If you need a command line tool to convert markdown to pdf or html, then pandoc might be a good candidate. You simply call:

$ pandoc infile.md -o outfile.pdf 

or

$ pandoc infile.md -o outfile.html 

to generate the desired output.

You can configure the process, if desired. For math in html, you could choose different rendering options, like --mathjax or --mathml. If you need embedded pictures, use --webtex. This is all documented in the manual: https://pandoc.org/MANUAL.html#math-rendering-in-html

3
  • So, do --mathjax and --mathml indicate what gets processed, or what gets placed in the output? Also, how do I indicate pieces of LaTeX with each of these options? $ on both ends? $$? \( and \)? Commented Apr 18, 2021 at 21:17
  • The options determine html output., cf. pandoc.org/MANUAL.html#math-rendering-in-html You can choose how tex math is handled, cf. pandoc.org/MANUAL.html#math-input Commented Apr 19, 2021 at 7:18
  • --mathml worked great, Thanks. --mathjax failed with this error /usr/share/javascript/mathjax/tex-mml-chtml.js: openBinaryFile: does not exist (No such file or directory) Commented Aug 19, 2024 at 23:38
2

You should consider use Rstudio and Rmarkdown. There are support for in line LaTeX math previews, and also can export it to PDF using pandoc and LaTeX in the background, or to HTML or even Word.

5
  • 1
    Can you expand regarding what Rstudio is, exactly? And is Rmarkdown a plugin for it? An extension of it? Commented Jun 8, 2021 at 19:03
  • Too long for a comment, but basically Rstudio is an IDE thought primarily to help with R programming, but it is also a markdown/LaTeX/HTML editor and much more. Need R and some R packages, pandoc, and LaTeX in the background, but the installation of all of this is not at all as difficult as messing with the plugins of another famous editors. Commented Jun 8, 2021 at 19:24
  • Please edit that into your answer... Commented Jun 8, 2021 at 19:28
  • It is not worth elaborating here the features of Rstudio since there are tons of info about out there (even in Wikipedia). My suggestion is just go to the official page and test it (the personal version is free). For a rmarkdown document is File > New File > R Markdown > (choose options) > OK > (edit as you want) > knit > choose HTML, PDF or Word output and see the result. Commented Jun 8, 2021 at 20:07
  • I meant, the contents of your last comment belongs in your answer. Commented Jun 8, 2021 at 21:04
2

Rendering is a good idea and pandoc would be my way to go. But if it is just a matter of display, you could also look into Typora

Typora is (currently) my markdown editor of choice. It renders markdown live into a document. This makes editing quite easy, because there is a direct visual feedback, whether the markdown syntax is correct ;-). If the syntax is not correct, it will not render as expected. This is all handled in one window and not in a side-by-side view as in other editors. Thus the text changes between markdown syntax and the rendered output. Some people don`t like that. But in my opinion it is solved quite good in Typora. But the most important part for this question is: There it also support for LaTeX-Equations and Mermaid-Diagrams.

Typora also supports PDF exports and an integration with pandoc.

Furthermore, I like the minimalistic look of the program.

Another option would be zettlr, which I also tested a while ago in parallel with Typora. In the end I went for Typora. Zettlr has similar capabilities. But I am not as aware of them and would not like to go into details. I can only mention, that a colleague of mine is quite happy with zettlr.

EDIT: As Typora has become a paid app, I tested also some others with similar features. For now I settled for MarkText, which also has the requested feature of rendering LaTeX inline math.

2
  • Please expand a bit about Typora rather than just providing the link. Commented Jun 8, 2021 at 19:02
  • I elaborated a bit on the tool. But at the end I really can just recommend to give it a try if it fits your needs. I also mentioned an alternative now. In the end its a matter of the technical needs and personal taste. Commented Jun 9, 2021 at 5:18
0

If you want to use MathJax (the most common solution) you just need to configure your markdown processor to leave the mathematics unchanged, but add a <script> element to the page head.

So if you start from the markdown

# test some text \(\frac{x}{y}=z^2\) 

Generate HTML

<!DOCTYPE html> <html> <head> <title>test</title> <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script> </head> <body> <h1>test</h1> <p> test \(\frac{x}{y}=z^2\)</p> </body> </html> 

which renders as

enter image description here

2
  • 1. So, which Markdown processor would do this for me? 2. So, it seems I will need to strip single-dollar-signs? Commented Apr 18, 2021 at 19:20
  • 1. any I would guess, all you have to do with the math is not mess it up (you may need to double \\ if there isn't an extension such as {:raw:} avalable in github pages. 2. no \( and \[ are the default but you can tell mathjax to look for $ ans $$ as well see docs.mathjax.org/en/latest/web/configuration.html @einpoklum Commented Apr 18, 2021 at 20:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.