Tikz 气缸对齐和配合

Tikz 气缸对齐和配合

我尝试将一个圆柱体安装到一个块和另一个圆柱体的高度,如下面的代码所示。圆柱体 2 应分别精确地安装到块 1 的顶部边缘和圆柱体 1 的底部边缘。当我仅使用块而不是两个圆柱体时,它可以按预期工作。图片中的红线表示圆柱体 2 的所需边界。

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,fit}

\tikzstyle{b}=[draw, minimum height=2.5em]
\tikzstyle{c}=[draw, minimum height=5em, minimum width=4em, cylinder, shape border rotate=90, shape aspect=0.1]

\begin{document}
\begin{tikzpicture}
    \node (block1) [b, anchor=north] {Block 1};
    \node (cylinder1) [c, below=of block1] {Cylinder 1};

    \node (cylinder2) [c, right=3cm of block1.north east,
    anchor=north, fit=(block1)(cylinder1)] {Cylinder 2};
\end{tikzpicture}
\end{document}

minimal example

答案1

fit总是试图适应其内容,因此形状总是更大。您可以更改yscale并进行一些其他设置,以便获得您想要的结果。虽然丑陋且手动,但它确实做到了。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,fit,calc}

\tikzstyle{b}=[draw, minimum height=2.5em]
\tikzstyle{c}=[draw, minimum height=5em, minimum width=4em, cylinder, shape border rotate=90, shape aspect=0.1]

\begin{document}
\begin{tikzpicture}
    \node (block1) [b, anchor=north,outer sep=0pt] at (0,0) {Block 1};
    \node (cylinder1) [c, below= 2cm of block1,outer sep=0pt] {Cylinder 1};

    \node (cylinder2) [c, right=3cm of block1.east,
    anchor=north, fit=(block1)(cylinder1),inner sep=0pt,outer sep=0pt,yscale=0.942,yshift=-0.38ex] {Cylinder 2};
\end{tikzpicture}
\end{document}

enter image description here

相关内容