可以重复使用 TikZ 代码吗?

可以重复使用 TikZ 代码吗?

假设创建下图并将其保存在文件中figure.tex

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{
                arrows.meta,
                bending,
                positioning
               }
\tikzset{
         > = Latex,
         arrows = {[bend]},
         signal/.style = coordinate,
         sum/.style = {
                       draw,
                       circle,
                       minimum size = 2mm
                      },
         block/.style = {
                         draw,
                         rectangle,
                         minimum height = 2em,
                         minimum width = 4em
                        },
         branch/.style = {
                          sum,
                          minimum size = 1mm,
                          fill = black
                         }
        }

\begin{document}

  \begin{tikzpicture}[auto]

    %placing the nodes
    \node[signal] (input) {};
    \node[sum, right = of input] (left sum) {};
    \node[block, right = of left sum] (controller) {$G_R$};
    \node[block, right = of controller] (system) {$G_S$};
    %connecting the controller and system to get the coordinates of u, its needed for the placement of the measurement block
    \draw
      [->] (controller) -- node[name = u] {$U$} (system);
    \node[block, above = of system] (dynamic of disturbances) {$G_D$};
    \node[signal, left = of dynamic of disturbances] (disturbances) {};
    \node[sum, right = of system] (right sum) {};
    \node[branch, right = of right sum] (branch) {};
    \node[signal, right = of branch] (output) {};
    \node[sum, below = of branch] (lower sum) {};
    \node[signal, right = of lower sum] (measurement noise) {};
    \node[block] (measurement) at (u |- lower sum) {$G_M$};
    %connecting the nodes
    \draw
      [->] (input) -- node {$W$} (left sum);
    \draw
      [->] (left sum) -- node {$E$} (controller);
    \draw
      [->] (system) -- (right sum);
    \draw
      [->] (disturbances) -- node {$Z$} (dynamic of disturbances);
    \draw
      [->] (dynamic of disturbances) -| (right sum);
    \draw
      (right sum) -- (branch);
    \draw
      [->] (branch) -- node {$Y$} (output);
    \draw
      [->] (branch) -- (lower sum);
    \draw
      [->] (measurement noise) -- node[above] {$M$} (lower sum);
    \draw
      [->] (lower sum) -- (measurement);
    \draw
      [->] (measurement) -| node[pos = .95] {$-$} (left sum);

  \end{tikzpicture}

\end{document}

结果:

图1

有人希望加载文件\input{figure}并添加一些代码(不更改文件figure.tex),例如箭头,我的相关问题。有可能吗?

答案1

为什么不把所有东西都放到宏里?在这个例子中,我使用了一个范围,以便能够auto在图表中使用,而不是在图表外部,但如果您在任何地方使用,则不需要这样做auto……

iii.tex

%%% probably is better left in your main file, but nothing happens if you have this 
%%% multiple times
%%%
\usepackage{tikz}
%%%
%%% ditto
%%%
\usetikzlibrary{
                arrows.meta,
                bending,
                positioning
               }
\tikzset{
         > = Latex,
         arrows = {[bend]},
         signal/.style = coordinate,
         sum/.style = {
                       draw,
                       circle,
                       minimum size = 2mm
                      },
         block/.style = {
                         draw,
                         rectangle,
                         minimum height = 2em,
                         minimum width = 4em
                        },
         branch/.style = {
                          sum,
                          minimum size = 1mm,
                          fill = black
                         }
        }

\newcommand{\mygraph}{%
    \begin{scope}[auto]
    %placing the nodes
    \node[signal] (input) {};
    \node[sum, right = of input] (left sum) {};
    \node[block, right = of left sum] (controller) {$G_R$};
    \node[block, right = of controller] (system) {$G_S$};
    %connecting the controller and system to get the coordinates of u, its needed for the placement of the measurement block
    \draw
      [->] (controller) -- node[name = u] {$U$} (system);
    \node[block, above = of system] (dynamic of disturbances) {$G_D$};
    \node[signal, left = of dynamic of disturbances] (disturbances) {};
    \node[sum, right = of system] (right sum) {};
    \node[branch, right = of right sum] (branch) {};
    \node[signal, right = of branch] (output) {};
    \node[sum, below = of branch] (lower sum) {};
    \node[signal, right = of lower sum] (measurement noise) {};
    \node[block] (measurement) at (u |- lower sum) {$G_M$};
    %connecting the nodes
    \draw
      [->] (input) -- node {$W$} (left sum);
    \draw
      [->] (left sum) -- node {$E$} (controller);
    \draw
      [->] (system) -- (right sum);
    \draw
      [->] (disturbances) -- node {$Z$} (dynamic of disturbances);
    \draw
      [->] (dynamic of disturbances) -| (right sum);
    \draw
      (right sum) -- (branch);
    \draw
      [->] (branch) -- node {$Y$} (output);
    \draw
      [->] (branch) -- (lower sum);
    \draw
      [->] (measurement noise) -- node[above] {$M$} (lower sum);
    \draw
      [->] (lower sum) -- (measurement);
    \draw
      [->] (measurement) -| node[pos = .95] {$-$} (left sum);
\end{scope}}

在你的主文件中:

\documentclass{scrartcl}
\input{iii}

\begin{document}

\begin{tikzpicture}[auto]
    \mygraph
    %% add things
    \draw[red] (measurement) circle[radius=2cm];
\end{tikzpicture}

\end{document}

得到预期结果:

在此处输入图片描述

相关内容