25

Inside a TikZ node, one can force line breaks using \\ providing the align option of the node is set.

I would like to increase the line spacing inside a node. This can be done in the usual way with the optional parameter of \\, e.g. \\[1em]. Doing this for every line break is tedious though. Is there a way to increase the break globally, so that any \\ inside a node acts as \\[1em]?

EDIT: Almost there!

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}

\tikzset{
  mynode/.style={
    draw
  , align=center
  , execute at begin node=\setlength{\baselineskip}{2em}
  }
}

\begin{tikzpicture}
  \node[mynode] {
    this \\
    is \\
    \tikz \node[mynode] {a \\ break};
  };
\end{tikzpicture}

\end{document}

This produces:

Node breaks

The remaining question is how to place a 2em break before the inner node.

1
  • 1
    Remark: it might be better to specify vertical distances in terms of ex's, not em's.
    – mbork
    Aug 5, 2011 at 18:03

2 Answers 2

14

What about this?

\setlength{\baselineskip}{16pt}

or

\baselineskip=16pt

(If it doesn't work, try ending your node text with an explicit \par.)

Now as for the second part: this is not only non-trivial, but also not well-defined. As the name suggests, \baselineskip is the distance between the baselines of consecutive lines of text (or hboxes, if you prefer). In the case of the "inner" tikz node, there's the question: where its baseline is located? This is configurable by the tikz option baseline (this was hard to guess, right;)?), but if you set it higher, then - if you add another line or whatever below - the distance there will be to small.

Probably the best answer to your problem are the \lineskip and \lineskiplimit parameters. Actually (well, AFAIR, since I've got my copy of The TeXbook at my office), the rule is (more or less) as follows:

if TeX puts two boxes on a vertical list, it puts glue between them so the distance between their baselines equals \baselineskip. But, if the distance between the bottom of the upper box and the top of the lower one would be smaller than \lineskiplimit, the glue inserted equals \lineskip.

Therefore you might want to set it up the following way:

\baselineskip=<the distance you want between the baselines>
\lineskiplimit=<the threshold distance>
\lineskip=<the distance you want between the boxes (not their baselines!) if they are close to each other>

where "close to each other" means "closer than the threshold".

4
  • 1
    length macros need \ (aka escape character) but counter macros do not. I also did the same mistake often in the past. :-) Aug 5, 2011 at 18:43
  • 2
    @xport: This is because \baselineskip is a length defined by TeX itself. On the other hand if you create a counter by \newcounter{name} (a LaTeX command) this actually to the TeX \newcounter\c@name plus initializing plus creating the \thename macro.
    – Caramdir
    Aug 5, 2011 at 19:42
  • xport: thanks for correcting me! Caramdir: thanks for the explanation!
    – mbork
    Aug 5, 2011 at 21:03
  • 11
    Where in the document to place \baselineskip=16pt and \setlength{\baselineskip}{16pt}
    – Viesturs
    Aug 19, 2017 at 10:28
0

I'm adding this answer mainly because it appears that Forest tree nodes do not support internal \baselineskip modification. This method uses \linespread{<scaling factor>}\selectfont. The side effect is that it affects the spacing between the edge and the label; inner sep must be readjusted accordingly.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes}
\usepackage[linguistics]{forest}

\begin{document}
    
    \tikzset{
        mynode/.style={
            draw
            , align=center
            , execute at begin node = \linespread{1.65}\selectfont
        }
    }

\begin{tikzpicture}
    \node[mynode] {
        this \\
        is \\
        \tikz \node[mynode] {a \\ break};
    };
\end{tikzpicture}

\vspace{5ex}

\begin{forest}
  [DP\\the cat, anchor=north, execute at begin node = \setlength{\baselineskip}{2em}% This command has no effect.
    [D\\the, anchor=north, execute at begin node = \linespread{1.65}\selectfont]
    [NP\\cat, anchor=north, execute at begin node = \linespread{2}\selectfont]
  ]
\end{forest}

\end{document}

enter image description here

You must log in to answer this question.

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