tikz:每个节点文本的前缀和后缀

tikz:每个节点文本的前缀和后缀

为了配置节点的多行文本行距,我不得不在每个节点内用 开头\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}

是否有节点样式参数来定义这样的前缀和后缀命令?存在 postactionpreactions参数对我没有帮助。以下代码不会产生正确的行距(\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}

在此处输入图片描述

相关内容