如何改善一系列 TiKZ 节点中的布局

如何改善一系列 TiKZ 节点中的布局

我是 LaTeX 和 TikZ 的初学者,正在尝试制作一份包含一系列键控模式的工业文档。我已设法在附加的 MWE 中生成所需的内容,但我必须手动调整每个半圆在相应六边形中的位置。我想知道是否有更合理的方法来做到这一点……(即将半圆的中心与六边形的中心对齐)。由于我还有许多其他配置要展示,其中每个半圆要放置在不同的角度,我尝试使用 \foreach 循环,但找不到可行的方法……我将不胜感激任何帮助改进代码的帮助。

\documentclass{standalone} 
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz} % Enabling watermarks and special graphics
\usetikzlibrary{positioning,chains, matrix,shapes,decorations,backgrounds,}
\begin{document}
\begin{tikzpicture}[every node/.style={on grid, regular polygon, regular polygon sides=6,  minimum width=10mm, minimum height=10 mm, draw, very thick, node distance=15mm, anchor= west},]
\begin{scope}
\node[draw= white, rectangle, text width=18em,  ] (0) {\textbf{Coding pattern for plug}};
\node[right of=0, xshift= 4 em,   label=above:1 ] (1) {}; 
\node [right of=0,  xshift= 4 em,   circle, draw, minimum size=7.8mm] {};
\node [right of=0, xshift= 4 em, yshift =1ex, semicircle, fill= black,  minimum size=3.8mm,] {};
\node[right of=1, label=above:2] (2) {}; 
\node [right of=1,  circle, draw, minimum size=7.8mm] {};
\node [right of=1,  semicircle, fill= black, xshift= 0.9ex, yshift= 0.5ex,  minimum size=3.8mm, rotate=-60] {};
\node [right of=1,  circle, draw, minimum size=7.8mm] {};
\node[right of=2, label=above:3] (3) {};
\node [right of=2,  circle, draw, minimum size=7.8mm] {};
\node [right of=2,  semicircle, fill= black, xshift= 0.7ex, yshift =-0.4ex,  minimum size=3.8mm, rotate=-120] {};
\end{scope} 
\begin{scope}[yshift=2cm]
\node[draw= white, rectangle,text width=18em ] (0) {\textbf{Coding pattern for receptacle}};
\node[right of=0, xshift= 4 em,   label=above:4 ] (4) {}; 
\node [right of=0, xshift= 4 em,  yshift=-1ex, semicircle, fill= black,  minimum size=3.8mm, rotate=180] {};
\node [right of=0, xshift= 4 em,  circle, draw, minimum size=7.8mm] {};
\node[right of=4, label=above:3] (3) {}; 
\node [right of=4,  semicircle, fill= black,xshift=-1ex, yshift= -0.5ex, minimum size=3.8mm, rotate=120] {};
\node [right of=4,  circle, draw, minimum size=7.8mm] {};
\node[right of=3, label=above:2] (2) {};
\node [right of=3,  semicircle, fill= black, xshift=-0.7 ex, yshift=0.4ex,minimum size=3.8mm, rotate=60] {};
\node [right of=3,  circle, draw, minimum size=7.8mm] {};
\end{scope}
\end{tikzpicture} 

\end{document}

答案1

我引入了一条新命令:\plug[<opt>]{<internal name>}{<rotation>}{<label>}

  • <opt>可以是传递给圆​​的任意 TikZ 内容。
    六边形将位于圆的中心以及半圆的中心。
    (注意anchor=south!这实际上将南边(半圆直径的中间)置于(c.center)。)
  • <internal name>需要定位以下插头。
    谨防,您必须使用plug<internal name>
  • <rotation>
  • <label>在插头上方放置一个节点。如果您(在用户群中)希望更好地放置这些标签,您可以替换

    label={[yshift=1mm]above:#4}
    

    label={#4}
    

    对于一次性覆盖,不要使用<label>,但使用可选参数:

    \plug[…, label={below:5}]{05}{-60}{}
    

代码

\documentclass{standalone} 
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz} % Enabling watermarks and special graphics
\usetikzlibrary{positioning,chains, matrix,shapes,decorations,backgrounds}

\newcommand*\plug[4][]{%
    \node[draw, very thick, circle,          label={[yshift=1mm]above:#4},    minimum size=7.8mm,                                           #1]                    (plug#2) {};
    \node[draw, very thick, regular polygon, anchor=center,                   minimum width=10mm, minimum height=10mm, regular polygon sides=6] at (plug#2.center)          {};
    \node[draw, very thick, semicircle,      anchor=south,        fill=black, minimum size=3.5mm,                                    rotate=#3] at (plug#2.center)          {};
}
\begin{document}
\begin{tikzpicture}[node distance=15mm]
\node[align=left, text width=18em] (text0) {\textbf{Coding pattern for plug}};
\plug[right of=text0,node distance=40mm]{04}{0}{1}
\plug[right of=plug04]{03}{-60}{2}
\plug[right of=plug03]{02}{-120}{3}

\node[above of=text0, node distance=2cm, align=left, text width=18em] (text1) {\textbf{Coding pattern for receptacle}};
\plug[right of=text1, node distance=40mm]{11}{180}{1}
\plug[right of=plug11]{12}{120}{2}
\plug[right of=plug12]{13}{60}{3}
\end{tikzpicture}
\end{document}

输出

像这样吗?

相关内容