TransWikia.com

TikZ Graph with usebox commands not working

TeX - LaTeX Asked on January 28, 2021

I would like to connect "components", which I create with TikZ and store as a savebox to later incorporate into the final drawing. When using the graph command, the output is not what I expect. I though the top and bottom images would appear identical (barring the alignment, which your comments are welcome on explaining). Basically, the graph stops drawing after a middle blank node. Additionally, setting below=of lbus does not align as expected

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}

enter image description here

One Answer

This is an extended comment. When you say that something is "not working" you benefit a lot from indicating what the expected result is. The saveboxes behave no different from ordinary edge labels: if you place two identical ones in your setup, the second one will be swallowed.

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}

enter image description here

This is because, in your setting, the label also serves as an identifier. So the output of save boxes matches expectations.

By the same token, I do not understand your concerns regarding below=of lbus because there is no indication what the expected result is, and how the actual result differs from that.

Correct answer by user230294 on January 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP