我正在尝试通过以下方式绘制简单的框图:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[-,auto,node distance=1.25cm]
\tikzstyle{point}=[coordinate]
\tikzstyle{block}=[draw, rectangle, minimum height=1em, minimum width=1.5em]
\node[point] (0) {};
\node[point] (1) [right of=0 ] {};
\node[block] (2) [above right of=1] {A};
\node[block] (3) [right of=2] {B};
\node[block] (4) [right of=3] {C};
\node[block] (7) [right of=1] {A};
\node[block] (8) [right of=7] {E};
\node[block] (9) [right of=8] {D};
\node[block] (12) [below right of=1] {B};
\node[block] (13) [right of=12] {C};
\node[block] (14) [right of=13] {E};
\node[point] (15) [right of=4] {};
\node[point] (16) [right of=9] {};
\node[point] (17) [right of=14] {};
\node[point] (18) [right of=16] {};
\draw [thick] (12) -| (1) (7) -| (1) (2) -| (1) ;
\draw [thick] (0) -- (1) (2) -- (3) (7) -- (8) (3) -- (4) (8) -- (9) (12) -- (13);
\draw [thick] (13) -- (14) ;
\draw [thick] (15) -- (4) ;
\draw [thick] (16) -- (9) ;
\draw [thick] (17) -- (14) ;
\draw [thick] (18) -- (16) ;
\draw [thick] (15) -| (16) (17) -| (16);
\end{tikzpicture}
\end{document}
得出:
我遇到的问题是框之间的正确对齐和图表的一般对称性。也就是说,我希望左侧和右侧的线条长度相同。虽然我只是偶尔使用 Latex 的用户,但我希望得到提示以及一些替代的、也许更简洁的方法来绘制这样的图表。
答案1
您是指“对称性”之类的东西吗?
借助库chains
(用于更短的代码)和positioning
(用于更简单地定位节点),您的图像代码可以重写为:
\documentclass[margin=3mm]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,
positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 4mm and 4mm,
start chain = going right,
N/.style = {draw, thin, minimum height=1em, minimum width=1.5em},
every path/.append style = {semithick}
]
\scoped[nodes={N, on chain, join=by -}]
{
\coordinate (0);
\coordinate (1);
\node (7) {A};
\node (8) {E};
\node (9) {D};
\coordinate (15);
\coordinate (16);
}
\node (2) [N,above=of 7] {A};
\node (3) [N,above=of 8] {B};
\node (4) [N,above=of 9] {C};
%
\node (12) [N,below=of 7] {B};
\node (13) [N,below=of 8] {C};
\node (14) [N,below=of 9] {E};
\draw[red, densely dashed, very thin]
(2.north west) -- (14.south east)
(12.south west) -- (4.north east);
\draw (1) |- (2) -- (3) -- (4) -| (15)
(1) |- (12) -- (13) -- (14) -| (15);
\end{tikzpicture}
\end{document}
答案2
使用 TikZ 的解决方案nicematrix
。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\pgfset{nicematrix/cell-node/.append style = { minimum size = 6 mm } }
\begin{NiceTabular}{ccc}[pgf-node-code=\pgfusepathqstroke]
A & B & C \\[4mm]
A & E & D \\[4mm]
B & C & E
\CodeAfter
\begin{tikzpicture}
\draw (1-1) -- (1-2) (1-2) -- (1-3) ;
\draw (2-1) -- (2-2) (2-2) -- (2-3) ;
\draw (3-1) -- (3-2) (3-2) -- (3-3) ;
\draw (1-1) -- ++(-6mm,0pt) |- (3-1) ;
\draw (1-3) -- ++(6mm,0pt) |- (3-3) ;
\draw (2-1) -- ++(-8mm,0pt) (2-3) -- ++(8mm,0pt) ;
\end{tikzpicture}
\end{NiceTabular}
\end{document}