有人知道如何绘制两个连接在一起的 MIMO 块吗?我正在做一个项目,需要使用 LATEX 绘制一个类似于下面的示意图。有人知道怎么做吗? 具体来说,我想知道如何在块内放置文本。我还想知道如何创建这两个块以及如何正确连接它们。以下是我目前所拥有的。
\documentclass{article}
\usepackage{amsfonts}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage[margin=0.5in]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows, automata, backgrounds, fit, matrix, arrows.meta, calc, positioning}
\begin{document}
\begin{tikzpicture}[>=stealth]
\matrix[matrix of math nodes, nodes in empty cells, draw] (blockA) at (0,2)
{
A & & & F \\
B & & & G \\
C & & & H \\
D & & & I \\
& & & & E \\
};
\matrix[matrix of math nodes,nodes in empty cells,draw,minimum width=30pt,below=of blockA] (blockB)
{
A & & & F \\
B & & & G \\
C & & & H \\
D & & & I \\
E & & & J \\
};
\node at (blockB) {$Block B$};
\foreach \i in {1,2,3,4,5}
{
\draw[->] let \p1 = (blockA.west), \p2 = (blockA-\i-\i.west), \p3 = (blockB.west), \p4 = (blockB-\i-1.west) in (\x3,\y4) -|
++(-2.4+0.2*\i,1) |- (\x1,\y2);
\draw[->] let \p1 = (blockA.east), \p2 = (blockA-\i-\i.east), \p3 = (blockB.east), \p4 = (blockB-\i-1.east) in (\x1,\y2) -|
++(1.6-0.2*\i,-1) |- (\x3,\y4);
}
\end{tikzpicture}
\end{document}
答案1
为了使其对称,我使用绝对坐标作为中点。唯一棘手的是找到顶部块下方和底部块上方的 y 值。
由于行数为奇数,我只需将文本添加为矩阵的一部分即可。如果行数为偶数,则可以\hphantom{\text{Block A}}
像以前一样使用并覆盖节点来保留空间。
\documentclass{article}
\usepackage{amsfonts}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage[margin=0.5in]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows, automata, backgrounds, fit, matrix, arrows.meta, calc, positioning}
\begin{document}
\begin{tikzpicture}[>=stealth]
\matrix[matrix of math nodes, nodes in empty cells, draw] (blockA) at (0,2)
{
A & & F \\
B & & G \\
C & \text{Block A} & H \\
D & & I \\
E & & J \\
};
\matrix[matrix of math nodes,nodes in empty cells,draw,below=of blockA] (blockB)
{
A & & F \\
B & & G \\
C & \text{Block B} & H \\
D & & I \\
E & & J \\
};
\foreach \i in {1,2,3,4,5}
{
\draw[->] let \p1 = (blockA.west), \p2 = (blockA-\i-1.west), \p3 = (blockB.west), \p4 = (blockB-\i-1.west) in (\x3,\y4) -|
(-4+0.2*\i,0) |- (\x1,\y2);
\draw[->] let \p1 = (blockA.east), \p2 = (blockA-\i-1.east), \p3 = (blockB.east), \p4 = (blockB-\i-1.east) in (\x1,\y2) -|
(4-0.2*\i,0) |- (\x3,\y4);
}
\end{tikzpicture}
\end{document}
答案2
这是一个解决方案,它定义了一个名为 的新形状block
,可以与任意数量的输入和输出一起使用。我曾经为类似的问题做过一次。优点:
- 任意数量的输入和输出
- 输入/输出之间的可配置空间
- Tikz 锚定在输入/输出处
rectangle
形状与默认形状相同flip
可以选择右侧输入、左侧输出
如果您愿意,我可以详细说明代码,但您也可以直接使用它。
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\makeatletter
\newdimen\block@iospacing
\newdimen\block@height
\newif\if@block@flip
\tikzset{
block/.code={
\pgfkeys{
/block/.cd,
#1
}
\pgfmathsetlengthmacro{\block@height}{%
max(10mm, int(max(\block@inputs,\block@outputs) * \block@iospacing))}
\tikzset{
draw,
align=center,
minimum width = 15mm,
minimum height = \block@height,
shape=block
}
}
}
\pgfkeys{
/block/.is family,
/block/.cd,
inputs/.code={
\pgfmathparse{int(#1)}
\let\block@inputs=\pgfmathresult
},
inputs=1,
outputs/.code={
\pgfmathparse{int(#1)}
\let\block@outputs=\pgfmathresult
},
outputs=1,
io spacing/.code=\setlength\block@iospacing{#1},
io spacing=5mm,
flip/.is choice,
flip/true/.code={\@block@fliptrue\def\@block@flipbool{1}},
flip/false/.code={\@block@flipfalse\def\@block@flipbool{0}},
flip/.default=true,
flip=false,
}
\pgfdeclareshape{block}{
\inheritsavedanchors[from={rectangle}]
\savedanchor\centerpoint{%
\pgf@x=.5\wd\pgfnodeparttextbox%
\pgf@y=.5\ht\pgfnodeparttextbox%
\advance\pgf@y by-.5\dp\pgfnodeparttextbox%
}
\inheritsavedanchors[from=rectangle]
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{north west}
\inheritanchor[from=rectangle]{north east}
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{text}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
\inheritanchor[from=rectangle]{mid}
\inheritanchor[from=rectangle]{mid west}
\inheritanchor[from=rectangle]{mid east}
\inheritanchor[from=rectangle]{base}
\inheritanchor[from=rectangle]{base west}
\inheritanchor[from=rectangle]{base east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{south east}
\savedmacro\blockinputs{%
\pgfmathparse{int(\block@inputs)}%
\let\blockinputs=\pgfmathresult}%
\savedmacro\blockoutputs{%
\pgfmathparse{int(\block@outputs)}%
\let\blockoutputs=\pgfmathresult}%
\savedmacro\blockmaxio{%
\pgfmathparse{int(max(\block@inputs,\block@outputs))}%
\let\blockmaxio=\pgfmathresult}%
\savedmacro\blockflip{%
\pgfmathparse{\@block@flipbool}%
\let\blockflip=\pgfmathresult}%
\saveddimen\halfwidth{\pgfmathsetlength\pgf@x{%
\pgfkeysvalueof{/pgf/minimum width}/2}\pgfmathresult}
\saveddimen\halfheight{\pgfmathsetlength\pgf@x{%
\pgfkeysvalueof{/pgf/minimum height}/2}\pgfmathresult}
\saveddimen\iospacing{\pgfmathsetlength\pgf@x{%
\block@iospacing}\pgfmathresult}
\inheritbackgroundpath[from={rectangle}]
\pgfutil@g@addto@macro\pgf@sh@s@block{%
\pgfmathloop%
\ifnum\pgfmathcounter>\blockinputs\relax%
\else%
\pgfutil@ifundefined{pgf@anchor@block@input \pgfmathcounter}{%
\expandafter\xdef\csname pgf@anchor@block@input %
\pgfmathcounter\endcsname{\noexpand%
\pgf@sh@lib@block@in@anchor{\pgfmathcounter}%
}%
}{}
\repeatpgfmathloop%
\pgfmathloop%
\ifnum\pgfmathcounter>\blockoutputs\relax%
\else%
\pgfutil@ifundefined{pgf@anchor@block@output \pgfmathcounter}{%
\expandafter\xdef\csname pgf@anchor@block@output %
\pgfmathcounter\endcsname{\noexpand%
\pgf@sh@lib@block@out@anchor{\pgfmathcounter}%
}%
}{}
\repeatpgfmathloop%
}%
}
\def\pgf@sh@lib@block@in@anchor#1{%
\pgf@process{\centerpoint}%
\pgf@ya=\pgf@y%
\ifnum\blockflip=0\relax%
\pgf@process{\southwest}%
\else%
\pgf@process{\northeast}%
\fi%
\pgfmathsetlength\pgf@y{\pgf@ya + (0.5*(\blockinputs+1)-#1)*\iospacing}
}
\def\pgf@sh@lib@block@out@anchor#1{%
\pgf@process{\centerpoint}%
\pgf@ya=\pgf@y%
\ifnum\blockflip=0\relax%
\pgf@process{\northeast}%
\else%
\pgf@process{\southwest}%
\fi%
\pgfmathsetlength\pgf@y{\pgf@ya + (0.5*(\blockoutputs+1)-#1)*\iospacing}
}
\makeatother
\begin{document}
\begin{tikzpicture}[block/.append style={minimum width=3cm},iolabel/.style={text width=2.5mm,align=center}]
\node[block={inputs=4,outputs=5}](blockA){Block A};
\node[block={inputs=5,outputs=4,flip,io spacing=8mm},below=of blockA](blockB){Block B};
\foreach[count = \i] \letter in {F,...,J}{
\node[left,iolabel] at (blockA.output \i) {\letter};
\node[left,iolabel] at (blockB.input \i) {\letter};
\draw[->] (blockA.output \i) -- ++(2-\i*0.2,0) |- (blockB.input \i);
}
\foreach[count = \i] \letter in {A,...,D}{
\node[right,iolabel] at (blockA.input \i) {\letter};
\node[right,iolabel] at (blockB.output \i) {\letter};
\draw[->] (blockB.output \i) -- ++(-2+\i*0.2,0) |- (blockA.input \i);
}
\end{tikzpicture}
\end{document}
该代码的结果是: