TikZ Graph 与 \usebox 命令不起作用

TikZ Graph 与 \usebox 命令不起作用

我想连接我用来创建TikZ并存储为的“组件”,以便savebox稍后将其合并到最终绘图中。使用该graph命令时,输出不是我期望的。我认为顶部和底部的图像看起来相同(除非对齐,欢迎您对此发表评论)。基本上,图形在中间空白节点后停止绘制。此外,设置below=of lbus未按预期对齐

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,graphs}

\begin{document}

%%% circuit breaker symbol, I will have other, more complicated \usebox drawings in the final picture
%%% simple example shown for simplicity
\newsavebox{\tikzBRKt}
\savebox{\tikzBRKt}{%
    \begin{tikzpicture}[ultra thick]
    \draw (0,0) rectangle (1,1);
    \end{tikzpicture}%
}
\newcommand{\tikzBRK}{\usebox{\tikzBRKt}}


\begin{tikzpicture}[inner sep = 0pt, outer sep = 0pt,]
    \tikzset{node distance = 1cm and 2cm}
\coordinate (bjunc);
\node (rbrk)     [right= of bjunc] {\tikzBRK};
\node (lbrk)     [left= of bjunc] {\tikzBRK};
\node (lbus)    [left=of lbrk] {};
\node (rbus)    [right=of rbrk] {};
\draw (lbrk) edge (bjunc) edge (rbrk)
      (lbus) edge (lbrk)
      (rbus) edge (rbrk);

    \begin{scope}[yshift=-2cm, below=of lbus] % todo also not sure why below lbus doesnt work here?
        \graph [grow right sep=2cm]{ lbus1/ -- "\tikzBRK" -- bjunc1/ -- "\tikzBRK" -- rbus1/ }; % todo why isnt this working
    \end{scope}

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

这是一条扩展注释。当您说某事“不起作用”时,指出预期结果会对您大有裨益。保存框的行为与普通边缘标签无异:如果您在设置中放置两个相同的保存框,则第二个将被吞噬。

\documentclass{article}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning,graphs}

\begin{document}

%%% circuit breaker symbol, I will have other, more complicated \usebox drawings in the final picture
%%% simple example shown for simplicity
\newsavebox{\tikzBRKt}
\savebox{\tikzBRKt}{%
    \begin{tikzpicture}[ultra thick]
    \draw (0,0) rectangle (1,1);
    \end{tikzpicture}%
}
\newcommand{\tikzBRK}{\usebox{\tikzBRKt}}


\begin{tikzpicture}[inner sep = 0pt, outer sep = 0pt,]
    \tikzset{node distance = 1cm and 2cm}
\coordinate (bjunc);
\node (rbrk)     [right= of bjunc] {\tikzBRK};
\node (lbrk)     [left= of bjunc] {\tikzBRK};
\node (lbus)    [left=of lbrk] {};
\node (rbus)    [right=of rbrk] {};
\draw (lbrk) edge (bjunc) edge (rbrk)
      (lbus) edge (lbrk)
      (rbus) edge (rbrk);


\begin{scope}[yshift=-2cm,local bounding box=A1]
  \graph [grow right sep=2cm]{ lbus1/ -- "A" -- bjunc1/ -- "A" -- rbus1/ }; 
\end{scope}
\path (A1.west) node[left]{``not working''};

\begin{scope}[yshift=-4cm,local bounding box=A2]
  \graph [grow right sep=2cm]{ lbus1/ -- "A" -- bjunc1/ -- "B" -- rbus1/ }; 
\end{scope}
\path (A2.west) node[left]{``working''};


\begin{scope}[yshift=-6cm,local bounding box=A3]
  \graph [grow right sep=2cm]{ lbus1/ -- "\tikzBRK" -- bjunc1/ -- "\tikzBRK" -- rbus1/ }; 
\end{scope}
\path (A3.west) node[left]{``not working''};


\begin{scope}[yshift=-8cm,local bounding box=A4]
  \graph [grow right sep=2cm]{ lbus1/ -- "\tikzBRK" -- bjunc1/ -- "{}\tikzBRK" -- rbus1/ }; 
\end{scope}
\path (A4.west) node[left]{``working''};

\end{tikzpicture}

\end{document}

在此处输入图片描述

这是因为,在您的设置中,标签也充当标识符。因此保存框的输出符合预期。

同样,我不明白您的担忧,below=of lbus因为没有迹象表明预期结果是什么,以及实际结果与预期结果有何不同。

相关内容