# How to get the drop shadow for a pgfplots legend right?

When using drop shadows in legend style of a plot created with pgfplots not only the legend box but also the lines that are part of the legend images "cast a shadow" in the final picture. Since markers that are part of the legend images don't "cast a shadow" I'd say that this is not the intended behavior. Is there a way to get rid of the line shadows within the legend box?

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
small,
legend style={
rounded corners=3pt, legend pos=north west,
yshift=-2pt}
}
]
\legend{$x$,$2x$}
\end{axis}
\end{tikzpicture}

\end{document}


-

The problem, I think, lies in the fact that the shadow is automatically inherited by the legend images.

To cope with that, it is possible to exploit the late options like append after command.

Here is an example:

\documentclass[border=10pt,png]{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
small,
legend pos=north west,
legend style={rounded corners=3pt,
append after command={
\pgfextra{
\draw[rounded corners=3pt,
(\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
}
}
},
]
\legend{$x$,$2x$}
\end{axis}
\end{tikzpicture}

\end{document}


The result:

Code improvement

In order to have a more automatic way to apply a shadow to the legend, here are a bunch of keys/styles; they permit to specify:

• the legend path options like rounded corners or any other option that applies to a path;
• the legend shadow options, namely the specific options that concern only the shadow
• the use legend shadowed that is simply a shortcut for the piece of code:
      append after command={
\pgfextra{
\draw[rounded corners=3pt,
(\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
}
}


The previous MWE, now becomes:

\documentclass[border=10pt,png,tikz]{standalone}

\usepackage{pgfplots}

\tikzset{
append after command={
\pgfextra{
(\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
}
}
},
path options/.style={},
legend path options/.code={
\tikzset{path options/.append style={#1}}
},
},
}

\begin{document}

\begin{tikzpicture}[
legend path options={rounded corners=3pt},
opacity=0.5,
}
]
\begin{axis}[
small,
legend pos=north west,
legend style={rounded corners=3pt,
},
legend entries={$x$,$2x$}
]
\legend{$x$,$2x$}
\end{axis}
\end{tikzpicture}

\end{document}


### Disclaimer

The keys legend path options and legend shadow options, for simplicity, have been put under the path /tikz/. This means that they can not be used within legend style like:

\begin{axis}[
small,
legend pos=north west,
legend style={rounded corners=3pt,
legend entries={$x$,$2x$}