如何使用 CircuiTikz 插入 80 个 dipchip?

如何使用 CircuiTikz 插入 80 个 dipchip?

\foreach 非常受限,我怎样才能将三个不同的数字插入到 dipchip 中,x 和 y 位置以及芯片编号?

看到这个问题并回答。

在此处输入图片描述

我被要求分享我的知识,因此我自己写了答案。

答案1

虽然我也做了很多用 Python 编写并输出 LaTeX 的事情(这对于学生从 Excel 生成的反馈表非常有用!),但在这种情况下,我认为直接在 LaTeX 中编写循环更容易,并且可以像 Python 循环一样进行自定义(请注意,您可以为各种、等\def编写宏...)5.511

我编写的这个代码基本上是将 Python 循环直接翻译成 LaTeX:

\documentclass[]{article}
\usepackage[siunitx, RPvoltages]{circuitikz}

%\usepackage{verbatim}
\usepackage{geometry} % to change the page dimensions
\geometry{a3paper} % or letterpaper (US) or a5paper or....
% set the basic length to 0.5in (1 in is very big, but could work)
% SET THIS ONCE and only in the preamble.
\ctikzset{bipoles/length=0.5in}
% pin spacing is in basic lengths, see manual, section 3.27.1, around page 135
\ctikzset{multipoles/dipchip/pin spacing=0.2} %0.1in
\ctikzset{multipoles/external pins width=0.1} %0.05in
\ctikzset{multipoles/dipchip/width=0.8}       %0.4in
\ctikzset{multipoles/dipchip/width=0.7}       %0.35in body + 2*0.025in half pads 
\ctikzset{multipoles/external pad fraction=4} %draw the pad
\begin{document}

\begin{figure}[ht]


