一遍又一遍地使用同一张图片?

一遍又一遍地使用同一张图片?

假设我有一个用 TikZ 制作的复杂图形,如何多次使用它而不必重复所有命令?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}

Base figure

\begin{tikzpicture}
  \draw[blue] circle (2);
  \foreach \i in {0,10,...,350}
    \draw[blue] (\i:1.9) -- (\i:2.1);
\end{tikzpicture}

Using base figure.

\begin{tikzpicture}
  \draw[blue] circle (2);
  \foreach \i in {0,10,...,350}
    \draw[blue] (\i:1.9) -- (\i:2.1);
  \foreach \i in {0,10,...,350}
    \node at (\i:2.7) {\tiny $\i^\circ$};
\end{tikzpicture}

Using base figure.

\begin{tikzpicture}
  \draw[blue] circle (2);
  \foreach \i in {0,10,...,350}
    \draw[blue] (\i:1.9) -- (\i:2.1);
  \foreach \i in {0,10,...,350}
    \draw (0,0) -- (\i:2);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

您可以定义一个宏来构造基础图形,并提供一个添加到基础图形的可选参数。这样您就可以按原样使用它,但可以选择添加内容。这种抽象促进了一致性并允许将来轻松进行更改:

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
\begin{document}

\newcommand{\myfigure}[1][]{%
\begin{tikzpicture}
  \draw[blue] circle (2);
  \foreach \i in {0,10,...,350}
    \draw[blue] (\i:1.9) -- (\i:2.1);
  #1
\end{tikzpicture}%
}

Base figure

\myfigure

Using base figure.

\myfigure[%
  \foreach \i in {0,10,...,350}
  \node at (\i:2.7) {\tiny $\i^\circ$};%
]

Using base figure.

\myfigure[%
  \foreach \i in {0,10,...,350}
  \draw (0,0) -- (\i:2);%
]
\end{document}

您使用\myfigure它作为基准图形并\myfigure[<addition>]添加<addition>到基准图形中。

答案2

将其装入newcommand

\newcommand{\bluecircle}{
    \draw[blue] circle (2);
      \foreach \i in {0,10,...,350}
    \draw[blue] (\i:1.9) -- (\i:2.1);
}

现在你的最终数字将是:

\begin{tikzpicture}
    \bluecircle
    \foreach \i in {0,10,...,350}
    \draw (0,0) -- (\i:2);
\end{tikzpicture}

答案3

使用xsavebox

\documentclass{article}

\usepackage{tikz}
\usepackage{xsavebox}

\begin{document}

\begin{xlrbox}{base figure}
  \begin{tikzpicture}
    \draw[blue] circle (2);
    \foreach \i in {0,10,...,350}
      \draw[blue] (\i:1.9) -- (\i:2.1);
  \end{tikzpicture}
\end{xlrbox}%
%
Base figure:

\xusebox{base figure}

Using base figure:

\begin{tikzpicture}
  \node {\xusebox{base figure}};
  \foreach \i in {0,10,...,350}
    \node at (\i:2.7) {\tiny $\i^\circ$};%
\end{tikzpicture}%

Using base figure again:

\begin{tikzpicture}
  \node {\xusebox{base figure}};
  \foreach \i in {0,10,...,350}
    \draw (0,0) -- (\i:2);
\end{tikzpicture}

\end{document}

答案4

使用包的答案的变体scontents

\documentclass{article}
\usepackage{tikz}
\usepackage[store-env=figbase]{scontents}
\begin{document}
Store Base figure

\begin{scontents}
  \draw[blue] circle (2);
  \foreach \i in {0,10,...,350}
    \draw[blue] (\i:1.9) -- (\i:2.1);
\end{scontents}
% print fig base
\begin{tikzpicture}
\getstored[1]{figbase}
\end{tikzpicture}

Using base figure.

\begin{tikzpicture}
  \getstored[1]{figbase}
  \foreach \i in {0,10,...,350}
    \node at (\i:2.7) {\tiny $\i^\circ$};
\end{tikzpicture}

Using base figure.

\begin{tikzpicture}
  \getstored[1]{figbase}
  \foreach \i in {0,10,...,350}
    \draw (0,0) -- (\i:2);
\end{tikzpicture}
\end{document}

相关内容