0

This answer describes how to create multiline axis labels for boxed axes. However, when I try to do this with non-boxed axes it fails. For example,

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
  title=MWE,
  xlabel style={align=center}, xlabel={some\\x\\label},
  ylabel style={align=center}, ylabel={some\\y\\label},
  axis x line=middle,
  % axis y line=middle,
  xtick=\empty,
  ytick=\empty,
]
  \addplot[
    samples=100,
    domain=-2:2,
  ] {x};
\end{axis}
\end{tikzpicture}
\end{document}

Produces enter image description here

How can I get the x-axis label to also span 3 lines?

I'm aware that the question linked above also discusses the use of text width as a possible solution. However, replacing xlabel style=... in my example with xlabel style={align=center, text width=2cm} does not change the result.

2
  • @jsbibra that question asks about boxed axes, which is the same as the question I linked. But, this question asks specifically about non-boxed axes. I'm aware of the two methods mentioned in both links (align and text width) but they don't have an effect here when used in the same way.
    – MattHusz
    Jul 30, 2020 at 15:46

2 Answers 2

0

The "problem" with your code is that you change axis x line after you have changed the xlabel style and thus your change is overwritten. So just change the axis x line first and you'll receive the result you expect.

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    title=MWE,
    axis x line=middle,     % <-- moved up
    xlabel style={align=center}, xlabel={some\\x\\label},
    ylabel style={align=center}, ylabel={some\\y\\label},
    xtick=\empty,
    ytick=\empty,
]
  \addplot[
    samples=100,
    domain=-2:2,
  ] {x};
\end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

0

xlabel is printed as node. So you can set a specific text width. This allows line breaks. The modification can be done by the option xlabel style. An example is shown in the documentation on page 173:

xlabel style={align=right,text width=3cm}

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    title=MWE,
    xlabel style= xlabel style={align=left,text width=2cm},{align=center}, xlabel={some\\x\\label},
    ylabel style={align=center}, ylabel={some\\y\\label},
    axis x line=middle,
    % axis y line=middle,
    xtick=\empty,
    ytick=\empty,
    ]
    \addplot[
    samples=100,
    domain=-2:2,
    ] {x};
    \end{axis}
    \end{tikzpicture}
\end{document}

EDIT

As long as the xaxis and yaxis are at the default pos that is bottom and left respectively the multiline is obeyed for the axis labels but if the yaxis or xaxis is moved to the center/middle the multiline environ does not behave and we get a single line axis label

In the example below moving the yaxis to middle but leaving the xaxis at default bottom pos-- here the xaxis retains multiline environ but not so for the yaxis

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[xlabel style={
                                align=left,
                                text width=3cm
                                },
                xlabel=A quite long label with a line break,
                title=MWE,
%               xlabel style={
%                               align=center,
%                               text width=1cm
%                               },
%               {align=center}, 
%               xlabel={some x label},
                ylabel style={align=center}, 
                ylabel={some\\y\\label},
%               axis x line=middle,
                 axis y line=middle,
%               xtick=\empty,
%               ytick=\empty,
                ]
    \addplot[
    samples=100,
    domain=-2:2,
    ] {x};
    \end{axis}
    \end{tikzpicture}
\end{document}

Now if xaxis also moves to the middle the multiline here also is not obeyed

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[xlabel style={
                                align=left,
                                text width=3cm
                                },
                xlabel=A quite long label with a line break,
                title=MWE,
%               xlabel style={
%                               align=center,
%                               text width=1cm
%                               },
%               {align=center}, 
%               xlabel={some x label},
                ylabel style={align=center}, 
                ylabel={some\\y\\label},
                axis x line=middle,
                 axis y line=middle,
%               xtick=\empty,
%               ytick=\empty,
                ]
    \addplot[
    samples=100,
    domain=-2:2,
    ] {x};
    \end{axis}
    \end{tikzpicture}
\end{document}

Probably one of the legends will clarify

1
  • It appears that just the {align=center} part has an effect. I'm not noticing any effect from xlabel style. In particular, I don't notice any effect from adding text width to my original example. I think maybe this analysis is a false positive. Do you know why align=center outside of xlabel style produces the desired result?
    – MattHusz
    Jul 30, 2020 at 5:04

You must log in to answer this question.

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