13

I found the following code to manually draw legend, but it produces vertical(column) legend. How to draw it horizontally? I don't want surrounding rectangle also.

\documentclass{article}

\usepackage{pgfplots}

% argument #1: any options
\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
}%

% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

\begin{document}

\thispagestyle{empty}

\begin{tikzpicture}
    \begin{customlegend}[legend entries={$a$,$e^x$,C,$d$}]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}
    \end{customlegend}
\end{tikzpicture}

\end{document} 

Output:

enter image description here

tikzlearner
  • 4,527
  • It is tempting to work on a solution using the automatic way of drawing legends with pgfplots. Maybe you should give some more information, to understand why this (simpler) method doesn't fit your needs, details aside. – T. Verron Mar 09 '13 at 01:02

1 Answers1

19

Originally, you could use legend style={draw=none, legend columns=-1}, which would draw the legend horizontally and doesn’t have a box around it.

The draw=none part works, the legend columns key does not work in the legend style style anymore. But it does outside.

Ergo:

\begin{customlegend}[
  legend columns=-1,
  legend style={
    draw=none,
    column sep=1ex,
  },
  legend entries={$a$,$e^x$,C,$d$}
]

I also have used \makeatletter and \makeatother so you can use @ as a letter instead of using \csname. (See What do \makeatletter and \makeatother do?)

Code

\documentclass[tikz]{standalone}

\usepackage{pgfplots}

% argument #1: any options
\makeatletter
\newenvironment{customlegend}[1][]{%
    \begingroup
    % inits/clears the lists (which might be populated from previous
    % axes):
    \pgfplots@init@cleared@structures
    \pgfplotsset{#1}%
}{%
    % draws the legend:
    \pgfplots@createlegend
    \endgroup
}%

% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\pgfplots@addlegendimage}
\makeatother
\begin{document}
\begin{tikzpicture}
    \begin{customlegend}[legend columns=-1,legend style={draw=none,column sep=1ex},legend entries={$a$,$e^x$,C,$d$}]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}
    \end{customlegend}
\end{tikzpicture}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821