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.

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

share|improve this question
    
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 9 '13 at 1:02
add comment

1 Answer

up vote 6 down vote accepted

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

share|improve this answer
add comment

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.