假设有四个节点,排成两行,每行内都有文本。进一步假设我想按以下模式排列这些节点:
|----Node A----| |Node B|
|Node C||Node D|
我通过以下方式对齐了节点 C anchor=north east
(有点违反直觉,但是......),但节点 C 和 D 的组合在每一边都比节点 A 略长。
我该如何纠正这个问题?
\documentclass[border=5mm]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, arrows.meta}
\usetikzlibrary{calc, arrows, fit, positioning, shapes.symbols, chains}
\usetikzlibrary{decorations.markings, decorations.pathmorphing, decorations.pathreplacing}
\pagenumbering{gobble} % Remove page number
\begin{document}
% Define block styles
\tikzstyle{block_large} = [rectangle, draw, text width=8cm, rounded corners, text height=0.4cm, text depth=1.25cm]
\tikzstyle{block_medium} = [rectangle, draw, text width=4cm, rounded corners, text height=0.4cm, text depth=1.25cm]
\begin{tikzpicture}[node distance=1.5cm, auto, comment/.style={rectangle, inner sep=5pt}]
% Draw nodes - first row
\node [block_large] (col1_row1) {
\textbf{Node A}\\
\textit{Foo:} Bar\\
\textit{Baz:} Qux
};
\node [block_medium, right=1cm of col1_row1] (col3_row1) {
\textbf{Node B}\\
\textit{Foo:} Bar\\
\textit{Baz:} Qux
};
% Draw nodes - second row
\node [block_medium, below=0.1cm of col1_row1, anchor=north east] (col1_row2) {
\textbf{Node C}\\
\textit{Foo:} Bar\\
\textit{Baz:} Qux
};
\node [block_medium, right=0cm of col1_row2] (col2_row2) {
\textbf{Node D}\\
\textit{Foo:} Bar\\
\textit{Baz:} Qux
};
\end{tikzpicture}
\end{document}
输出:
答案1
实际上,由于您定义了text width
框的,因此需要注意,这会inner sep
增加框的大小。因此,通过定义inner xsep
并调整框的大小,结果如下:
代码:
\documentclass[border=5mm]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, arrows.meta}
\usetikzlibrary{calc, arrows, fit, positioning, shapes.symbols, chains}
\usetikzlibrary{decorations.markings, decorations.pathmorphing, decorations.pathreplacing}
\pagenumbering{gobble} % Remove page number
\begin{document}
% Define block styles
\tikzstyle{block_large} = [rectangle, draw, text width=8cm, inner xsep=0.25cm, rounded corners, text height=0.4cm, text depth=1.25cm]
\tikzstyle{block_medium} = [rectangle, draw, text width=3.75cm, inner xsep=0.25cm, rounded corners, text height=0.4cm, text depth=1.25cm]
\begin{tikzpicture}[node distance=1.5cm, auto, comment/.style={rectangle, inner sep=5pt}]
% Draw nodes - first row
\node [block_large] (col1_row1) {
\textbf{Node A}\\
\textit{Foo:} Bar\\
\textit{Baz:} Qux
};
\node [block_medium, right=1cm of col1_row1] (col3_row1) {
\textbf{Node B}\\
\textit{Foo:} Bar\\
\textit{Baz:} Qux
};
% Draw nodes - second row
\node [block_medium, below=0.1cm of col1_row1, anchor=north east] (col1_row2) {
\textbf{Node C}\\
\textit{Foo:} Bar\\
\textit{Baz:} Qux
};
\node [block_medium, right=0cm of col1_row2] (col2_row2) {
\textbf{Node D}\\
\textit{Foo:} Bar\\
\textit{Baz:} Qux
};
\end{tikzpicture}
\end{document}
答案2
对于这种情况,tikzpicture
您可以考虑tcolorbox
使用它的raster
库来或多或少轻松地组织文本框。
在这种特殊情况下,可以使用以下方法获得与您想要的结果类似的结果
\documentclass{article}
\usepackage[most]{tcolorbox}
\newcommand{\mytext}[1]{\textbf{Node #1}\\\textit{Foo:} Bar\\\textit{Baz:} Qux}
\begin{document}
\begin{tcbitemize}[raster equal height=rows, raster columns=3, notitle, colback=white]
\tcbitem[raster multicolumn=2] \mytext{A}
\tcbitem \mytext{B}
\tcbitem \mytext{C}
\tcbitem \mytext{D}
\end{tcbitemize}
\end{document}