Tikz 节点文本格式问题

Tikz 节点文本格式问题

我在 Tikz 中的节点文本格式方面遇到了一个奇怪的问题。我想使用节点在 Tikz 图表中创建信息框,例如在 TikZ & PGF Manual v3.0.1a - Till Tantau, 2015,第 2.1 节,第 29 页中,这样的解释框位于几何图的右侧,其 Tikz 代码见第 45 页。

有时,希望在这些信息框内有更复杂的格式,例如多种字体大小。

然而,我遇到了 Tikz 生成的格式问题,如下图所示,我创建了 6 个示例节点:-

节点 1
我们将node font属性设置为\LARGE
对于第二段,我们将其更改为\large
第二段的行距过大。

节点 2
这里我们没有使用node font属性,而只是\LARGE使用了一个段落。
格式不正确。

节点 3
这和节点 2 一样,只是我们添加了\large第二个段落。
格式有所改善,但仍然不正确。

节点 4
这与节点 1 完全相同,只是这次我们将第二段文本括在环境中quote
格式明显改善。

节点 5
这与节点 1 完全相同,只是这次我们将第二段文本括在description列表环境中。
格式明显改善。

节点 6
这与节点 1 完全相同,只是这次我们将第二段文本括在环境中trivlist
格式看起来相当不错。使用\vspace具有负数空格量的命令可以删除两个段落之间不需要的空格。


因此,Node 6 中的方法提供了某种“黑客”手段来获得正确的格式。

不过,我很想知道为什么会出现这些问题,有没有比上述方法更合适的方法来获得正确的格式?

我在 Windows 7 上使用 MiKTeX v2.9 和 TeXstudio v2.12.10。


在此处输入图片描述


\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage[margin=0.3in]{geometry}

\begin{document}

\begin{tikzpicture}

% Node 1
\node[draw, inner sep=0.5em, text width=5cm, anchor=south west, node font=\LARGE] at (0, 0) {
PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.

\large

The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.
};


% Node 2
\node [draw, inner sep=0.5em, text width=5cm, anchor=south west] at (6.5, 0) {
\LARGE
PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ. The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.
};


% Node 3
\node [draw, inner sep=0.5em, text width=5cm, anchor=south west] at (13, 0) {
\LARGE
PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ. 

\large
The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.
};

\end{tikzpicture}

\vspace{4em}


\begin{tikzpicture}

% Node 4
\node[draw, inner sep=0.5em, text width=5cm, anchor=north west, node font=\LARGE] at (0, 0) {
PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.

\large

\begin{quote}
The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.
\end{quote}

};


% Node 5
\node[draw, inner sep=0.5em, text width=5cm, anchor=north west, node font=\LARGE] at (6.5, 0) {
PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.

\large

\begin{description}
\item[item1] The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.
\end{description}

};


% Node 6
\node[draw, inner sep=0.5em, text width=5cm, anchor=north west, node font=\LARGE] at (13, 0) {
PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.

\large

\vspace{-0.5em}

\begin{trivlist}
\item The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.
\end{trivlist}

};

\end{tikzpicture}


\end{document}

答案1

该问题与以下内容相关:Tikz 节点中的文本不遵守基线跳跃

以下 MWE 和输出演示了在节点中设置字体大小的 8 个用例。结果取决于传递给 的键的选择\node、使用字体大小开关(例如\Large节点内部的 ),以及可能需要\par在需要应用字体大小和基线设置的文本块末尾添加 。

  1. 沒有node font=密鑰傳遞給\node
  2. 与 1 相同 +\par在第一个文本块的末尾添加。
  3. 与 2 相同 +\par在两个文本块的末尾添加。
  4. 使用钥匙node font=\Large,不\par
  5. 与 4 相同但添加\par在两个文本块的末尾。
  6. 与 5 相同,但替换node font=\Largefont=\Large
  7. 用语法替换字体大小开关\fontsize{}{}\selectfont{},没有\par
  8. 与 7 相同,但添加\par在两个文本块的末尾。

