4

I have encountered a strange problem with text formatting within Nodes in Tikz. I want to use nodes to create information boxes within Tikz diagrams, for example as in the TikZ & PGF Manual v3.0.1a - Till Tantau, 2015, section 2.1, pg 29, where such an explanatory box is placed to the right of the geometry diagram, the Tikz code for which is seen on pg 45.

Sometimes it is desirable to have more complicated formatting within such information boxes such as multiple font sizes.

However I am encountering problems with the formatting that Tikz produces, as seen in the diagram below where I have created 6 example nodes :-

Node 1
We use the node font property set to \LARGE.
For the second paragraph we change to \large.
The second paragraph has excessive line spacing.

Node 2
Here we do not use node font property, but just \LARGE with a single paragraph.
The formatting does not come out properly.

Node 3
This is as Node 2, except we add \large and a second paragraph.
The formatting improves somewhat but is still not right.

Node 4
This is exactly as Node 1 except this time we enclose the second paragraph text within a quote environment.
The formatting improves notably.

Node 5
This is exactly as Node 1 except this time we enclose the second paragraph text within a description list environment.
The formatting improves notably.

Node 6
This is exactly as Node 1 except this time we enclose the second paragraph text within a trivlist environment.
The formatting appears reasonably good. Using a \vspace command with a negative amount of space removes the unwanted space between the 2 paragraphs.


Thus the method in Node 6 provides some sort of a `hack' to get the formatting right.

However I would be interested to know why are these problems occurring in the first place, and is there any more proper way to get the formatting right than the above hack ?

I am using MiKTeX v2.9, and TeXstudio v2.12.10 on Windows 7.


enter image description here


\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

1 Answer 1

2

This question is related to: Text in Tikz node not respecting baselineskip.

The following MWE and output demonstrates 8 use cases for setting the font size in a node. The outcomes depend on the choice of keys passed to \node, the use font size switches such as \Large inside the node, and the possible need to add a \par at the end of text blocks to which font size and baseline settings need to be applied.

  1. No node font= key passed to \node.
  2. Same as 1 + add \par at the end of the first text block.
  3. Same as 2 + add \par at the end of both text blocks.
  4. Use the key node font=\Large, no \par.
  5. Same as 4 but add \par at the end of both text blocks.
  6. Same as 5 but replace node font=\Large with font=\Large.
  7. Replace font size switch with \fontsize{}{}\selectfont{} syntax, no \par.
  8. Same as 7 but add \par at the end of both text blocks.

These tests show that when node font= and font= are omitted from the keys passed to \node, it is necessary to add \par at the end of the first text block (example 2), but not at the end of the second text block (example 3). In the OP's post, this explains why example 2 is not typeset correctly.

Adding either node font= (examples 4-5) or font= (example 6) requires adding \par (examples 5-6).

Font size and baselineskip can be changed using \fontsize{font size}{baselineskip}\selectfont syntax (examples 7-8) so long as par is added.

This is the output from the 8 examples:

enter image description here

This is the 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}
1
  • Thanks, \par does the trick. My Node 2 is fixed by putting \par at the end of the paragraph, and my Node 1 is fixed by putting \par at the end of the second paragraph. Perhaps there are some quirks within Tikz re node text formatting. However I have just noticed in the Tikz manual v3.0.1a in the Geometry example on p62 a node which does make use of \par commands (and a \vskip) for formatting. Also, searching for \par in that document shows use of other related Latex commands such as \parsep, \parskip, \parindent with Tikz. We should also end paragraphs with blank lines (Lamport p13). Sep 3, 2018 at 20:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .