# (Org) TikZ for exporting both LaTeX and HTML/MathJax

I'm exporting an Org Mode project both to LaTeX and to HTML (with MatJax). Now I need to add a TikZ picture which can be exported nativelly on LaTeX and must be rendered in svg for HTML.

Writing the TikZ env in Org directly works for LaTeX and even for HTML with dvipng/dvisvg, but not with MatJax:

#some org
\begin{tikzpicture}
% some tikz
\end{tikzpicture}
# some org


I tried embedding that in a LaTeX special block:

#some org
#+BEGIN_LaTeX
\begin{tikzpicture}
% some tikz
\end{tikzpicture}
#+END_LaTeX
# some org


but the result was the same. (BTW I read this syntax is now deprecated)

I tried with a babel block:

;; without this the tikz env is exported as verbatim code

#some org
#+BEGIN_SRC latex :file myfile.svg :imagemagick :results (if (org-export-derived-backend-p org-export-current-backend 'latex) "latex" "file")
\begin{tikzpicture}
% some tikz
\end{tikzpicture}
#+END_SRC
# some org


and in this case it ignores the header: it tries to export to a temporary pdf file, regardless of the options of file and results. I tried also with the #+header: syntax.

I found a solution:

(defvar is-latex (org-export-derived-backend-p org-export-current-backend 'latex))

#+BEGIN_SRC latex :file (unless is-latex "myfile.svg") :imagemagick (not is-latex) :results (if is-latex "latex" "graphics file")


This works in a sample file I made (gist), but:

• It does not include the packages provided in org-latex-packages-alist: you have to use the :headers property to pass usepackages
• Even if in my example I used two normal LaTeX blocks, when trying the same file with batch emacs and an minimal init file it complains the generated file is an HTML and not an SVG. It does not do this if the latex block contains a tikzpicture (the tikz package will be included by default)

There is another strange thing: with graphics in results header option the svg file is generated (even if in https://orgmode.org/manual/Results-of-Evaluation.html is said the contrary) but without graphics it is not included in the html.

• The problem seems to be babel uses htlatex and not dvisvgm
– DPD-
Sep 25 '21 at 17:33
• There was a change in ob-latex.el: in older versions (which I have in batch mode) the svg exporting was done via htlatex, in newer versions with inkscape
– DPD-
Sep 25 '21 at 18:01