6

I am adding text with \node to a graph with the help of the pfgplots package. Inside the \node text I want a line break. From this answer a solution is to add a text width such as [text width=0] which will allow usual line breaks \\ to be parsed (it works maybe because setting a text width creates a mini page or something like that).

A working example where I add such text width and use \\ for line break is:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}    

\begin{tikzpicture}
  \begin{axis}[xmin=0, xmax=11,ymin=600,ymax=1000]
    \node (source) at (axis cs:5, 750) [text width=0]{\color{gray} {\tiny line1\\line2}};
  \end{axis}
\end{tikzpicture}

\end{document}

The result:

enter image description here

As you can see, I have also reduced the font size to \tiny for this \node. But the line spacing done with \\ does not seem to follow this font-size reduction. The line spacing - the distance between the two gray text lines - is simply way too big now. Can reduced line spacing be achieved?

Steeven
  • 1,407

3 Answers3

9

somehow similar to @esdd answer but with even closer lines:

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[xmin=0, xmax=11,ymin=600,ymax=1000]
    \node (source) at (5, 750) [align=left, text=gray,
                                font=\tiny\linespread{0.8}\selectfont]  
                               {line1\\line2};
  \end{axis}
\end{tikzpicture}

\end{document}
Zarko
  • 296,517
5

You might use the optional argument of \\:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}    

\begin{tikzpicture}
  \begin{axis}[xmin=0, xmax=11,ymin=600,ymax=1000]
    \node (source) at (axis cs:5, 750) [text width=0]{\color{gray} {\tiny line1\\[-3ex]line2}};
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Skillmon
  • 60,462
5

You have to add \par before closing the group:

\node (source) at (axis cs:5, 750) [text width=0]{\color{gray} {\tiny line1\\line2\par}};

But I think its better to use the node options to change the color and the fontsize of the node text. Additionally I would replace text width=0 by align=left.

Result:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
  \pgfplotsset{every tick label/.append style={font=\tiny}}
  \begin{axis}[xmin=0, xmax=11,ymin=600,ymax=1000]
    \node (source) at (axis cs:5, 750) [align=left,font=\tiny,text=gray]{line1\\line2};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

esdd
  • 85,675