![跨越多个矩阵列的节点中的文本垂直对齐](https://linux22.com/image/287494/%E8%B7%A8%E8%B6%8A%E5%A4%9A%E4%B8%AA%E7%9F%A9%E9%98%B5%E5%88%97%E7%9A%84%E8%8A%82%E7%82%B9%E4%B8%AD%E7%9A%84%E6%96%87%E6%9C%AC%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90.png)
以下 MWE 的问题是我不明白为什么大节点中的文本没有对齐在中心(垂直而言)。
我怎样才能做到这一点?
图片
平均能量损失
\documentclass[
11pt
]{scrartcl}
\usepackage{
tikz,
relsize,
tgheros
}
\usetikzlibrary{
calc,trees,shadows,positioning,arrows,chains,
decorations.pathreplacing,
decorations.pathmorphing,
decorations.shapes,
decorations.text,
shapes,
shapes.geometric,
shapes.symbols,
matrix,
patterns,
intersections,
fit
}
\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}
\tikzset{
>=latex,
line/.style={draw, thick, ->},
phase/.style={
align=left,
anchor=center,
rectangle,
%drop shadow={opacity=0.5},
% minimum width=2cm,
minimum height=1.0cm,
% text width=6cm,
font=\sffamily\bfseries\small,
draw=black,
very thick,
fill=black!40,
%top color=white,
%bottom color=white!20!blue
},
phasentext/.style={
align=left,
anchor=center,
%node distance=0.5cm,
rectangle,
%drop shadow={opacity=0.5},
%rounded corners,
minimum width=2cm,
minimum height=1cm,
% text width=7cm,
draw=gray,
very thick,
%fill=black!20,
%shade,
font=\sffamily\small,
%top color=white!80!black,
%bottom color=white
}
}
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small,node distance=0.5cm]
\matrix (Versuchsplan) [
matrix of nodes,
nodes in empty cells,
row sep=6mm,
column sep=3mm,
inner sep=7pt
] {
%
|[phase]| 1 & & & & \\
& |[phasentext] (WordA)| Testa: \tikz \draw[thick, circular glow={fill=gray}] (0,0) circle [radius=6pt]; &
|[phasentext] (WordB)| Stuff: \tikz \draw[very thick, drop shadow] (0.2,0.1) rectangle (0.7,0.2); &
|[phasentext] (WordC)| Stuff text word: \begin{tikzpicture} \draw[thick] (0,0) circle [radius=6pt]; \draw[thick,fill=black] (0,0) circle [radius=0.5pt]; \end{tikzpicture} &
|[phasentext] (WordD)| Some more words: \begin{tikzpicture} \draw[thick] (0,0) circle [radius=6pt]; \draw[thick] (-3pt,-2pt) -- (3pt,-2pt) -- (0,2pt) -- cycle; \draw[thick] (0,2pt) -- (0,-2pt); \end{tikzpicture} \\
};
\node[phase, fit=(Versuchsplan-1-2)(Versuchsplan-1-5), align=left, anchor=center] (Words) {Longs text I suppose};
\end{tikzpicture}
\end{center}
\end{document}
答案1
正如 PGF-TikZ 版本手册3.0.0
(第 68 页,第 5.1 节)中所述,您必须手动调整text height
和text depth
。在下面的代码中,我添加了红线以突出显示正确的垂直位移。
\documentclass[
11pt
]{scrartcl}
\usepackage{
tikz,
relsize,
tgheros
}
\usetikzlibrary{
calc,trees,shadows,positioning,arrows,chains,
decorations.pathreplacing,
decorations.pathmorphing,
decorations.shapes,
decorations.text,
shapes,
shapes.geometric,
shapes.symbols,
matrix,
patterns,
intersections,
fit
}
\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}
\tikzset{
>=latex,
line/.style={draw, thick, ->},
phase/.style={text height=1.5ex,text depth=2ex,%<-- Here's the trick
align=left,
anchor=center,
rectangle,
%drop shadow={opacity=0.5},
% minimum width=2cm,
minimum height=1.0cm,
% text width=6cm,
font=\sffamily\bfseries\small,
draw=black,
very thick,
fill=black!40,
%top color=white,
%bottom color=white!20!blue
},
phasentext/.style={
align=left,
anchor=center,
%node distance=0.5cm,
rectangle,
%drop shadow={opacity=0.5},
%rounded corners,
minimum width=2cm,
minimum height=1cm,
% text width=7cm,
draw=gray,
very thick,
%fill=black!20,
%shade,
font=\sffamily\small,
%top color=white!80!black,
%bottom color=white
}
}
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small,node distance=0.5cm]
\matrix (Versuchsplan) [
matrix of nodes,
nodes in empty cells,
row sep=6mm,
column sep=3mm,
inner sep=7pt
] {
%
|[phase] (1)| 1 & & & & \\
& |[phasentext] (WordA)| Testa: \tikz \draw[thick, circular glow={fill=gray}] (0,0) circle [radius=6pt]; &
|[phasentext] (WordB)| Stuff: \tikz \draw[very thick, drop shadow] (0.2,0.1) rectangle (0.7,0.2); &
|[phasentext] (WordC)| Stuff text word: \begin{tikzpicture} \draw[thick] (0,0) circle [radius=6pt]; \draw[thick,fill=black] (0,0) circle [radius=0.5pt]; \end{tikzpicture} &
|[phasentext] (WordD)| Some more words: \begin{tikzpicture} \draw[thick] (0,0) circle [radius=6pt]; \draw[thick] (-3pt,-2pt) -- (3pt,-2pt) -- (0,2pt) -- cycle; \draw[thick] (0,2pt) -- (0,-2pt); \end{tikzpicture} \\
};
\node[phase, fit=(Versuchsplan-1-2)(Versuchsplan-1-5), align=left, anchor=center] (Words) {Longs text I suppose};
\draw [red] let \p1=(1.base),
\p2=(Words.base)
in (\x1,\y1) -- ++(5cm,0)
(\x2,\y2) -- ++(-5cm,0);
\end{tikzpicture}
\end{center}
\end{document}