0

I can't get tikz to show the whole image created by using \showbox. I think it is a node issue and have tried using a scope and using local bounding box but that doesn't work either. I have posted the code below as well.

For the record I started using the answers provided in the following post to get to where I am now. How to get consistent positioning for a node with \usebox containing a \tikzpicture.

For the record I am attempting to do this because I need to use the image on a chain, but draw different lines on top of it as well. but first need to be able to repeat this multiple times. Plus I may be using this same image in other tikz pictures/drawings as well.

CODE

\documentclass[tikz,border=0pt]{standalone}
% Helpful resources
% https://tex.stackexchange.com/questions/37823/how-to-get-consistent-positioning-for-a-node-with-usebox-containing-a-tikzpict

% general packages or settings

\usetikzlibrary{calc,positioning,arrows,scopes,shapes,chains,matrix,fit,quotes,decorations.pathmorphing,backgrounds,patterns,shadings}



\tikzset{
    %general shapes
    unburn/.style={circle, draw=black,minimum size=0.2cm},
    burn/.style={circle, draw=black,minimum size=0.2cm,pattern=crosshatch},
    burning/.style={circle, draw=black,minimum size=0.2cm,pattern=dots},
    every join/.style={-latex}
}


%custom nodes
\newcommand{\chamber}[1]{

    \begin{scope}[local bounding box=#1]
        %draw combustion chamber
        %outer coordinates of chamber
        \coordinate (p1) at (0,0);
        \coordinate (p2) at (0,4);
        \coordinate (p3) at ($(p2)+(30:2)$);
        \coordinate (p4) at ($(p3)+(2,0)$);
        \coordinate (p5) at ($(p4)+(-30:2)$);
        \coordinate (p6) at ($(p5)+(0,-4)$);
        
        %additional coordinates for reference
        \coordinate (p7) at ($(p1)!0.5!(p6)$); %middle point at the bottom of the chamber
        \coordinate (p8) at ($(p3)!0.5!(p4)$); %middle point at top of chamber
        
        %draw cylinder
        \draw[very thick] (p1)--(p2)--(p3)--(p4)--(p5)--(p6);
        %draw piston
        \filldraw[fill=gray!60,very thick] (p1)+(0.2,0) rectangle ++($(p6)+(-0.2,1.8)$);
        \filldraw[fill=white,very thick] ($(p7)+(0,0.9)$) circle (0.26);
        
        %draw spark plug
        \filldraw[fill=gray!60] ($(p8)+(-0.12,-0.8)$) rectangle ($(p8)+(0.12,0)$);
        \filldraw[fill=gray!60] ($(p8)+(-0.06,-0.8)$) rectangle ($(p8)+(0.06,-1)$);
        \draw[semithick] ($(p8)+(0.18,0)$) |- ($(p8)+(-0.12,-1.08)$);
        
        
  \end{scope}
}
\newsavebox{\manychamber}
\savebox{\manychamber}{%
    \begin{tikzpicture}
    \chamber{};
    \end{tikzpicture}%
    

}

\begin{document}
    \begin{tikzpicture}%[start chain=going below]
        \pgfmathsetseed{4} %get same random decorations repeated
        \def\ht{4}; %total height of chamber
        \def\cmtopt{28.45274} %cm to pt conversion
        \node at (0,0){\usebox{\manychamber}};

        %\draw[help lines,step=0.05cm,opacity=0.2] (current bounding box.south west) grid (current bounding box.north east);

    \end{tikzpicture}


\end{document}
1

The culprit is the following line:

\def\ht{4}; %total height of chamber

The command \ht is an important TeX command for the height of a box. It is used in various places in LaTeX and TikZ.

To detect this kind of accident, LaTeX provides \newcommand. The equivalent line with \newcommand:

\newcommand*{\ht}{4}

And LaTeX detects the accident and raises an error message:

! LaTeX Error: Command \ht already defined.
               Or name \end... illegal, see p.192 of the manual.

PS: The star form \newcommand* uses \def, \newcommand without star uses \long\def and allows arguments with paragraphs (\par tokens/empty lines). Since the macro does not have arguments, the difference does not matter here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.