Convert .md with equations to .pdf using pandoc

Hello guys,

I just want to report that I managed to convert my .md file with lots of latex equations to a .pdf using pandoc.

I have noticed if you want the equation numbers I have to use the plain code

\begin{equation}
I(x)=I_0e^{-ax}.
\end{equation}

If I use just the regular $$I(x)=I_0e^{-ax}.$$ the output is fine, but without the eq. number, tho.

I tried using

$$\begin{equation}I(x)=I_0e^{-ax}\end{equation}$$

But it doesn’t work, naturally.

As I said in here: How to show latex equation numbers? I’m looking forward to see some more advanced latex environments like equation to be compatible with obsidian.

Have a nice day :facepunch:t4:
Best regards


Gabriel

4 Likes

It’d be helpful for future readers if you can include the example pandoc command as well. Thanks!

1 Like

Thanks for your suggestion. There is a very good tutorial in the pandoc link I posted on my comment.

Basically, after you install pandoc, just open the terminal/cmd in the folder of your .md file and type the following

pandoc file.md -s -o file.pdf

That’s pretty much it. For more info open the link in my post.

Regards,
Gabriel

Hello, I have used this method but it seems that pandoc doesn’t understands mermaid on the pdf export. Do you know any way to solve this?

1 Like

EDIT: I should have just googled before responding. It seems such a filter I described below already exists: https://github.com/timofurrer/pandoc-mermaid-filter.


I haven’t done it for mermaid, but here is a method that should work.

Pandoc works by converting every document type into an intermediate AST representation, then encoding that AST again into the new format. It allows you to modify this AST by a filter before the encoding is done.

For example, take this md (removed the 3 backticks inside the md doc, because it confuses the forum:

$ cat test.md
This is a mermaid diagram

{three backticks here}mermaid
graph TD
A[CREATIVITY] --> B[Combinatorial]
A[CREATIVITY] --> C[Exploratory]
{three backticks here}

The AST is a JSON:

pandoc -t json test.md | jq

{
  "blocks": [
    {
      "t": "Para",
      "c": [
        {
          "t": "Str",
          "c": "This"
        },
        {
          "t": "Space"
        },
        {
          "t": "Str",
          "c": "is"
        },
        {
          "t": "Space"
        },
        {
          "t": "Str",
          "c": "a"
        },
        {
          "t": "Space"
        },
        {
          "t": "Str",
          "c": "mermaid"
        },
        {
          "t": "Space"
        },
        {
          "t": "Str",
          "c": "diagram"
        }
      ]
    },
    {
      "t": "CodeBlock",
      "c": [
        [
          "",
          [
            "mermaid"
          ],
          []
        ],
        "graph TD\nA[CREATIVITY] --> B[Combinatorial]\nA[CREATIVITY] --> C[Exploratory]"
      ]
    }
  ],
  "pandoc-api-version": [
    1,
    17,
    5,
    1
  ],
  "meta": {}
}

Note that there is a CodeBlock node in the tree, with type “mermaid”.

You should be able to write a filter to run the content of the block through something like this to generate an image, then insert replace the CodeBlock node with an image reference.

Then, when pandoc writes the latex then pdf, it’ll get included as an image.

2 Likes