[Tex/LaTex] How to adjust the horizontal spacing between legend entries in PGFPlots

legendpgfplotsspacing

I am using legend columns=-1 to make a horizontal legend. However, the distance between the individual legend entries is too small for my taste. Is there a straightforward way to increase the horizontal spacing between individual legend entries?

Best Answer

The legend is a TikZ matrix, so you can use the styles that apply to general matrices to influence the appearance.

In the legend matrix, the sample image and the entry text each occupy their own cell, so we have to increase the spacing for every second cell. For this, we can use the every even column style: By setting the column sep value of the matrix to a larger value for every second column, the horizontal spacing between the legend entries is increased without changing the distance between the image and the text.

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend columns=-1,
legend entries={Long plot title, B, C},
legend style={/tikz/every even column/.append style={column sep=0.5cm}}]

\addplot{-x^3};
\addplot{-35*x^3};
\addplot{-6*x^3};

\end{axis}

\end{tikzpicture}
\end{document}

Spacing between columns in pgfplots legend

To adjust the spacing between rows of entries, we don't need to jump through the every even column/.append style hoop. Instead, we can just say legend style={row sep=0.5cm}.