如何才能使形状“矩形分割”的“节点部分”内的一些文本向左对齐,而另一些文本向右对齐:
\documentclass[]{book}
\usepackage{tikz}
\usetikzlibrary {shapes.multipart}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\node[
rectangle split, rectangle split parts=3,
draw, anchor=center,
minimum size=25mm, inner sep=2pt,
]{
first
\nodepart{second} left right
\nodepart{third} third
};
\end{tikzpicture}
\end{document}
答案1
如果你正在设置一个minimum width
并且可以确保节点实际上不比该节点宽,那么你可以设置一个text width
依赖于它的节点并应用如何将单词均匀分布在几行中?。
由于 PGF/TikZ 允许 TeX 对节点文本进行所有排版,因此您必须对其方法进行任何文本间距处理。(使用text width
将其委托给 TeX,因为 TikZ 只是minipage
在节点文本内安装了一个。)
代码
\documentclass[]{book}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\makeatletter
\tikzset{
align/spread/.code=\def\tikz@text@action{\parfillskip=0pt},
spread across minimum width/.style={
text width=\pgfkeysvalueof{/pgf/minimum width}
-2*(\pgfkeysvalueof{/pgf/inner xsep}),
align=spread}}
\makeatother
\begin{document}
\sffamily
\tikz\node[
rectangle split, rectangle split parts=3,
draw, anchor=center, minimum size=25mm, inner sep=2pt]{
first
\nodepart[spread across minimum width]{second} left right
\nodepart{third} third
};
\tikz\node[
rectangle split, rectangle split parts=3,
draw, anchor=center, minimum size=30mm, inner sep=2pt]{
first
\nodepart[spread across minimum width]{second} left middle right
\nodepart{third} third
};
\end{document}
输出
答案2
像这样?
\documentclass[]{book}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\node[
rectangle split, rectangle split parts=3,
draw, align=center, % <----
text width=25mm, % <----
inner sep=2pt,
]{
first
\nodepart{second} left\hfill right
\nodepart{third} third
};
\end{tikzpicture}
\end{document}
编辑:
由于您坚持不在text width
多部分节点中使用选项,因此下面是替代的、更复杂的方法:
\documentclass[]{book}
\usepackage{tikz}
\usetikzlibrary {shapes.multipart}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\node (n) [
rectangle split, rectangle split parts=3,
draw, align=center, % <----
minimum size=25mm,
inner sep=2pt,
]{
first
\nodepart{second} \vphantom{fg}
\nodepart{third} third
};
\node[right] at (n.two west) {left};
\node[left] at (n.two east) {right};
\end{tikzpicture}
\end{document}
附录:
隐式使用text width
,现在用作nodepart
选项并根据节点的最小宽度计算。受 Qrrbrbirlbel 回答启发(+1 for align/spread/.code
!)
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\tikzset{
VMPN/.style = {rectangle split, rectangle split parts=3,
draw, anchor=center,
minimum size=25mm, inner sep=2pt,
text width=\pgfkeysvalueof{/pgf/minimum width}
-2*(\pgfkeysvalueof{/pgf/inner xsep}),
align=center}, % for my addendum
}
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\node (n) [VMPN]{first
\nodepart{second} left\hfill right
\nodepart{third} third
};
\end{tikzpicture}
\end{document}
结果与第一个例子相同。