对于我正在从事的一个项目,我正在制作一个 tikz 图片,并且需要多次使用相同的节点。有没有办法(例如 \tikzset 或其他)让我可以定义一个节点一次,然后在其他地方重新打印/重复使用它?
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[rectangle, draw] (node1) {InsertComplexNodeHere};
\node[rectangle, draw] (node2) [below of=node1] {ExactSameAsAbove};
\end{tikzpicture}
\end{document}
编辑以获取更多信息:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[rectangle, draw] (node1) {\includegraphics{image.png}};
\node[rectangle, draw] (node2) [below of=node1] {\includegraphics{image.png}};
\end{tikzpicture}
\end{document}
我需要在不同位置多次使用这张图片,但可能需要更改图像引用/名称/大小/等等,并且希望先定义一次,然后执行以下操作:
\node[copy of node1] (node3) at (3,3)
答案1
这听起来像是 的一个用例node contents
。这是一个键,它的作用正如它的名字所言:它指定内容节点。这意味着\node {contents};
可以通过 TikZ 样式设置内容,从而提供更大的灵活性。
\documentclass{article}
%\url{https://tex.stackexchange.com/q/699407/86}
\usepackage{tikz}
\usetikzlibrary{
positioning,
shapes.geometric
}
\tikzset{
my complicated node/.style={
node contents={Some complicated text},
draw,
ellipse,
}
}
\begin{document}
\begin{tikzpicture}
\node[my complicated node,name=node1];
\node[my complicated node,below=of node1];
\end{tikzpicture}
\end{document}
答案2
使用@Alan Munn 的建议(我之前从未听说过保存箱),我能够解决这个问题。谢谢!
\documentclass{standalone}
\newsavebox\Reuseblock
\sbox\Reuseblock{\begin{tikzpicture}
\node (ru) {\includegraphics{image.png}};
\end{tikzpicture}}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[rectangle, draw] (node1) {\usebox\Reuseblock};
\node[rectangle, draw] (node2) [below of=node1] {\\usebox\Reuseblock};
\end{tikzpicture}
\end{document}
答案3
这是一个更类似 Tikz 的解决方案。
第一页使用一个简单的自定义图片样式,称为img
,并且可以放置任意数量的图片:
\begin{tikzpicture}[% self-defined styles to be used here
img/.pic={% now just draw what shall be inside img
\node {\includegraphics{example-image-duck}};
}
]
% ~~~ placing two nodes containing the image ~~~
\pic at (0,0) {img}; % read: a pic at (0,0) with style img
\pic at (8,4) {img};
\pic at (15,-7) {img};
\end{tikzpicture}
第二个示例仅传递 1 个参数。请参阅要使用的样式的不同“调用”的更好规范:
\begin{tikzpicture}[
pics/img2/.style args={#1}{% allow one parameter to be passed
code={
\node (A) {\includegraphics{example-image-duck}};
\node [below=of A,yshift=5mm] {#1};
}
}
]
% ~~~ placing two nodes containing the image ~~~
\pic at ( 0,-3) {img2={A duck!}};
\pic at (15,-4) {img2={May be duck}};
\pic at ( 8, 7) {img2={No duck}};
\end{tikzpicture}
完整代码:
\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{graphicx}% when you want to use images, incl. the duck
\usetikzlibrary{positioning}
\begin{document}
% ~~~ 1st example ~~~~~~~~~~~~~~~~~~~~
\begin{tikzpicture}[% self-defined styles to be used here
img/.pic={% now just draw what shall be inside img
\node {\includegraphics{example-image-duck}};
}
]
% ~~~ placing two nodes containing the image ~~~
\pic at (0,0) {img}; % read: a pic at (0,0) with style img
\pic at (8,4) {img};
\pic at (15,-7) {img};
\end{tikzpicture}
% ~~~ 2nd example ~~~~~~~~~~~~~~~~~~~~
\begin{tikzpicture}[
pics/img2/.style args={#1}{% allow one parameter to be passed
code={
\node (A) {\includegraphics{example-image-duck}};
\node [below=of A,yshift=5mm] {#1};
}
}
]
% ~~~ placing two nodes containing the image ~~~
\pic at ( 0,-3) {img2={A duck!}};
\pic at (15,-4) {img2={May be duck}};
\pic at ( 8, 7) {img2={No duck}};
\end{tikzpicture}
\end{document}