这是我上一个问题的后续:使用 vhdl 或其他语言定义框图。
有没有可能把标签放在正方形里?就像这样:
还可以在连接线上写一些文字吗?
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning, arrows.meta}
\newlength{\myheight}
\setlength{\myheight}{2.5cm}
\tikzset{labels/.style={font=\sffamily\scriptsize},
circuit/.style={draw,minimum width=2cm,minimum height=\myheight,very thick,inner sep=1mm,outer sep=0pt,cap=round,font=\sffamily\bfseries},
triangle 45/.tip={Triangle[angle=45:8pt]}
}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[font=\sffamily,>=triangle 45]
\node [circuit] (item) {Comp};
\matrix[
matrix of nodes,
left= of item,
row sep=\myheight/5,
nodes={anchor=east}
] (rightmatr) {
A\\
B\\
};
\matrix[
matrix of nodes,
right= of item,
row sep=\myheight/6,
nodes={anchor=west}
] (leftmatr) {
greater\\
less\\
equal\\
};
\foreach \i in {1,2}
\draw [->] (rightmatr-\i-1) -- (rightmatr-\i-1 -| item.west);
\foreach \i in {1,2,3}
\draw [<-] (leftmatr-\i-1) -- (leftmatr-\i-1 -| item.east);
\end{tikzpicture}
\caption{Entity of Comp}
\end{figure}
\end{document}
答案1
我会使用简单的 tikz 命令,例如:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning, arrows.meta}
\newlength{\myheight}
\setlength{\myheight}{2.5cm}
\tikzset{labels/.style={font=\sffamily\scriptsize},
circuit/.style={draw,minimum width=2cm,minimum height=\myheight,very thick,inner sep=1mm,outer sep=0pt,cap=round,font=\sffamily\bfseries},
triangle 45/.tip={Triangle[angle=45:8pt]}
}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[font=\sffamily,>=triangle 45]
\path[draw,ultra thick] (0,0) node (A){} --(0,4) node (B){}-- (3,4) node (C){} --(3,0) node (D){}--cycle;
\node (cent) at (1.5,2){\large Comp};
\node[right] (AB1) at (0,1){A};
\node[right] (AB2) at (0,3){B};
\node[left] (CD1) at (3,1) {less};
\node[left] (CD2) at (3,3) {greater};
\node[above] (AD) at (1.5,0) {equal};
\node (A1) at (-2,1) {input};
\node (A2) at (-2,3) {input};
\node (C1) at (5,1) {output};
\node (C2) at (5,3) {output};
\node (D1) at (5,-1) {output};
\draw[->] (A1)--(AB1);
\draw[->] (A2)--(AB2);
\draw[->] (CD1)--(C1);
\draw[->] (CD2)--(C2);
\draw[->] (AD)|-(D1);
\end{tikzpicture}
\caption{Entity of Comp}
\end{figure}
\end{document}
输出几乎符合您的要求(抱歉...这台电脑上没有截图)
当然,如果你想多次使用某些命令,你可以自动执行它们
答案2
如果您想保持上一个问题中似乎需要的灵活性:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning, arrows.meta}
\newlength{\myheight}
\setlength{\myheight}{3cm}
\tikzset{labels/.style={font=\sffamily\scriptsize},
description/.style={font=\scriptsize, below, text centered, text width=4em},
circuit/.style={draw,minimum width=2cm,minimum height=\myheight,very thick,inner sep=1mm,outer sep=0pt,cap=round,font=\sffamily\bfseries}, triangle 45/.tip={Triangle[angle=45:8pt]} }
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[font=\sffamily,>=triangle 45]
\node [circuit] (item) {Comp};
\matrix[
matrix of nodes,
nodes in empty cells,
left=7em of item, row sep=\myheight/6, nodes={anchor=east} ] (leftmatr)
{input\\ \\ input\\ };
\matrix[
nodes in empty cells,
matrix of nodes,
right=7em of item, row sep=\myheight/6, nodes={anchor=west} ] (rightmatr) { output\\ \\ output \\};
\foreach \i/\mylab\mydescr in {1/A/{some text \\ for A},
3/B/{some text \\ for B}}
{\draw [->] (leftmatr-\i-1) -- (leftmatr-\i-1 -| item.west)
node[right, labels] {\mylab}
node[midway,description] {\mydescr};}
\foreach \i/\mylab\mydescr in {1/greater/{some text \\ for greater},
3/less/{some text \\ for less}}
{\draw [<-] (rightmatr-\i-1) -- (rightmatr-\i-1 -| item.east)
node[left, labels] {\mylab}
node[midway,description] {\mydescr};}
\node[below=3ex of rightmatr] (out3) {output};
\draw[<-] (out3) -| (item.south) node[above, labels] {equal}
node[near start,description] {some text \\ for equal};
\end{tikzpicture}
\caption{Entity of Comp}
\end{figure}
\end{document}
如果您希望文本位于行上方而不是行下方,只需在样式中放入above
而不是:below
description
description/.style={font=\scriptsize, above, text centered, text width=4em},
你会得到这个:
此外,我强烈建议阅读此处的第 3 部分:DuckBoat——来自 TeX.SE 的新闻:提出有效的问题。
答案3
此类节点可以用简单的矩形节点绘制,数量不限labels
。无需矩阵。
在下面的代码中,我使用quotes
库来定义标签。由于它们是named
节点,因此您以后可以将其用作箭头的参考。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning, arrows.meta, quotes}
\newlength{\myheight}
\setlength{\myheight}{2.5cm}
\tikzset{labels/.style={font=\sffamily\scriptsize},
circuit/.style={draw,
minimum width=2cm,
minimum height=2.5cm,
very thick,
inner sep=1mm,
outer sep=0pt,
font=\sffamily\bfseries},
triangle 45/.tip={Triangle[angle=45:8pt]}
}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[font=\sffamily,>=triangle 45]
\node[circuit,
"north west:A" {anchor=west, labels, yshift=-5mm, name=A},
"north east:greater" {anchor=east, labels, yshift=-5mm, name=greater},
"south west:B" {anchor=west, labels, yshift=5mm, name=B},
"south east:less" {anchor=east, labels, yshift=5mm, name=less},
"south:equal" {anchor=south, labels, name=equal}] (comp) {Comp};
\draw[<-] (A.west)--node[above]{A}++(180:1cm) node[left]{input};
\draw[<-] (B.west)--node[above]{B}++(180:1cm) node[left]{input};
\draw[->] (greater.east)--node[above]{C}++(0:1cm) node (out) [right]{output};
\draw[->] (less.east)--node[above]{D}++(0:1cm) node[right]{output};
\draw[->] (equal.south)--++(-90:5mm) coordinate(aux) -- node[below]{E} (aux-|out.west) node[right]{output};
\end{tikzpicture}
\caption{Entity of Comp}
\end{figure}
\end{document}