我有一个 tikz 绘图,基本上有“两列节点”。第二列还可以。我希望每列的节点都居中对齐(如图 2 所示)。但我还希望第一列的节点与第二列的节点垂直对齐(如图 1 所示)。
我如何在 tikz 中进行这种对齐?我尝试使用矩阵,但它似乎不适用于多行文本
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, shapes.arrows, arrows, chains, positioning, automata, backgrounds,patterns,fit,petri,arrows.meta}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[align=center,node distance=1mm and 4cm,every edge/.style={thick,draw}, every node/.style = {font=\small}]
\tikzstyle{a}=[rectangle,draw=black,fill=black!5, rounded corners]
\tikzstyle{dummy}=[circle,draw]
\node(ha1)[a] {See \textit{myers2018anomaly}};
\node(ha2)[a, below=of ha1] {See \textit{yousfi2019discovering} \textit{yousfi2019discovering}\\ \textit{yousfi2019discovering} \textit{yousfi2019discovering} \textit{yousfi2019discovering}};
\node(ha3)[a, below=of ha2] {See \textit{diamantini2017discovering}};
\node(h1)[a, left=of ha1] {Industrial control systems devices} ;
\node(h2)[a, left=of ha2] {Ubiquitous computing devices};
\node(h3)[a, left=of ha3] {Geotags};
\draw (ha1)edge(h1);
\draw (ha2)edge(h2);
\draw (ha3)edge(h3);
\end{tikzpicture}
\caption{side-by-syde is correct, centering is wrong}
\end{figure}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[align=center,node distance=1mm and 4cm,every edge/.style={thick,draw}, every node/.style = {font=\small}]
\tikzstyle{a}=[rectangle,draw=black,fill=black!5, rounded corners]
\tikzstyle{dummy}=[circle,draw]
\node(ha1)[a] {See \textit{myers2018anomaly}};
\node(ha2)[a, below=of ha1] {See \textit{yousfi2019discovering} \textit{yousfi2019discovering}\\ \textit{yousfi2019discovering} \textit{yousfi2019discovering} \textit{yousfi2019discovering}};
\node(ha3)[a, below=of ha2] {See \textit{diamantini2017discovering}};
\node(h1)[a, left=of ha1] {Industrial control systems devices} ;
\node(h2)[a, below=of h1] {Ubiquitous computing devices};
\node(h3)[a, below=of h2] {Geotags};
\draw (ha1)edge(h1);
\draw (ha2)edge(h2);
\draw (ha3)edge(h3);
\end{tikzpicture}
\caption{side-by-syde is wrong, centering is correct}
\end{figure}
\end{document}
答案1
使用|-
路径上的语法来放置节点。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, shapes.arrows, arrows, chains, positioning, automata, backgrounds,patterns,fit,petri,arrows.meta}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[align=center,node distance=1mm and 4cm,every edge/.style={thick,draw}, every node/.style = {font=\small}]
\tikzstyle{a}=[rectangle,draw=black,fill=black!5, rounded corners]
\tikzstyle{dummy}=[circle,draw]
\node(ha1)[a] {See \textit{myers2018anomaly}};
\node(ha2)[a, below=of ha1] {See \textit{yousfi2019discovering} \textit{yousfi2019discovering}\\ \textit{yousfi2019discovering} \textit{yousfi2019discovering} \textit{yousfi2019discovering}};
\node(ha3)[a, below=of ha2] {See \textit{diamantini2017discovering}};
\node(h1)[a, left=of ha1] {Industrial control systems devices} ;
\path (h1) |- node(h2)[a] {Ubiquitous computing devices} (ha2) ;
\path (h2) |- node(h3)[a] {Geotags} (ha3);
\draw (ha1)edge(h1);
\draw (ha2)edge(h2);
\draw (ha3)edge(h3);
\end{tikzpicture}
\caption{side-by-side is now right, centering is correct}
\end{figure}
\end{document}
答案2
使用该tikz-cd
包绘制图像很简单,箭头是直的,代码很短:
\documentclass{article}
\usepackage{geometry}
\usepackage{tikz-cd}
\usepackage{makecell}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzcd}[column sep = 2em,
row sep = 1ex,
cells = {nodes={draw, semithick, rounded corners,
fill=gray!10}
},
]
Industrial control systems devices
\ar[r]
& See \textit{myers2018anomaly} \\
Ubiquitous computing devices
\ar[r]
& \makecell{See
\textit{yousfi2019discovering yousfi2019discovering} \\
\textit{yousfi2019discovering yousfi2019discovering} \\
\textit{yousfi2019discovering}} \\
Geotags
\ar[r]
& See \textit{diamantini2017discovering}
\end{tikzcd}
\caption{side-by-side is now right, centering is correct}
\label{fig:tikzcd}
\end{figure}
\end{document}