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}

是否有節點樣式參數來定義此類前置和後置命令?存在 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.5exto\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}

在此輸入影像描述

相關內容