我是 LaTeX 和 TikZ 的初学者,正在尝试制作工业文档,其中包含一系列接线端子,其接线方式在 MWE 中显示。我必须制作这些图纸的系列。外壳相同,但引脚的大小、行数和行间距会发生变化。我试图将有用的参数放在宏中,以便轻松生成每个配置。但是我无法解决最后一步,即自动计算 foreach 语句中的最后几个数字,例如数字 12,即 #3-1,这是包含偶数的行的腔体数量。我找不到一种方法来计算每行的最后几个引脚的数量,即 25、38、50,这些引脚是开始对下一行进行编号所需的。我也怀疑此代码是否已优化,欢迎任何改进它的提示。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\newcommand{\DrawTerminalBlock}[5]{
\begin{tikzpicture}[scale=#1, thick,
mynode/.style={circle, draw, very thin, minimum size=2.6mm, text centered,font=\tiny, inner sep=0.5pt},
]
\coordinate (O) at (0,0) ;
\coordinate (A) at (1.495,0) ;
\coordinate (B) at (1.495, 0.684) ;
\coordinate (C) at (1.309,.870) ;
\coordinate (D) at (0.208,0.870) ;
\coordinate (E) at (0,0.684) ;
\draw (O) --(A) --(B)--( C)-- (D) --(E) --(O)--cycle; %Draw outline
% #1 Scale
% #2 Horizontal spacing between cavities
% #3 Number of horizontal cavities long rows (odd number of cavities)
% #4 Vertical distance to lower (bottom) row
% #5 Vertical spacing between rows
\foreach \x in {1,...,#3}
{
\node[mynode,] at (#2*\x, #4) {\x};
}
\foreach \x in {1,...,12}
{\pgfmathparse{int(#3+\x)}
\edef\n{\pgfmathresult}
\node[mynode,] at (0.5*#2+#2*\x,#4+#5) {\n};
}
\foreach \x in {1,...,#3}
{\pgfmathparse{int(25+\x)}
\edef\n{\pgfmathresult}
\node[mynode,] at (#2*\x,#4+2*#5) {\n};
}
\foreach \x in {1,...,12}
{\pgfmathparse{int(38+\x)}
\edef\n{\pgfmathresult}
\node[mynode,] at (0.5*#2+#2*\x,#4+3*#5) {\n};
}
\foreach \x in {1,...,#3}
{\pgfmathparse{int(50+\x)}
\edef\n{\pgfmathresult}
\node[mynode,] at (#2*\x,#4+4*#5) {\n};
}
\foreach \x in {1,...,12}
{\pgfmathparse{int(63+\x)}
\edef\n{\pgfmathresult}
\node[mynode,] at (0.5*#2+#2*\x,#4+5*#5) {\n};
}
\end{tikzpicture}
}
\DrawTerminalBlock{4}{.108}{13}{0.1}{0.1}
\end{document}
答案1
这是解决方案“始终为 6 行”。由于找到正确的值是一项繁琐的任务,我自动计算它,因此该命令现在有 4 个参数。对于位置,#2
您只需要一点div
和mod
“如果不是的话”(? :
)。
代码
\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
\parindent0mm
\newcommand{\DrawTerminalBlock}[4]{
\begin{tikzpicture}[scale=#1, thick,node distance=#1in,
mynode/.style={circle, draw, very thin, minimum size=3.5mm, text centered,font=\tiny, inner sep=0.5pt},
]
\coordinate (O) at (0,0) ;
\coordinate (A) at (1.495,0) ;
\coordinate (B) at (1.495, 0.684) ;
\coordinate (C) at (1.309,.870) ;
\coordinate (D) at (0.208,0.870) ;
\coordinate (E) at (0,0.684) ;
\draw (O) --(A) --(B)--( C)-- (D) --(E) --(O)--cycle; %Draw outline
% #1 Scale
% (Horizontal spacing between cavities)
\pgfmathsetmacro{\hsbc}{1.495/(#2+1)}
% #2 Number of horizontal cavities long rows (odd number of cavities)
% #3 Vertical distance to lower (bottom) row
% #4 Vertical spacing between rows
\pgfmathtruncatemacro{\maxval}{6*#2-3}
\foreach \x in {1,...,\maxval}
{ \pgfmathtruncatemacro{\rownum}{2*div(\x-1,2*#2-1)+div(mod(\x-1,2*#2-1),#2)+1}
\pgfmathtruncatemacro{\colnum}{mod(mod(\x-1,2*#2-1),#2)+1}
\pgfmathtruncatemacro{\rowindent}{mod(\rownum,2)==0 ? 1 : 0}
\node[mynode] at ({\hsbc*(\colnum+0.5*\rowindent)},{#3+(\rownum-1)*#4}) {\x};
}
\end{tikzpicture}
}
\DrawTerminalBlock{4}{13}{0.1}{0.1}
\DrawTerminalBlock{4}{5}{0.1}{0.1}\\
\DrawTerminalBlock{8}{19}{0.1}{0.1}
\end{document}
输出
编辑1:现在可以选择行数,并自动计算垂直位置,从而将参数减少到三个:
代码
\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
\parindent0mm
\newcommand{\DrawTerminalBlock}[3]{% #1 Scale, #2 Number of horizontal cavities, #3 Number of rows
\begin{tikzpicture}[scale=#1, thick,node distance=#1in, mynode/.style={circle, draw, very thin, minimum size=3.5mm, text centered,font=\tiny, inner sep=0.5pt}]
\coordinate (O) at (0,0) ;
\coordinate (A) at (1.495,0) ;
\coordinate (B) at (1.495, 0.684) ;
\coordinate (C) at (1.309,.870) ;
\coordinate (D) at (0.208,0.870) ;
\coordinate (E) at (0,0.684) ;
\draw (O) --(A) --(B)--( C)-- (D) --(E) --(O)--cycle; %Draw outline
\pgfmathsetmacro{\hsbc}{1.495/(#2+1)}% (Horizontal spacing between cavities)
\pgfmathsetmacro{\vsbr}{0.684/(#3+1)}% (Vertical spacing between rows)
\pgfmathtruncatemacro{\maxval}{(#3*#2)-div(#3,2)}
\foreach \x in {1,...,\maxval}
{ \pgfmathtruncatemacro{\rownum}{2*div(\x-1,2*#2-1)+div(mod(\x-1,2*#2-1),#2)+1}
\pgfmathtruncatemacro{\colnum}{mod(mod(\x-1,2*#2-1),#2)+1}
\pgfmathtruncatemacro{\rowindent}{mod(\rownum,2)==0 ? 1 : 0}
\node[mynode] at ({\hsbc*(\colnum+0.5*\rowindent)},{\rownum*\vsbr}) {\x};
}
\end{tikzpicture}
}
\DrawTerminalBlock{4}{13}{6}
\DrawTerminalBlock{4}{5}{4}\\
\DrawTerminalBlock{8}{17}{13}
\end{document}