0

It seems that fractions are placed too low within the legend in pgfplot. In my opinion, the fraction is placed a bit too low, therefore, the distance to the upper legend entry is too wide and to the lower, it is too narrow.

E.g. in this MWE:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture} 
\begin{axis}[domain=1:2]
  \addplot {x^2 };\addlegendentry{$x^2$}; 
  \addplot {1/x};\addlegendentry{$\frac{1}{x}$}; 
  \addplot {x};\addlegendentry{$x$}; 
\end{axis}
\end{tikzpicture}
\end{document}

which results in:enter image description here

Is this a bug? And is it possible to adjust the spacings?

sistlind
  • 723
  • See also https://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture/148855?s=1|2.5773#148855 – John Kormylo Jun 18 '17 at 13:52
  • It is also possible to use an optional argument for addlegendentry; See also https://tex.stackexchange.com/a/158892/104637 – sistlind Jun 25 '17 at 10:43

1 Answers1

1

It appears that the text is aligned from the top down rather than from the baseline, and the image is placed independently (ignoring the baseline). One can compensate using the optional arguments of \raisebox to change the height. The value needed appears to be twice the default inner sep.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture} 
\begin{axis}[domain=1:2]
  \addplot {x^2 };\addlegendentry{$x^2\mathstrut$}; 
  \addplot {1/x};\addlegendentry{\raisebox{0pt}[.666em]{$\frac{1}{x}$}}; 
  \addplot {x};\addlegendentry{$x\mathstrut$}; 
\end{axis}
\end{tikzpicture}
\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120