我正在尝试编写有关逻辑门的概述。我阅读了手册并研究了大量示例。然而,我未能取得令人满意的结果。
这是我当前的代码:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{circuits.logic.IEC}
\usepackage{amsopn}
\DeclareMathOperator{\AND}{\wedge}
\DeclareMathOperator{\OR}{\vee}
\DeclareMathOperator{\XOR}{\underline{\vee}}
\DeclareMathOperator{\NOT}{\neg}
\begin{document}
Davor
\begin{tikzpicture}[circuit logic IEC]
\draw [help lines] grid (12,7);
% Gates
\node at (1, 6) [buffer gate] (g11) {};
\node at (8, 6) [not gate] (g12) {};
\node at (1, 4) [and gate] (g21) {};
\node at (8, 4) [nand gate] (g22) {};
\node at (1, 2) [or gate] (g31) {};
\node at (8, 2) [nor gate] (g32) {};
\node at (1, 0) [xor gate] (g41) {};
\node at (8, 0) [xnor gate] (g42) {};
% Inputs
\node at ([xshift=-1cm] g11.input) (g11-a) {$A$}; \draw (g11.input) -- (g11-a);
\node at ([xshift=-1cm] g12.input) (g12-a) {$A$}; \draw (g12.input) -- (g12-a);
\foreach \gate in {g21, g22, g31, g32, g41, g42}
{
\node at ([xshift=-1cm] \gate.input 1) (\gate-a) {$A$}; \draw (\gate.input 1) -- (\gate-a);
\node at ([xshift=-1cm] \gate.input 2) (\gate-b) {$B$}; \draw (\gate.input 2) -- (\gate-b);
};
% Outputs
\node at ([xshift=1.7cm] g11.output) (g11-y) {$Y = A$}; \draw (g11.output) -- (g11-y);
\node at ([xshift=1.7cm] g12.output) (g12-y) {$Y = \NOT A$}; \draw (g12.output) -- (g12-y);
\node at ([xshift=1.7cm] g21.output) (g21-y) {$Y = A \AND B$}; \draw (g21.output) -- (g21-y);
\node at ([xshift=1.7cm] g22.output) (g22-y) {$Y = \NOT \left( A \AND B \right)$}; \draw (g22.output) -- (g22-y);
\node at ([xshift=1.7cm] g31.output) (g31-y) {$Y = A \OR B$}; \draw (g31.output) -- (g31-y);
\node at ([xshift=1.7cm] g32.output) (g32-y) {$Y = \NOT \left( A \OR B \right)$}; \draw (g32.output) -- (g32-y);
\node at ([xshift=1.7cm] g41.output) (g41-y) {$Y = A \XOR B$}; \draw (g41.output) -- (g41-y);
\node at ([xshift=1.7cm] g42.output) (g42-y) {$Y = \NOT \left( A \XOR B \right)$}; \draw (g42.output) -- (g42-y);
\end{tikzpicture}
Danach
\end{document}
我想改进的是
- “Y =”标签的对齐方式;
- 在 xshift 中使用相对单位而不是厘米;
- “A”和“B”标签相对于周围文本的对齐方式。
答案1
对于问题 1 和 3,改变事物的绘制方式。首先绘制线,然后放置节点,如
\draw[shorten >= 3pt] (g11.input) -- +(-1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=east] (g11-a) {$A$};
inner xsep
对于问题 3,也将和都设为outer sep
零。此外,您可以缩短线以在A
s 和B
s 和线之间留出一些间隙。对于 2,使用
positioning
库和right = of
和below = of
语法。
有了这些,你的代码将是,
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{circuits.logic.IEC,positioning}
\usepackage{amsopn}
\DeclareMathOperator{\AND}{\wedge}
\DeclareMathOperator{\OR}{\vee}
\DeclareMathOperator{\XOR}{\underline{\vee}}
\DeclareMathOperator{\NOT}{\neg}
\begin{document}
Davor
\begin{tikzpicture}[circuit logic IEC]
\draw [help lines] grid (12,7);
% Gates
\node at (1, 6) [buffer gate] (g11) {};
\node[right = 6cm of g11] [not gate] (g12) {};
\node [below = of g11] [and gate] (g21) {};
\node [below = of g12] [nand gate] (g22) {};
\node [below = of g21] [or gate] (g31) {};
\node [below = of g22] [nor gate] (g32) {};
\node [below = of g31] [xor gate] (g41) {};
\node [below = of g32] [xnor gate] (g42) {};
% Inputs
\draw[shorten >= 3pt] (g11.input) -- +(-1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=east] (g11-a) {$A$};
\draw[shorten >= 3pt] (g12.input) -- +(-1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=east] (g12-a) {$A$};
\foreach \gate in {g21, g22, g31, g32, g41, g42}
{
\draw[shorten >= 3pt] (\gate.input 1) -- +(-1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=east] (\gate-a) {$A$};
\draw[shorten >= 3pt] (\gate.input 2) -- +(-1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=east] (\gate-b) {$B$};
};
% Outputs
\draw[shorten >= 3pt] (g11.output) -- +(1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=west] (g11-y) {$Y = A$};
\draw[shorten >= 3pt] (g12.output) -- +(1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=west] (g12-y) {$Y = \NOT A$};
\draw[shorten >= 3pt] (g21.output) -- +(1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=west] (g21-y) {$Y = A \AND B$};
\draw[shorten >= 3pt] (g22.output) -- +(1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=west] (g22-y) {$Y = \NOT \left( A \AND B \right)$};
\draw[shorten >= 3pt] (g31.output) -- +(1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=west] (g31-y) {$Y = A \OR B$};
\draw[shorten >= 3pt] (g32.output) -- +(1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=west] (g32-y) {$Y = \NOT \left( A \OR B \right)$};
\draw[shorten >= 3pt] (g41.output) -- +(1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=west] (g41-y) {$Y = A \XOR B$};
\draw[shorten >= 3pt] (g42.output) -- +(1cm,0) node[outer sep=0pt,inner xsep=0pt,anchor=west] (g42-y) {$Y = \NOT \left( A \XOR B \right)$};
\end{tikzpicture}
Danach
\end{document}