请问是否可以通过以下方式收集 tikz 图片?我得到了\box40=\hbox(0.0+0.0)x0.0 []
。我甚至更喜欢\vbox
,但结果更糟。
\makeatletter
\newbox\tempbox
\newbox\mainbox
\def\collecttikzpicture{\@testopt\@collecttikzpicture{}}
\def\@collecttikzpicture[#1]{%
\global\setbox\tempbox=\hbox\bgroup\begin{tikzpicture}[#1]%
}
\def\endcollecttikzpicture{%
\end{tikzpicture}\egroup
\setbox\mainbox=\hbox{\unhbox\mainbox\unhbox\tempbox}%
}
\begin{collecttikzpicture}[remember picture,overlay]
\draw [cyan,fill=yellow]
(0cm,0cm)--(2cm,0cm)--(2cm,-2cm)--(0cm,-2cm)--cycle;
\end{collecttikzpicture}
\showbox\mainbox
\makeatother
答案1
您需要在环境组之外收集您的主箱:
\def\endcollecttikzpicture{%
\end{tikzpicture}\egroup
\aftergroup\collectbox
}
\def\collectbox{\setbox\mainbox=\hbox{\unhbox\mainbox\unhbox\tempbox}}
请注意,这使得\mainbox
包含图片,但由于传递给 tikzpicture 的覆盖选项,它仍然具有零大小。如果删除该选项,框将达到正方形的自然尺寸。
正如你所说,你更喜欢 vbox:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\makeatletter
\newbox\tempbox
\newbox\mainbox
\def\collecttikzpicture{\@testopt\@collecttikzpicture{}}
\def\@collecttikzpicture[#1]{%
\global\setbox\tempbox=\hbox\bgroup\begin{tikzpicture}[#1]%
}
\def\endcollecttikzpicture{%
\end{tikzpicture}\egroup
\aftergroup\collectbox
}
\def\collectbox{\setbox\mainbox=\vbox{\unvbox\mainbox\box\tempbox}}
\makeatother
\begin{collecttikzpicture}[remember picture]
\draw [cyan,fill=yellow]
(0cm,0cm)--(2cm,0cm)--(2cm,-2cm)--(0cm,-2cm)--cycle;
\end{collecttikzpicture}
\begin{collecttikzpicture}[remember picture]
\draw [cyan,fill=blue]
(0cm,0cm)--(2cm,0cm)--(2cm,-2cm)--(0cm,-2cm)--cycle;
\end{collecttikzpicture}
\begin{collecttikzpicture}[remember picture]
\draw [cyan,fill=green]
(0cm,0cm)--(2cm,0cm)--(2cm,-2cm)--(0cm,-2cm)--cycle;
\end{collecttikzpicture}
[[[\box\mainbox]]]
\end{document}