这些测试表明,当传递给 的键中省略node font=和时,需要在第一个文本块的末尾添加(示例 2),但不需要添加到第二个文本块的末尾(示例 3)。在 OP 的帖子中,这解释了为什么示例 2 排版不正确。font=\node\par

添加node font=(示例 4-5)或font=(示例 6)需要添加\par(示例 5-6)。

\fontsize{font size}{baselineskip}\selectfont只要par添加,就可以使用语法(示例 7-8)更改字体大小和基线跳跃。

这是 8 个示例的输出:

在此处输入图片描述

这是 MWE:

\documentclass[tikz,border=6pt,12pt,article]{standalone}
\usetikzlibrary{positioning}
\usepackage{verbatim}

\tikzset{mynode/.style={draw, inner sep=0.5em, text width=5cm},
         mybox/.style={draw, rectangle, align=center,inner sep=0.5em, text width=5cm,minimum height=3.5cm}
         }

\begin{document}
  \begin{tikzpicture}
    % Node 1
    \node[mynode] (A) at (0, 0) {
    \LARGE PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.
    {\small \color{blue} The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.}
    };

    % Node 2
    \node[mynode,right=of A.south east,anchor=south west] (B) {
    \LARGE PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.\par
    {\small \color{blue} The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.}
    };

    % Node 3
    \node[mynode,right=of B.south east,anchor=south west] (C) {
    \LARGE PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.\par
    {\small \color{blue} The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.}
    \par};

    % Node 4
    \node[mynode,node font=\LARGE,right=of C.south east,anchor=south west] (D) {
    PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.
    {\small \color{blue} The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.}
    };

    % Node 5
    \node[mynode,node font=\LARGE,right=of D.south east,anchor=south west] (E) {
    PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.\par
    {\small \color{blue} The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.\par}
    };

    % Node 6
    \node[mynode,font=\LARGE,right=of E.south east,anchor=south west] (F) {
    PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.\par
    {\small \color{blue} The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.\par}
    };

    % Node 7
    \node[mynode,font=\LARGE,right=of F.south east,anchor=south west] (G) {
    PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.\par
    {\fontsize{12pt}{14pt}\selectfont \color{blue} The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.}
    };

    % Node 8
    \node[mynode,font=\LARGE,right=of G.south east,anchor=south west] (H) {
    PGF/TikZ is a pair of languages for producing vector graphics from a geometric/algebraic description. PGF is a lower-level language, while TikZ.\par
    {\fontsize{12pt}{14pt}\selectfont \color{blue} The top-level PGF and TikZ commands are invoked as TeX macros, but in contrast with PSTricks, the PGF/TikZ graphics themselves are described in a language that resembles MetaPost.\par}
    };

    \node[mybox,above=of D.north] (I) {4\\add \verb|node font=\Large| key, no \verb|\par|.};
    \node[mybox] at (A |- I) {1\\no \verb|node font=| or \verb|font=| keys, no \verb|\par|};
    \node[mybox] at (B |- I) {2\\no \verb|node font=| or \verb|font=| keys, add \verb|\par| after first text block only.};
    \node[mybox] at (C |- I) {3\\no \verb|node font=| or \verb|font=| keys, add \verb|\par| after first text block only.};
    \node[mybox] at (E |- I) {5\\use \verb|node font=\LARGE| key, add \verb|\par| after both text blocks.};
    \node[mybox] at (F |- I) {6\\replace \verb|node font=\LARGE| with \verb|font=\LARGE| key, add \verb|\par| after both text blocks.};
    \node[mybox] at (G |- I) {7\\use \verb|\fontsize{}{}\selectfont|, add \verb|\par| after first text block only.};
    \node[mybox] at (H |- I) {8\\use \verb|\fontsize{}{}\selectfont| syntax, add \verb|\par| after both text blocks.};

    \end{tikzpicture}
\end{document}

相关内容