0

I have a simple task, which I couldn't find a solution for so far. I have a plot with 3 legend entries, 2 short ones, and a long one. I can use legend columns=2, to get a two-column legend but it looks like this:

aaaa ________________ bbbb

ccccccccccccccccccccc

What I like to have is:

aaaa ________ bbbb

ccccccccccccccccccccccccc

(I put the "_" for white space )

So, something like a multicolumn for a table used in a legend. Do you have any idea how to do that?

Thanks, Axel

Added at 12.04.21:

Sorry for not adding the mentioned minimal example: I prepared it as follows:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{graphicx} \usepackage{xcolor} \usepackage{tikz} \usetikzlibrary{decorations, shapes, calc, backgrounds, shadings, arrows.meta, trees, positioning} \usepackage{pgfplots} \pgfplotsset{compat=newest}

\begin{document}

\begin{figure} \centering \begin{tikzpicture} \begin{axis}[% width=.7\textwidth, scale only axis, clip = false, legend columns=2, legend style={ at={(axis cs:1.5,1.2)}, /tikz/column 2/.style={ column sep=5pt}, anchor=south}, ] \addlegendimage{area legend,fill=red, opacity = 0.5} \addlegendimage{area legend,fill=gray, opacity = 0.5} \addlegendimage{area legend,fill=green, opacity = 0.5}

    \addplot[area legend, draw=black, fill=gray, fill opacity=0.5, forget plot]
    table[row sep=crcr] {%
    x   y\\
    1 0\\
    2 0\\
    2 1\\
    1 1\\
    }--cycle;
    \node[align=center]
    at (axis cs:1.5,0.5) {a};

    \addplot[area legend, draw=black, fill=red, fill opacity=0.5, forget plot]
    table[row sep=crcr] {%
    x   y\\
    0 0\\
    1 0\\
    1 1\\
    0 1\\
    }--cycle;
    \node[align=center]
    at (axis cs:0.5,0.5) {b};
    \addplot[area legend, draw=black, fill=green, fill opacity=0.5, forget plot]
    table[row sep=crcr] {%
    x   y\\
    2 0\\
    3 0\\
    3 1\\
    2 1\\
    }--cycle;
    \node[align=center]
    at (axis cs:2.5,0.5) {c};
    \legend{short text 1, short text 2, looooooooooooooooooooooooooooooooooooooooog text};  
    \end{axis}
\end{tikzpicture}
\caption{Caption}
\label{fig:my_label}

\end{figure} \end{document}

Axel
  • 13
  • 2
    Welcome. Can you provide an example, where you show us your code of the things you tried so far? – Excelsior Apr 10 '21 at 18:16
  • If you have defined the legendentries this answer (https://tex.stackexchange.com/questions/488793/pgfplots-legend-with-multiple-columns-and-rows-different-entry-widths?rq=1) could be helpful. – Excelsior Apr 10 '21 at 18:27
  • 4
    Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Rmano Apr 10 '21 at 18:34

1 Answers1

0

I had a look at your example and in cases like this I personaly build my own legend with the matrix option in \usetikzlibrary. As mentioned here for long entries you can use nodes={text width=width("the longest legend entry")} to set the width of the node (in this case the legend). For the space between entry 1 and 2, I added \hspace{length}, which you can modify for your needs.

enter image description here

\documentclass[border=5pt]{standalone}

\usepackage{pgfplots} \pgfplotsset{compat=newest} \usetikzlibrary{matrix} % <- added for self made legend

\begin{document}

\begin{tikzpicture}
    \begin{axis}[%
        name=plot,
        width=.7\textwidth,
        scale only axis,
        clip = false,
        legend columns=2,
        legend style={  at={(axis cs:1.5,1.2)},
            /tikz/column 2/.style={
                column sep=5pt},
            anchor=south}, 
        ]

        \addplot[area legend, draw=black, fill=gray, fill opacity=0.5, forget plot]
        table[row sep=crcr] {%
            x   y\\
            1 0\\
            2 0\\
            2 1\\
            1 1\\
        }--cycle;
        \node[align=center]
        at (axis cs:1.5,0.5) {a};

        \label{plot:a}

        \addplot[area legend, draw=black, fill=red, fill opacity=0.5, forget plot]
        table[row sep=crcr] {%
            x   y\\
            0 0\\
            1 0\\
            1 1\\
            0 1\\
        }--cycle;
        \node[align=center]
        at (axis cs:0.5,0.5) {b};

        \label{plot:b}

        \addplot[area legend, draw=black, fill=green, fill opacity=0.5, forget plot]
        table[row sep=crcr] {%
            x   y\\
            2 0\\
            3 0\\
            3 1\\
            2 1\\
        }--cycle;
        \node[align=center]
        at (axis cs:2.5,0.5) {c};

        \label{plot:c}

    \coordinate (legend) at (axis cs:1.5,1.2);

    \end{axis}

    \matrix[
    draw,
    matrix of nodes,
    anchor=south,
    font=\footnotesize,
    nodes={text width=width(&quot;looooooooooooooooooooooooooooooooooooooooog text&quot;)},
    ] at (legend) {
        \ref{plot:a} short text 1 \hspace{1cm} \ref{plot:b}  short text 2 \\
        \ref{plot:c} looooooooooooooooooooooooooooooooooooooooog text \\
    };

\end{tikzpicture}

\end{document}

Excelsior
  • 2,322