mgaitan/sphinxcontrib-mermaid

include mermaid graph in latexpdf

LuisBL opened this issue · 3 comments

With this simple test

$ tree
.
├── _build
├── conf.py
├── index.rst
├── Makefile
├── requirements.txt
├── _static
└── _templates
$
$ tail -1 conf.py
extensions = ['sphinxcontrib.mermaid']
$
$ cat index.rst                             
Test mermaid with `sphinx latexpdf`
====================================

Test mermaid with `sphinx latexpdf`

Try to render::

   graph TD
      A --> B --> C --> A

.. mermaid::

   graph TD
      A --> B --> C --> A

Mermaid rendered above ?
$ 
$ make html
...
$ firefox _build/html/index.html
$

Sélection_714

$ make latexpdf
...
$ evince _build/latex/mermaid_test.pdf `
$

Sélection_713

look's like the SVG is not added to the tex file::

$ tail -20 _build/latex/mermaid_test.tex
\pagestyle{normal}
\phantomsection\label{\detokenize{index::doc}}
Test mermaid with \sphinxtitleref{sphinx latexpdf}
Try to render:

\begin{sphinxVerbatim}[commandchars=\\\{\}]
\PYG{n}{graph} \PYG{n}{TD}
   \PYG{n}{A} \PYG{o}{\PYGZhy{}}\PYG{o}{\PYGZhy{}}\PYG{o}{\PYGZgt{}} \PYG{n}{B} \PYG{o}{\PYGZhy{}}\PYG{o}{\PYGZhy{}}\PYG{o}{\PYGZgt{}} \PYG{n}{C} \PYG{o}{\PYGZhy{}}\PYG{o}{\PYGZhy{}}\PYG{o}{\PYGZgt{}} \PYG{n}{A}
\end{sphinxVerbatim}

Mermaid rendered above ?
\renewcommand{\indexname}{Index}
\printindex
\end{document}(venv) 
$
mapio commented

Have you installed https://github.com/mermaid-js/mermaid-cli? In my logs I've found an error related to a missing command.

If you don't want to install the command but use the dockerized version this script can help; save it and point to it with the mermaid_cmd option in your config.py (dont forget to also define mermaid_pdfcrop).

#!/bin/bash

in=$2
out=$4

wd=$(realpath $(pwd))
out=${out#$wd}
outdir=$(dirname $out)
infile=$(basename $in)

cp $in .$outdir/$infile
docker run --rm -v $wd:/wd -u $(id -u) minlag/mermaid-cli -i /wd$outdir/$infile -o /wd${out}

In windows,when you type where mmdc in cmd,you will get two results as follows:

D:\Software\node\mmdc
D:\Software\node\mmdc.cmd

so you should add mermaid_cmd = "D:\\Software\\node\\mmdc.cmd" into your conf.py, and add mermaid_pdfcrop = "path of your pdfcrop" into conf.py too.

As mentioned here, you can alternatively replace pdf with png at row257 in mernaid.py. This forces the rendering process to use 'PNG' files instead pdf's It's not the highest quality, but it's a workaround.
For this solution uncomment mermaid-pdfcrop to prevent some errors.