我有几张 tikz 图片,它们只有一行不同。我想重复使用始终相同的部分\savebox
(来源),但我一直收到错误。出了什么问题?
MWE(无 \savebox)
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[step=1,help lines,black!20] (-0.99,-0.99) grid (8.99,8.99);
\draw[thick,->] (-1,0) -- (9,0) node[pos=0.97,below] {$x$};
\draw[thick,->] (0,-1) -- (0,9) node[pos=0.97,left] {$y$};
% Graph 1
\draw[very thick,color=black,domain=-1:9] plot (\x,{\x});
\end{tikzpicture}
\begin{tikzpicture}
\draw[step=1,help lines,black!20] (-0.99,-0.99) grid (8.99,8.99);
\draw[thick,->] (-1,0) -- (9,0) node[pos=0.97,below] {$x$};
\draw[thick,->] (0,-1) -- (0,9) node[pos=0.97,left] {$y$};
% Graph 2
\draw[very thick,color=black,domain=-1:9] plot (\x,{0.5*\x});
\end{tikzpicture}
\end{document}
MWE (使用 \savebox)
\documentclass[tikz]{standalone}
\begin{document}
\newsavebox{\mygrid}
\savebox{\mygrid}{
\draw[step=1,help lines,black!20] (-0.99,-0.99) grid (8.99,8.99);
\draw[thick,->] (-1,0) -- (9,0) node[pos=0.97,below] {$x$};
\draw[thick,->] (0,-1) -- (0,9) node[pos=0.97,left] {$y$};
}
\begin{tikzpicture}
\usebox{\mygrid}
% Graph 1
\draw[very thick,color=black,domain=-1:9] plot (\x,{\x});
\end{tikzpicture}
\begin{tikzpicture}
\usebox{\mygrid}
% Graph 2
\draw[very thick,color=black,domain=-1:9] plot (\x,{0.5*\x});
\end{tikzpicture}
\end{document}
截图
图 1
图2
答案1
A\savebox
只能存储一个完整的盒子。完整的 tikzpicture 有资格在 中使用\savebox
,而路径集合则不行。Ti钾这样做的 Zy 方式是定义一个pic
。(style=grid
在您的 MWE 中没有定义,所以我编造了一些东西。)
\documentclass[tikz]{standalone}
\tikzset{grid/.style={draw=gray,thin},
pics/alwaysthesame/.style={code={%
\draw[style=grid] (-0.99,-0.99) grid (8.99,8.99);
\draw[thick,->] (-1,0) -- (9,0) node[pos=0.97,below] {$x$};
\draw[thick,->] (0,-1) -- (0,9) node[pos=0.97,left] {$y$};
}}}
\begin{document}
\begin{tikzpicture}
\pic{alwaysthesame};
% Graph 1
\draw[very thick,color=black,domain=-1:9] plot (\x,{\x});
\end{tikzpicture}
\begin{tikzpicture}
\pic{alwaysthesame};
% Graph 2
\draw[very thick,color=black,domain=-1:9] plot (\x,{0.5*\x});
\end{tikzpicture}
\end{document}
您还可以使用every picture
和execute at begin picture
键,这样就无需在每张图片中输入图片了。现在网格的代码已更新。
\documentclass[tikz]{standalone}
\tikzset{pics/alwaysthesame/.style={code={%
\draw[step=1,help lines,black!20] (-0.99,-0.99) grid (8.99,8.99);
\draw[thick,->] (-1,0) -- (9,0) node[pos=0.97,below] {$x$};
\draw[thick,->] (0,-1) -- (0,9) node[pos=0.97,left] {$y$};
}},every picture/.append style={execute at begin picture={\pic{alwaysthesame};}}}
\begin{document}
\begin{tikzpicture}
% Graph 1
\draw[very thick,color=black,domain=-1:9] plot (\x,{\x});
\end{tikzpicture}
\begin{tikzpicture}
% Graph 2
\draw[very thick,color=black,domain=-1:9] plot (\x,{0.5*\x});
\end{tikzpicture}
\end{document}
你可以将完整的内容保存tikzpicture
在 a 中并将其放入 a 节点中。这确实有效,但执行此操作时\savebox
需要非常小心。inner sep
\documentclass[tikz]{standalone}
\newsavebox\AlwaysTheSame
\sbox\AlwaysTheSame{\begin{tikzpicture}
\draw[step=1,help lines,black!20] (-0.99,-0.99) grid (8.99,8.99);
\draw[thick,->] (-1,0) -- (9,0) node[pos=0.97,below] {$x$};
\draw[thick,->] (0,-1) -- (0,9) node[pos=0.97,left] {$y$};
\end{tikzpicture}}
\begin{document}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0pt,outer sep=0pt] at (-0.99,-0.99)
{\usebox\AlwaysTheSame};
% Graph 1
\draw[very thick,color=black,domain=-1:9] plot (\x,{\x});
\end{tikzpicture}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0pt,outer sep=0pt] at (-0.99,-0.99)
{\usebox\AlwaysTheSame};
% Graph 2
\draw[very thick,color=black,domain=-1:9] plot (\x,{0.5*\x});
\end{tikzpicture}
\end{document}
答案2
除了薛定谔猫提供的解决方案之外,还可以使用 来实现\newcommand
。
有人应该在这里解释为什么盒子不是解决这个问题的正确方法。我认为这不是最清楚的解释,但这就是它。粗略地说,盒子\box
是已经格式化的存储材料的集合。当您“使用”盒子时,盒子中存储的已格式化材料将被转储到页面上。特别是,不允许在盒子中存储任何逻辑。Tikz 需要大量逻辑来控制绘制全局状态(例如,如果 tikz 向 pdf 添加曲线,则绘制曲线所使用的颜色、线条粗细和虚线图案取决于全局状态)。因此,如果您想存储 Tikz 代码,请将其存储在宏中或使用 Scrodinger's cat 指示的技术之一。
\documentclass[tikz]{standalone}
\begin{document}
\tikzset{gridstyle/.style={help lines}}
\newcommand{\mygrid}{
\draw[gridstyle] (-0.99,-0.99) grid (8.99,8.99);
\draw[thick,->] (-1,0) -- (9,0) node[pos=0.97,below] {$x$};
\draw[thick,->] (0,-1) -- (0,9) node[pos=0.97,left] {$y$};
}
\begin{tikzpicture}
\mygrid
% Graph 1
\draw[very thick,color=black,domain=-1:9] plot (\x,{\x});
\end{tikzpicture}
\begin{tikzpicture}
\mygrid
% Graph 2
\draw[very thick,color=black,domain=-1:9] plot (\x,{0.5*\x});
\end{tikzpicture}
\end{document}