代码

代码

我无法让 tikz 显示使用 \showbox 创建的整个图像。我认为这是一个节点问题,并尝试使用范围和使用本地边界框,但这也不起作用。我也在下面发布了代码。

为了记录,我开始使用以下帖子中提供的答案来达到我现在的状态。如何使用包含 \tikzpicture 的 \usebox 获得节点的一致定位

记录一下,我尝试这样做是因为我需要在链上使用图像,但同时在其上绘制不同的线条。但首先需要能够重复多次。另外,我可能也会在其他 tikz 图片/绘图中使用同一幅图像。

代码

\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

罪魁祸首是下面这一行:

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

该命令\ht是用于控制框高度的重要 TeX 命令。它在 LaTeX 和 TikZ 中被广泛使用。

为了检测这种意外情况,LaTeX 提供了\newcommand。 等效行如下\newcommand

\newcommand*{\ht}{4}

LaTeX 检测到事故并发出错误消息:

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

PS:星号形式\newcommand*使用\def\newcommand不带星号形式使用\long\def,并允许带段落的参数(\par标记/空行)。由于宏没有参数,因此这里的差异并不重要。

相关内容