2

I have the following code for an equation using TikZ pictures.

\documentclass[crop,tikz,convert={outext=.svg,command=\unexpanded{pdf2svg \infile\space\outfile}},multi=false]{standalone}[2012/04/13]
\usepackage{amsmath}

\def\Hask{\mathsf{Hask}}
\def\Id{\mathsf{Id}}
\def\T{\mathsf{T}}

\begin{document}
\begin{equation*}
  \begin{tikzpicture}[node distance=2cm, auto]
    \node (H1) {$\Hask$};
    \node (H2) [right of= H1] {$\Hask$};
    \draw[->, bend left] (H1) to node[above] {$\Id_{\Hask}$} (H2);
    \draw[->, bend right] (H1) to node[below] {$\T$} (H2);
  \end{tikzpicture} =
  \begin{tikzpicture}[node distance=2cm, auto]
    \node (H1) {$\Hask$};
    \node (H2) [right of= H1] {$\Hask$};
    \draw[->, bend left] (H1) to node[above] {$\Id_{\Hask}$} (H2);
    \draw[->, bend right] (H1) to node[below] {$\T$} (H2);
  \end{tikzpicture}
\end{equation*}
\end{document}

I.e., note the use of in-line math in the text nodes. Unfortunately it does not work, and yields the following errors:

(/usr/local/share/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))
! Missing $ inserted.
<inserted text> 
                $
l.28   \end{tikzpicture}
                         =
? 
! Missing \endgroup inserted.
<inserted text> 
                \endgroup 
l.28   \end{tikzpicture}
                         =
? 
! Display math should end with $$.
<to be read again> 
                   \par 
l.28   \end{tikzpicture}
                         =
? 
[1]

! LaTeX Error: \begin{equation*} on input line 22 ended by \end{tikzpicture}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.28   \end{tikzpicture}
                         =
? 

Overfull \hbox (22.7778pt too wide) in paragraph at lines 28--34
[]\OT1/cmr/m/n/10 = 

Overfull \vbox (3.66875pt too high) has occurred while \output is active
[2{/usr/local/share/texmf-var/fonts/map/pdftex/updmap/pdftex.map
}] [3]

! LaTeX Error: Bad math environment delimiter.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.35 \end{equation*}
                    
? 
! LaTeX Error: \begin{document} ended by \end{equation*}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.35 \end{equation*}
                    
? 
! Missing $ inserted.
<inserted text> 
                $
l.35 \end{equation*}
                    
? 
! Display math should end with $$.
<to be read again> 
                   \endgroup 
l.35 \end{equation*}
                    
? 
! Extra \endgroup.
<recently read> \endgroup 
                          
l.35 \end{equation*}
                    
? 
[4] (./monadUnit.aux) )</usr/local/share/texmf-dist/fonts/type1/public/amsfonts
/cm/cmr10.pfb></usr/local/share/texmf-dist/fonts/type1/public/amsfonts/cm/cmss1
0.pfb></usr/local/share/texmf-dist/fonts/type1/public/amsfonts/cm/cmss8.pfb>
Output written on monadUnit.pdf (4 pages, 24416 bytes).
Transcript written on monadUnit.log.
system returned with code 256

Class standalone:
Output written on monadUnit.svg.
 ) )
No pages of output.
Transcript written on monadUnit.log.

So what is the trick to make this work?

A related question was asked here before TikZ Diagrams in Math Mode but for the math mode text in the nodes.

Addendum

The solution provided by @gernot works for me, provided we define multi={equation*} in the document class declaration. This is needed to get correctly cropped output.

3
  • 2
    your fragment is fine in normal classes the error is an artifact of standalone processing Jan 5, 2022 at 15:18
  • Unrelated: Add the option baseline=(H1.base) to the two tikzpictures, to align the pictures with the equality sign
    – gernot
    Jan 5, 2022 at 15:20
  • Lovely. Thank you for that. Which makes me wonder, why does it not work normally with standalone?
    – 6d03
    Jan 5, 2022 at 18:29

1 Answer 1

1

As David pointed out in the comments, the problem is the result of using the standalone class. So one solution is to use another document class.

If you are bound to use standalone, you can use a \savebox:

  • Define a savebox, e.g. one called \mypic.

    \newsavebox\mypic
    
  • Save the picture into the box:

    \savebox\mypic{\begin{tikzpicture}...\end{tikzpicture}}
    
  • Use the box, also in difficult contexts like the equation in standalone:

    \begin{equation*}
      \usebox\mypic = \usebox\mypic
    \end{equation*}
    

enter image description here

\documentclass[crop,tikz,convert={outext=.svg,command=\unexpanded{pdf3svg \infile\space\outfile}},multi=false]{standalone}[2012/04/13]
\usepackage{amsmath}

\def\Hask{\mathsf{Hask}}
\def\Id{\mathsf{Id}}
\def\T{\mathsf{T}}

\newsavebox\mypic
\savebox\mypic{\begin{tikzpicture}[node distance=2cm, auto]
    \node (H1) {$\Hask$};
    \node (H2) [right of= H1] {$\Hask$};
    \draw[->, bend left] (H1) to node[above] {$\Id_{\Hask}$} (H2);
    \draw[->, bend right] (H1) to node[below] {$\T$} (H2);
  \end{tikzpicture}}
\begin{document}
\begin{equation*}
  \usebox\mypic = \usebox\mypic
\end{equation*}
\end{document}
4
  • Thank you, this is very nice and compiles without error. But now I had to change the multi option to {equation*} to get correctly cropped output. This does produce the output I expect, but also throws an error: ``` ! LaTeX Error: Bad math environment delimiter. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.22 \end{equation} ``` Any idea how that can be fixed?
    – 6d03
    Jan 5, 2022 at 18:17
  • I do need to use standalone for cropping and the output conversion to SVG. Any idea why this problem arises?
    – 6d03
    Jan 5, 2022 at 18:33
  • @6d03 This is a problem of standalone with crop and the equation environment and is unrelated to tikz. See tex.stackexchange.com/a/50171/110998 for a discussion of this problem, with workarounds (e.g., use \(...\) instead of the equation environment, maybe with \displaystyle.
    – gernot
    Jan 5, 2022 at 20:18
  • Thanks @gernot. It turns out cleaning aux files and rerunning the build fixed it.
    – 6d03
    Jan 6, 2022 at 11:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .