0

How can I do so the size of the customlegend box adapts to its content?

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\usepackage{amsmath}
\usepackage{float}
\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}
    \newenvironment{customlegend}[1][]{%
        \begingroup
        % inits/clears the lists (which might be populated from previous
        % axes):
        \csname pgfplots@init@cleared@structures\endcsname
        \pgfplotsset{#1}%
    }{%
        % draws the legend:
        \csname pgfplots@createlegend\endcsname
        \endgroup
    }
    \def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}
%%--------------------------------
\begin{axis}[
width=5cm, height=5cm,
xmin=0, xmax=1,
ymin=0, ymax=1.3, 
xtick=\empty, ytick=\empty,
axis lines=middle,
x label style={at={(1,0)},right},
xlabel={$X_\mathrm{A}$}, ylabel={$-\dfrac{1}{r_\mathrm{A}}$},
restrict y to domain=0:1,
ylabel style={at={(ticklabel cs:0.75)},xshift=-1.25cm},
clip=false
]
\node[anchor=east] (zero) at (axis cs:0,0) {0};             
\addplot [name path=V,red, very thick, samples=100, smooth,domain=0:1]
{ (1/3) * ((1)/(1-x)) };
\path[name path=axis] (axis cs:0,0) -- (axis cs:0.6,0);

\addplot [
    thick,
    color=blue,
    fill=blue, 
    fill opacity=0.1
]
fill between[
    of=V and axis,
    soft clip={domain = 0:0.6},
];
\node[anchor=east] (zero) at (axis cs:0,0) {0};

\draw[dotted] (axis cs:0.6,0) --++ (0,-0.3) node[anchor=north] (Xaf) {$X_\mathrm{A,finale}$};  
\end{axis}
\begin{customlegend}[
    legend cell align=left,
    legend entries={ $\dfrac{t}{c_\mathrm{A,0}}$ },
    legend style={at={(6,3)},font=\footnotesize}]
    \addlegendimage{legend image code/.code={
    \node [color=white, draw, rectangle, minimum size=1.5em, fill=blue, fill opacity=0.1,anchor=15] {};}
    }
\end{customlegend}
\end{tikzpicture}

\end{figure} \end{document}

enter image description here

Or, is there an easier way to get the same kind of legend?

user3713179
  • 635
  • 2
  • 11

2 Answers2

1

In my opinion, creating custom legends, axis and tick labels should be a last resort when using a high level package such as pgfplots. There are several well documented features in the manual that allow you to achieve the results you desire, including a plot legend which may be customized. One problem others noticed is that when using display style maths in a legend cell, you may need to introduce some artifical white space using \raisebox{<vertical shift>}{<content>}, as suggested in this question.

\documentclass[tikz]{standalone}

\usepackage{amsmath} \usepackage{pgfplots} \pgfplotsset{compat=1.18}

\begin{document} \begin{tikzpicture} \begin{axis}[ width=5cm, height=5cm, xmin=0, xmax=1, ymin=0, ymax=1.3, xlabel={$X_\text{A}$}, ylabel={$-\frac{1}{r_\text{A}}$}, xlabel style={at={(rel axis cs:1,0)}, right}, ylabel style={at={(rel axis cs:0,0.75)}, rotate=-90}, axis lines=left, major y tick style={draw=none}, xtick=\empty, ytick={0}, clip=false, legend pos=outer north east, legend cell align=left, extra x ticks={0.6}, extra x tick style={tick align=outside,tick style={black,dotted},tickwidth=0.5cm}, extra x tick labels={$X_\text{A,finale}$}, ]

\addplot [domain=0:0.6, draw opacity=0, fill=blue, fill opacity=0.1, area legend] {1/3*1/(1-x)} \closedcycle; \addlegendentry{\raisebox{0.5ex}{$\frac{t}{c_\text{A,0}}$}}

\addplot [restrict y to domain=0:1, red, very thick, domain=0:1, forget plot] {1/3*1/(1-x)};

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

Output

Rastow
  • 43
  • 7
0

legend style={nodes={text depth=3mm}} does the trick for me:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\usepackage{amsmath}
\usepackage{float}
\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}
    \newenvironment{customlegend}[1][]{%
        \begingroup
        % inits/clears the lists (which might be populated from previous
        % axes):
        \csname pgfplots@init@cleared@structures\endcsname
        \pgfplotsset{#1}%
    }{%
        % draws the legend:
        \csname pgfplots@createlegend\endcsname
        \endgroup
    }
    \def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}
%%--------------------------------
\pgfmathsetmacro{\k}{5};
\pgfmathsetmacro{\A}{1};
\begin{axis}[
width=5cm, height=5cm,
xmin=0, xmax=1,
ymin=0, ymax=1.3, 
xtick=\empty, ytick=\empty,
axis lines=middle,
x label style={at={(1,0)},right},
xlabel={$X_\mathrm{A}$}, ylabel={$-\dfrac{1}{r_\mathrm{A}}$},
restrict y to domain=0:1,
ylabel style={at={(ticklabel cs:0.75)},xshift=-1.25cm},
clip=false
]
\node[anchor=east] (zero) at (axis cs:0,0) {0};             
\addplot [name path=V,red, very thick, samples=100, smooth,domain=0:1]
{ (1/3) * ((1)/(1-x)) };
\path[name path=axis] (axis cs:0,0) -- (axis cs:0.6,0);

\addplot [
    thick,
    color=blue,
    fill=blue, 
    fill opacity=0.1
]
fill between[
    of=V and axis,
    soft clip={domain = 0:0.6},
];
\node[anchor=east] (zero) at (axis cs:0,0) {0};

\draw[dotted] (axis cs:0.6,0) --++ (0,-0.3) node[anchor=north] (Xaf) {$X_\mathrm{A,finale}$};  
\end{axis}
\begin{customlegend}[
    legend cell align=left,
    legend entries={ $\dfrac{t}{c_\mathrm{A,0}}$ },
    legend style={
        nodes={text depth=3mm}, % ‹— this
        at={(6,3)},
        font=\footnotesize}
    ]
    \addlegendimage{legend image code/.code={
    \node [color=white, draw, rectangle, minimum size=1em, fill=blue, fill opacity=0.1,anchor=east] {};}
    }
\end{customlegend}
\end{tikzpicture}

\end{figure}

\end{document}

enter image description here

user3713179
  • 635
  • 2
  • 11