2

I want the legend of my plot to display a fraction the same way it would be done using \displaystyle. When using \displaystyle, the border of the legend gets cut (see picture below). How can I proceed to nicely format my equation ? I have tried using \addlegendentryextended but it doesn't work.

Here is a MWE :

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[axis x line=center,
      axis y line=center,
        legend pos = north west,
        xlabel = $x$,
        ylabel = {$f(x)$},
        xmin=-5,
        xmax=5,
        ymin=-2,
        ymax=5,
    ]
    %Below the red parabola is defined
    \addplot [
        samples=100,
        color=blue,
    ]
    {e^x};
    \addlegendentry{$e^x$}

    %Here the blue parabloa is defined
    \addplot [
        domain=-4:5,
        samples=100,
        color=red,
        ]
        {1+x};
    \addlegendentry{$1+x$}

    \addplot [
        domain=-3:5,
        samples=100,
        color=orange,
    ]
    {1+ x + 1/2*x^2};
    \addlegendentry{$\displaystyle 1+ x +\frac{1}{2}x^2$} % <---- goes out of border

    \end{axis}
\end{tikzpicture}
\end{document}

Here is the output :

enter image description here

1
  • 1
    Just a correction: I think you have tried \addlegendentryexpanded not \addlegendentryextended, am I right?
    – Leone
    Mar 17, 2021 at 22:26

1 Answer 1

1

Update: I did some further research and found that this problem has already been solved here.


I've just faced the same problem and solved it using raisebox:

\raisebox{<vertical shift>}{<content>}

The drawback of doing this is the necessity of manually tuning the <vertical shift>

So, for your problem:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[axis x line=center,
      axis y line=center,
        legend pos = north west,
        xlabel = $x$,
        ylabel = {$f(x)$},
        xmin=-5,
        xmax=5,
        ymin=-2,
        ymax=5,
    ]
    %Below the red parabola is defined
    \addplot [
        samples=100,
        color=blue,
    ]
    {e^x};
    \addlegendentry{$e^x$}

    %Here the blue parabloa is defined
    \addplot [
        domain=-4:5,
        samples=100,
        color=red,
        ]
        {1+x};
    \addlegendentry{$1+x$}

    \addplot [
        domain=-3:5,
        samples=100,
        color=orange,
    ]
    {1+ x + 1/2*x^2};
    \addlegendentry{\raisebox{7pt}{$\displaystyle 1+ x +\frac{1}{2}x^2$}} % <---- goes out of border

    \end{axis}
\end{tikzpicture}
\end{document}

result

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