2

I'm using the legend-only solution from Using a pgfplots-style legend in a plain-old tikzpicture

How can I get a style for a single row? I'm trying to have row sep = 5pt for example for all rows, but between the second and third row have a different value. I've tried

legend style={row sep = 5pt,
    /tikz/row 2/.style={row sep = 10pt}
}

This compiles fine and adds a 5pt separation to all rows, but it doesn't change the separation after the second row.

Thanks!

EDIT

Here's a MWE. I've simple copied the answer in the link and added the code above:

\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 style={row sep = 5pt,
        /tikz/row 2/.style={row sep = 10pt}
    },
    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}
bjorne
  • 637
  • I don't understand what you are asking. If you want to set a style for a single row, then you want that style to apply to that row and just that row. So you don't want it to apply to any other rows. But that is what you say the problem is. Also, please remember that questions should include a complete minimal example and not just a code fragment, which is generally not very useful. – cfr May 25 '16 at 17:56
  • @cfr, the MWE is the same as the linked answer. What I'm looking for is a single row sep for every row except one, which I would like to be different. I am trying to set it to be different for a single row, but don't know how. The problem is that what I wrote doesn't work - it compiles without complaining, but leaves all the rows with the same row sep. – bjorne May 25 '16 at 18:01
  • You didn't link to an answer. You linked to a question with multiple answers. But thank you for adding the example here. – cfr May 25 '16 at 18:12
  • row sep is not a documented key for legend style as far as I can tell. The pgfplots manual only seems to mention it for table. Why do you think this should work? Does it work in legends for regular pgfplots plots? – cfr May 25 '16 at 20:35
  • @cfr See 4.9.5 "Legend Appearance", p. 237, pgfplots manual v1.13: The legend is a TikZ-matrix, so one can use any TikZ option which affects nodes and matrices... – Paul Gaborit May 25 '16 at 21:07
  • @PaulGaborit Thanks. I was actually writing an answer including that information while you were commenting, I think. – cfr May 25 '16 at 21:08

2 Answers2

2

row sep is passed as an option to the TikZ matrix which pgfplots uses to create the legend. This works fine, I think. However, it is an option which applies to rows and not to cells, whereas row <number> affects the style of cells. So setting row sep in the style row 2 does nothing because individual cells aren't affected by this option at all.

You can approximate the effect by shifting all cells in a row using yshift, for example. Because yshift applies to cells, setting this in row 2's style options does work.

For example (exaggerated for purposes of illustration):

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{pgfplots}
% code from Christian Feuersänger's answer at http://tex.stackexchange.com/a/54834/
% 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}
\begin{tikzpicture}
  \begin{customlegend}
    [
      legend style={%
        row sep = 5pt,
        /tikz/row 2/.style={yshift = 15pt},
      },
      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}

shifted cells in a row

cfr
  • 198,882
2

AFAIK changing row sep for a single row does not work for a normal TikZ matrix. In such a normal matrix you have to use the optional argument of \\ at the end of the row to increase or decrease the row sep.

To enlarge the row sep you can insert an emty line in the legend:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\newenvironment{customlegend}[1][]{%
    \begingroup
    \csname pgfplots@init@cleared@structures\endcsname
    \pgfplotsset{#1}%
}{%
    \csname pgfplots@createlegend\endcsname
    \endgroup
}%
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

\begin{document}
\begin{tikzpicture}
    \begin{customlegend}[legend style={row sep = 5pt,
        %nodes=draw
    },
    legend entries={$a$,$e^x$,{[inner sep=0pt]},C,$d$}]
    \addlegendimage{red,fill=black!50!red,area legend}
    \addlegendimage{red,fill=black!50!red,sharp plot}
    \addlegendimage{empty legend}
    \addlegendimage{red,fill=black!50!red,mark=*,sharp plot}
    \addlegendimage{red,fill=black!50!red,ybar,ybar legend}
    \end{customlegend}
\end{tikzpicture}
\end{document}

enter image description here

or you can insert an invisible node with an invisible row in the cell of the legend entry:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\newenvironment{customlegend}[1][]{%
    \begingroup
    \csname pgfplots@init@cleared@structures\endcsname
    \pgfplotsset{#1}%
}{%
    \csname pgfplots@createlegend\endcsname
    \endgroup
}%
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

\begin{document}
\begin{tikzpicture}
    \begin{customlegend}[legend style={row sep = 5pt,
        row 2 column 2/.style={
            execute at end cell={
                \node[anchor=north,yshift=-\dp\strutbox,inner sep=0pt,draw=none,fill=none]{\rule{0pt}{5pt}};
        }},
        %nodes=draw
    },
    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}

enter image description here

esdd
  • 85,675
  • +1. Thanks. I've accepted the other answer because it's the simplest IMO, but I like these ideas! – bjorne May 26 '16 at 09:09