8

I want to change the font size of text in a node. Ideally finally I have multiple font sizes in a node. However when trying to change the font size in the node, I get unexpected different line spacings. Here is the code:

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\node[anchor=north east,text width=0.2\textwidth,font=\small] at (0,0) {This text demonstrates how your own text will look when you replace the placeholder with your own text.};
\node[anchor=north west,text width=0.2\textwidth] at (0,0) {\small This text demonstrates how your own text will look when you replace the placeholder with your own text.};
\end{tikzpicture}
\end{document}

As you can clearly see, setting the font size in the node leads to a different line spacing. How can I prevent this? And what is going on there?

enter image description here

2 Answers 2

8

Very likely, the text for the node is enclosed in a group and the final \par that triggers line breaking is outside this group, so the standard baseline distance is in effect and not the one pertaining to \small.

You can see the same effect below:

\documentclass{article}

\begin{document}

\begin{minipage}[t]{0.2\textwidth}
\raggedright
\small
This text demonstrates how your own text will 
look when you replace the placeholder with 
your own text.
\end{minipage}
\quad
\begin{minipage}[t]{0.2\textwidth}
\raggedright
{\small
This text demonstrates how your own text will 
look when you replace the placeholder with 
your own text.}
\end{minipage}

\end{document}

enter image description here

Add \par.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\node[anchor=north east,text width=0.2\textwidth,font=\small] at (0,0) {This text demonstrates 
how your own text will look when you replace the placeholder with your own text.};
\node[anchor=north west,text width=0.2\textwidth] at (0,0) {\small This text demonstrates how 
your own text will look when you replace the placeholder with your own text.\par};
\end{tikzpicture}
\end{document}

enter image description here

1

@egreg Not an answer, but an observation. If you overlap the two nodes you can see that even the space between the words look a little different, but the two nodes seem to have exactly the same dimensions.

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[anchor=north east,text width=0.2\textwidth,font=\small,draw] at (0,0) {This text demonstrates how your own text will look when you replace the placeholder with your own text.};
\node[anchor=north east,text width=0.2\textwidth,red,opacity=0.5,draw] at (0,0) {\small This text demonstrates how your own text will look when you replace the placeholder with your own text.\par};
\end{tikzpicture}
\end{document}
1
  • 2
    TikZ installs \leftskips and \rightskips that are dependent on the unit em right after it executes whatever was setup via the font key. When you use \small inside the node this don't get updated – similar to the baseline. Aug 26, 2022 at 22:54

You must log in to answer this question.

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