![tikz:每个节点文本的前缀和后缀](https://linux22.com/image/479445/tikz%EF%BC%9A%E6%AF%8F%E4%B8%AA%E8%8A%82%E7%82%B9%E6%96%87%E6%9C%AC%E7%9A%84%E5%89%8D%E7%BC%80%E5%92%8C%E5%90%8E%E7%BC%80.png)
为了配置节点的多行文本行距,我不得不在每个节点内用 开头\baselineskip=2.5ex
并在后面添加一个\par
。有没有办法避免重复执行此操作?
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc,fit,positioning}
\begin{document}
\begin{tikzpicture}[
remember picture, overlay,
every node/.style={fill=green, font=\large}
]
\node (N0)
at (0,0)
{Start}; % <- That's what I want for multi-line text: Only the text
\node[right=of N0, text width=3cm, align=center] (N1)
{\baselineskip=2.5ex Looooooooooong multi-line text.\par}; % <- That's what's required
\node[right=of N1, text width=3cm, align=center] (N2)
{\baselineskip=2.5ex Another looooooooooong multi-line text.\par};
\end{tikzpicture}
\end{document}
是否有节点样式参数来定义这样的前缀和后缀命令?存在 postaction
和preactions
参数对我没有帮助。以下代码不会产生正确的行距(\par
似乎被忽略了)。
%...
every node/.style={fill=green, font=\large, postaction={\par}}
%...
\node[right=of N1, text width=3cm, align=center] (N2)
{\baselineskip=2.5ex Another looooooooooong multi-line text.};
%...
有任何想法吗?
答案1
我认为没有必要\par
,但您可以使用/tikz/execute at begin node
和/tikz/execute at end node
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
every node/.style={fill=green, font=\large},
execute at begin node={\baselineskip=5ex\relax},
% execute at end node={\endgraf}% not needed, as far as I can see
]
\node (N0) at (0,0) {Start};
\node[right=of N0, text width=3.2cm, align=center] (N1)
{Looooooooooong multi-line text.};
\node[right=of N1, text width=3.2cm, align=center] (N2)
{Another looooooooooong multi-line text.};
\end{tikzpicture}
\end{document}
我删除了不需要的remember picture, overlay
,将 3cm 增加到 3.2cm 以避免\hbox
第二个节点过满,并增加了\baselineskip=2.5ex
以\baselineskip=5ex
使其效果更加明显。 确保TeX在读取分配给 的值时\relax
不会尝试扩展 之后的标记。5ex
\baselineskip
答案2
你可以使用font
。看看这是否适合你。我用它\baselineskip=5ex
来强调差异。
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{calc,fit,positioning}
\begin{document}
\begin{tikzpicture}[
remember picture, overlay,
every node/.style={fill=green, font=\large},
baselineskip/.style={font={\large\baselineskip=#1}}
]
\node (N0)
at (0,0)
{Start}; % <- That's what I want for multi-line text: Only the text
\node[right=of N0, text width=3cm, align=center,baselineskip=5ex] (N1)
{Looooooooooong multi-line text.}; % <- That's what's required
\node[right=of N1, text width=3cm, align=center,baselineskip=5ex] (N2)
{Another looooooooooong multi-line text.};
\end{tikzpicture}
\end{document}