Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
\usetikzlibrary{shadows}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  small,
  legend style={
    rounded corners=3pt, legend pos=north west,
    drop shadow={fill=black, opacity=0.5, shadow xshift=2pt, shadow
      yshift=-2pt}
    }
    ]
  \addplot[thick, blue]{x};
  \addplot[thick, red]{2*x};
  \legend{$x$,$2x$}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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}
\usetikzlibrary{shadows,shadows.blur}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  small,
  legend pos=north west,
  legend style={rounded corners=3pt,
    append after command={
    \pgfextra{
      \draw[rounded corners=3pt,
      drop shadow={fill=black, opacity=0.5, shadow xshift=2pt, shadow yshift=-2pt}]
        (\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
      }
      }
  },
  ]
  \addplot[thick, blue]{x};
  \addplot[thick, red]{2*x};
  \legend{$x$,$2x$}
\end{axis}
\end{tikzpicture}

\end{document}

The result:

enter image description here

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,
          drop shadow={fill=black, opacity=0.5, shadow xshift=2pt, shadow yshift=-2pt}]
            (\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
          }
       }

The previous MWE, now becomes:

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

\usepackage{pgfplots}
\usetikzlibrary{shadows}


\tikzset{
  legend shadow/.style 2 args={% #1 path options, #2 shadow options 
  append after command={
    \pgfextra{
      \draw[#1,drop shadow={#2}] 
       (\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
      }
    }
  },
  use legend shadowed/.style={legend shadow={path options}{shadow options}},
  path options/.style={},
  shadow options/.style={},
  legend path options/.code={
    \tikzset{path options/.append style={#1}}
  },
  legend shadow options/.code={
    \tikzset{shadow options/.append style={#1}}
  },
}

\begin{document}

\begin{tikzpicture}[
  legend path options={rounded corners=3pt},
  legend shadow options={black,
    opacity=0.5,
    shadow xshift=2pt, 
    shadow yshift=-2pt
  }
]
\begin{axis}[
  small,
  legend pos=north west,
  legend style={rounded corners=3pt,
    use legend shadowed,
  },
  legend entries={$x$,$2x$} 
  ]
  \addplot[thick, blue,mark=*]{x};
  \addplot[thick, red]{2*x};
  \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,
    use legend shadowed,
    legend path options={rounded corners=3pt},
    legend shadow options={black,
      opacity=0.5,
      shadow xshift=2pt, 
      shadow yshift=-2pt
    }
  },
  legend entries={$x$,$2x$} 
  ]
share|improve this answer
    
Nice. Learned something new again :) –  Philipp Aug 28 '13 at 14:15
    
@Philipp: you're welcome :) –  Claudio Fiandrino Aug 28 '13 at 14:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.