% work with units of pin distance
\begin{circuitikz}[x=0.1in, y=0.1in,
    chip16/.style={dipchip, anchor=pin 1, num pins=16, hide numbers, rotate=90},
    chip14/.style={dipchip, anchor=pin 1, num pins=14, hide numbers, rotate=90},
    ]
    \newcounter{chipN}\setcounter{chipN}{38}
    % redefine the labels here
    \newcommand{\mylabel}[1]{\large\ttfamily\rotatebox{-90}{#1}}
    \newcommand{\xstep}{11}
    \newcommand{\ystep}{5.5}
    \foreach \j in {0,...,5} {
        \path (9,{ 2.5+\j*\ystep}) node[chip16]{\mylabel{IC 1.\arabic{chipN}}};
        \path (9,{57.5+\j*\ystep}) node[chip16]{\mylabel{IC 2.\arabic{chipN}}};
        \addtocounter{chipN}{-1}
    }

    \foreach \i in {0,...,3} {
        \pgfmathsetmacro{\y}{52-\ystep*\i}
        \pgfmathsetmacro{\x}{20+\xstep*\i}
        \pgfmathtruncatemacro{\n}{\i+1}
        \path (9, \y) node[chip14]{\mylabel{IC 3.\n}};
        \foreach \j in {0,...,7} {
            \path (\x, {2.5+\j*\ystep}) node[chip16]{\mylabel{IC 1.\arabic{chipN}}};
            \path (\x, {46.5+\j*\ystep}) node[chip16]{\mylabel{IC 2.\arabic{chipN}}};
            \addtocounter{chipN}{-1}
        }
    }


\draw [black,  thin] (0in, 0in) rectangle (160.0mm, 233.4mm); % draw the rectangle for the double euro format board

\end{circuitikz}
\end{figure}
\end{document}

...你的情况也一样(我不得不在你的最后一个循环中切换 IC 1. 和 IC 2.,我认为图像与此相对应)。我还使用了多种东西来进行计算(直接在坐标中,使用各种\pgfmath...东西等)以显示几种可能性。

对于芯片编号,我使用了一个计数器,因为它们在 LaTeX 中是全局的,并且可以节省一些本地组的麻烦。

在此处输入图片描述

答案2

这可以通过结合 LaTex 和 Python 的功能来实现。

LaTex 代码:

\documentclass[border=10pt]{article}
\usepackage[siunitx, RPvoltages]{circuitikz}
% set the basic length to 0.5in (1 in is very big, but could work)
% SET THIS ONCE and only in the preamble.
%\usepackage{verbatim}
\usepackage{geometry} % to change the page dimensions
\geometry{a3paper} % or letterpaper (US) or a5paper or....

\ctikzset{bipoles/length=0.5in}
% pin spacing is in basic lengths, see manual, section 3.27.1, around page 135
\ctikzset{multipoles/dipchip/pin spacing=0.2} %0.1in
\ctikzset{multipoles/external pins width=0.1} %0.05in
\ctikzset{multipoles/dipchip/width=0.8}       %0.4in
\ctikzset{multipoles/dipchip/width=0.7}       %0.35in body + 2*0.025in half pads 
\ctikzset{multipoles/external pad fraction=4} %draw the pad
\begin{document}

\begin{figure}[h]

% work with units of pin distance
\begin{circuitikz}[x=0.1in, y=0.1in]

\input{mountICs.tex} % import the Python generated file with the placement of 80 chips
                    
\draw [black,  thin] (0in, 0in) rectangle (160.0mm, 233.4mm); % draw the rectangle for the double euro format board

\end{circuitikz}

\end{figure}

\end{document}

Python代码:

# define the LaTex strings
# the double backslash \\ is needed in Python to get a single \ for the LaTex string

# first part of the LaTex string
left = '\\path ('

# second part of the LaTex string, two versions: mid1 and mid2
# num pins = 16
mid1 = ") node[dipchip, anchor=pin 1, num pins=16, hide numbers, rotate=90] {\\large\\rotatebox{-90}{"

# num pins = 14
mid2 = ") node[dipchip, anchor=pin 1, num pins=14, hide numbers, rotate=90] {\\large\\rotatebox{-90}{"

# third and last part of the LaTex string
right = "}};\n"

tex = open('mountICs.tex', 'w') # open output file mountICs.tex for writing

n = 38 # number for chip label

for j in range(6) : # 6 chips in a row
   # compose a LaTex string using the left, mid1 and right string constants
   # and write it to the opened .tex file
   # chip label number n is pasted to IC 1.
   y = 2.5 + j * 5.5
   tex.write(left + "9, " + str(y) + mid1 + "IC 1." + str(n) + right)
   y = 57.5 + j * 5.5
   tex.write(left + "9, " + str(y) + mid1 + "IC 2." + str(n) + right)
   n -= 1 # decrement chip label number n

for i in range(4) : # 4 chips in a row
   y = 52 - i * 5.5 # vertical position
   x = 20 + i * 11 # horizontal position
   tex.write(left + "9, " + str(y) + mid2 + "IC 3." + str(i+1) + right)
   for j in range(8) : # 8 chips in a column
       # insert the numbers x, y and n as strings into the LaTex string
       y = 2.5 + j * 5.5 # vertical position
       tex.write(left + str(x) + ", " + str(y) + mid1 + "IC 2." + str(n) + right)
        y = 46.5 + j * 5.5 # vertical position
        tex.write(left + str(x) + ", " + str(y) + mid1 + "IC 1." + str(n) + right)
        n -= 1

tex.close() #close the file and write all to the disk

Python 脚本写入文件 mountICs.tex,使用 \input{} 将该文件插入到 LaTex 代码中。

Python 和 LaTex 应该使用相同的本地目录来交换 mountICs.tex,如果需要使用本地目录以外的目录,则可以添加文件路径。

LaTex 不会向 Python 脚本传递任何参数,如果要更改行和列中的芯片数量,则必须编辑并重新运行 Python 脚本。每次编辑 Python 脚本时,都不会自动构建 mountICs.tex。

Python 脚本易于修改,如果要更改行或列中的芯片数量,只需更改范围 (4) 或范围 (8) 中的数字。如果要更改芯片的距离,只需修改 y = 2.5 + j * 5.5 中的 5.5

相关内容