1

I would like to use pgfplots' "legend to name" feature to reference the (named) legend in the caption of the respective figure environment. Additionally, it would be nice to be able to put the legend inline with text, as well.

It works, generally speaking, but the vertical alignment is off in both cases.

I had a look at (amongst others) vertical alignment of tikzpicture with text and TikZ vertical alignment of circled text and the answers didn't really solve the problem.

This is an MWE where the alignment is off:

\documentclass{article}
\usepackage{lipsum}

\usepackage{pgfplots}
\pgfplotsset{
    width=7cm,
    compat=1.18,
    footnotesize,
    samples=10,
    every legend to name picture/.style={
        % baseline={([yshift=+0.55em]current bounding box.south)}
        % baseline={[yshift=+1.65em](current bounding box.base)}
    },
}

\begin{document}

\lipsum[1-2]\ref{named}
\lipsum[3]

\begin{figure}[b]
%
\begin{tikzpicture}
\begin{axis}[
legend columns=-1,
legend entries={$(x+0)^k$;,$(x+1)^k$},
legend to name=named,
every axis legend/.append style={
    draw=none,
},
]
\addplot {x};
\addplot {x+1};
\end{axis}
\end{tikzpicture}
%
\caption{Test: \ref{named}}
\end{figure}

\end{document}

Legend in caption.

Legend inline with text.

I tried some settings including both commented out using baseline. But manually setting the "correct" yshift seems a bit "hacky".

I hope someone can help me here.

3
  • 1
    Welcome to TeX.SX! baseline={([yshift=-.5ex]current bounding box.center)} would maybe less hacky. It takes the center point of the legend picture and aligns it vertically to the middle of the x-height of the surrounding text, that is, more or less at the height of a hyphen. The text inside the legend picture is still not exactly aligned to the text surrounding it, of course. Jul 15 at 9:02
  • I browsed the PGF manual, but I could not find a way to catch the nodes that contain the text inside the legend. The idea would be to align the whole thing to the base of one of these nodes. Jul 15 at 9:18
  • A node was what I was looking for as well. But to no avail. The yshift=-.5ex looks fine for now but is obviously not perfect. Additionally, I tried the adjustbox package. But the alignment using valign doesn't produce any better results. And thank you very much for your effort. Jul 15 at 9:31

1 Answer 1

1

I found that you can name the legend with legend style={name=leg} (or whatever name you like), which allows you to align its inline representation to its own base using every legend to name picture/.style={baseline={(leg.base)}}. You can also align the nodes inside the legend (which are positioned in a matrix) to their base using legend style={nodes={anchor=base}}. So, the trick would be to essentially align everything to the same baseline. This would result in something like the following:

enter image description here

Now, the next step would be to un-align the legend pictures or to shift them a bit upwards. This can be done using legend image post style={yshift=.5ex} for example. The complete code of this approach would be:

\documentclass{article}
\usepackage{lipsum}

\usepackage{pgfplots}
\pgfplotsset{
    width=7cm,
    compat=1.18,
    footnotesize,
    samples=10,
    legend style={
        nodes={anchor=base},
        name=leg,
    },
    legend image post style={yshift=.5ex},
    every legend to name picture/.style={
        baseline={(leg.base)},
    },
}

\begin{document}

\lipsum[1-2]\ref{named}
\lipsum[3]

\begin{figure}[b]
%
\begin{tikzpicture}
\begin{axis}[
legend columns=-1,
legend entries={$(x+0)^k$;,$(x+1)^k$},
legend to name=named,
every axis legend/.append style={
    draw=none,
},
]
\addplot {x};
\addplot {x+1};
\end{axis}
\end{tikzpicture}
%
\caption{Test: \ref{named}}
\end{figure}

\end{document}

enter image description here

enter image description here

4
  • I would change legend style to legend style={ nodes={anchor=west}, % <--- name=leg},
    – Zarko
    Jul 15 at 10:07
  • With legend style={nodes={anchor=west}}, the nodes would not be aligned to the baseline any more. There was a mistake in my code anyways: it should be legend image post style={yshift=.5ex} and not every legend image post style={yshift=.5ex}. Jul 15 at 10:12
  • 1
    This is quite exactly what I was looking for. Thank you very much for the help. I marked your answer as accepted. Jul 15 at 12:25
  • 1
    I just wanted to add a small remark to the solution given in this answer. Depending on where the legend description should be aligned, anchor=base should be changed to e.g. anchor=base west for aligning the text left. The effect only shows when the legend has multiple rows. So for legends with only one line this doesn't have any effect. In the end both comments above are correct. Jul 22 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.