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}
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.





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. – Jasper Habicht Jul 15 '22 at 09:02The 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.
– Hannes Börngen Jul 15 '22 at 09:31