如何将数学模式放入 TikZ 图片内?

如何将数学模式放入 TikZ 图片内?

我有以下使用 TikZ 图片的方程式代码。

\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}

即,请注意文本节点中内联数学的使用。不幸的是,它不起作用,并产生以下错误:

(/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.

那么,实现这一目标的诀窍是什么呢?

之前在这里问过一个相关问题数学模式下的 TikZ 图表但对于节点中的数学模式文本。

附录

@gernot 提供的解决方案对我有用,只要我们multi={equation*}在文档类声明中定义即可。这是获得正确裁剪输出所必需的。

答案1

正如 David 在评论中指出的那样,问题是由于使用该类而导致的standalone。因此,一种解决方案是使用另一个文档类。

如果您必须使用standalone,则可以使用\savebox

  • 定义一个保存箱,例如名为 的保存箱\mypic

    \newsavebox\mypic
    
  • 将图片保存到框里:

    \savebox\mypic{\begin{tikzpicture}...\end{tikzpicture}}
    
  • 也可以在像以下等式这样的困难情况下使用方框standalone

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

在此处输入图片描述

\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}

相关内容