我对齿轮有以下 MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows,decorations.markings}
% #1 number of teeths
% #2 radius intern
% #3 radius extern
% #4 angle from start to end of the first arc
% #5 angle to decale the second arc from the first
% #6 internal key of the gear node
% #7 text to put inside the gear
\newcommand{\gear}[7]{
node (#6) {#7}
(0:#2)
\foreach \i [evaluate=\i as \n using {\i-1)*360/#1}] in {1,...,#1}{
arc (\n:\n+#4:#2) {[rounded corners=1.5pt] -- (\n+#4+#5:#3)
arc (\n+#4+#5:\n+360/#1-#5:#3)} -- (\n+360/#1:#2)
}
}
\begin{document}
% Define the layers to draw the diagram
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
% Define block styles used later
\tikzstyle{engine} = [fill=red!20, text centered,drop shadow]
\begin{tikzpicture}
\draw[engine] \gear{8}{2}{2.4}{10}{2}{a}{Gear A};
%\draw[engine] (a.east)+(8,0) \gear{8}{2}{2.4}{10}{2}{b}{Gear B};
\begin{pgfonlayer}{background}
\path (a.north west)+(-4,4) node (x) {};
\path (a.south east)+(10,-4) node (y) {};
\path[fill=yellow!10, rounded corners, draw=black!70, dashed] (x) rectangle (y);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
它按预期产生以下输出。
现在,我想要一个以上的齿轮,比如说齿轮 B,位于齿轮 A 的右侧。为此,我尝试取消注释上例中的以下行:
\draw[engine] (a.east)+(8,0) \gear{8}{2}{2.4}{10}{2}{b}{Gear B};
但不幸的是,它产生了类似这样的结果:
我尝试了其他几种方法,但都没有成功。齿轮项目相互叠加。有没有办法将它们绝对定位?
这里是上述示例的在线版本,您可以编辑和使用。
答案1
代替:
\draw[engine] (a.east)+(8,0) \gear{8}{2}{2.4}{10}{2}{b}{Gear B};
看看上述代码的修改是否可以接受:
\draw[engine,xshift=60mm] \gear{8}{2}{2.4}{10}{2}{b}{Gear B};
编辑:
上述方法的替代方法是使用scope
(如 Peter Grill 所建议的):
\begin{scope}[shift={($(a.east)+(5,0)$)}]
\draw[engine] \gear{8}{2}{2.4}{10}{2}{b}{Gear B};
\end{scope}
在这种情况下,您需要添加calc
用于计算shift
宏的库
我认为,与\newcommand
绘制齿轮相比,小图片pic
是更好的选择。
答案2
另一种解决方案(如果您没有pic
s,因为您仍然使用 tikzpgf 2.10),您可以更改的定义以\gear
使用相对坐标(仍然需要calc
添加到 tikz 包中):
\newcommand{\gear}[7]{
node (#6) {#7}
($(#6)+(0:#2)$)
\foreach \i [evaluate=\i as \n using {\i-1)*360/#1}] in {1,...,#1}{
arc (\n:\n+#4:#2) {[rounded corners=1.5pt] -- ($(#6)+ (\n+#4+#5:#3)$)
arc (\n+#4+#5:\n+360/#1-#5:#3)} -- ($(#6)+(\n+360/#1:#2)$)
}
}
(overleaf 中的代码看起来很奇怪,因为它假设这($...$)
是数学模式......)