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 try make plot with long text in legend.

\documentclass{report}
\usepackage{pgfplots}
\pgfplotsset{
every axis legend/.append style={at={(0.5,-0.13)},anchor=north,legend cell align=left},
tick label style={font=\tiny\scriptsize},
legend style={font=\scriptsize},
axis lines=left,
legend style={draw=none},
}
\begin{document}
\begin{figure} [H]
\centering
\begin{tikzpicture}
\begin{axis} [
xlabel={x},
ylabel={y},
height=10cm,
width=17cm,
legend columns=2,
]
\addplot[color=red, mark=*] coordinates {
 (-3.79489, -0.794084) 
 (-3.54497, -0.919739) 
 (-3.51697, -0.93037) 
 (-3.3082, -0.986154) 
 (-0.165787, 0.986289) 
 (0.92704, 0.600204) 
};
\addlegendentry{Long long  long  long  long  long  long  long  long  long  long long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long text};

\addplot[color=blue, mark=*] coordinates {
 (-3.79489, 0.607808) 
 (-3.54497, 0.39253) 
 (-3.51697, 0.366622) 
 (-3.3082, 0.165834) 
 (-0.165787, -0.165029) 
 (0.92704, 0.799847) 
};
\addlegendentry{Plot 1};

\addplot[color=green, mark=*] coordinates {
 (-2.79489, 1.607808) 
 (-2.54497, 1.39253) 
 (-2.51697, 1.366622) 
 (-2.3082, 1.165834) 
 (-2.165787, -1.165029) 
 (1.92704, 1.799847) 
};

\addlegendentry{Plot 3};
\end{axis}
\end{tikzpicture}
\caption{Chart}
\end{figure}

\end{document}

But I see that:

enter image description here

If I use legend style={text width=5.5cm}, I see that:

enter image description here

Is it possible to wrap the text in the legend?

share|improve this question
    
On the case two, after adding text width=5.5cm, changing legend columns=2 into legend columns=-1 will draw all entries horizontally, hence solve your problem in this case. –  Jesse Jan 8 at 8:26
    
Unfortunately, this is only an example. In general, I do not know the length of each text in legend. And there may be many lines. –  Harrix Jan 8 at 16:38
    
We'd like to keep answers separate from questions, so you should write a separate answer instead of editing your answer into the question. Self-answers are perfectly admissible, and a well-written answer may earn you additional reputation. –  Jubobs Jan 9 at 15:08
    
Corrected. Added in separate answer! –  Harrix Jan 9 at 15:16

2 Answers 2

A legend is a node, You can specify the minimum height and text width etc:

 \documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
    compat=1.9,
    every axis legend/.append style={at={(0.5,-0.13)},anchor=north,legend cell align=left},
    tick label style={font=\tiny\scriptsize},
    legend style={draw=none,
              font=\scriptsize,
              text width=2in,           %% adjust
              minimum height=0.5in,     %% adjust
              %anchor=center,
              %cells={anchor=west},
            },
    %axis lines=left,
}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
xlabel={x},
ylabel={y},
height=10cm,
width=11cm,
legend columns=2,
]
\addplot[color=red, mark=*] coordinates {
 (-3.79489, -0.794084)
 (-3.54497, -0.919739)
 (-3.51697, -0.93037)
 (-3.3082, -0.986154)
 (-0.165787, 0.986289)
 (0.92704, 0.600204)
};
\addlegendentry{Long long  long  long  long  long  long  long  long  long  long long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long text};

\addplot[color=blue, mark=*] coordinates {
 (-3.79489, 0.607808)
 (-3.54497, 0.39253)
 (-3.51697, 0.366622)
 (-3.3082, 0.165834)
 (-0.165787, -0.165029)
 (0.92704, 0.799847)
};
\addlegendentry{Plot 1};

\addplot[color=green, mark=*] coordinates {
 (-2.79489, 1.607808)
 (-2.54497, 1.39253)
 (-2.51697, 1.366622)
 (-2.3082, 1.165834)
 (-2.165787, -1.165029)
 (1.92704, 1.799847)
};

\addlegendentry{Plot 3};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
But the distance increases for all elements. Сan I make that cell only stretched for long text? –  Harrix Jan 8 at 15:09
    
@Harrix I am not sure :-( –  Harish Kumar Jan 8 at 23:01
up vote 1 down vote accepted

Thank you! Your comments has helped to find a solution. I can use [minimum height=0.9in] for each label: \addlegendentry[minimum height=0.9in]{111}.

\documentclass{report}
\usepackage{pgfplots}
\pgfplotsset{
every axis legend/.append style={at={(0.5,-0.13)},anchor=north,legend cell align=left},
tick label style={font=\tiny\scriptsize},
legend style={font=\scriptsize},
axis lines=left,
legend style={draw=none},
}
\begin{document}
\begin{figure} [H]
\centering
\begin{tikzpicture}
\begin{axis} [
xlabel={x},
ylabel={y},
height=10cm,
width=17cm,
legend columns=2,
legend style={text width=5.5cm},
]
\addplot[color=red, mark=*] coordinates {
 (-3.79489, -0.794084) 
 (-3.54497, -0.919739) 
 (-3.51697, -0.93037) 
 (-3.3082, -0.986154) 
 (-0.165787, 0.986289) 
 (0.92704, 0.600204) 
};
\addlegendentry[minimum height=0.9in]{
Long long  long  long  long  long  long  long  long  long  long long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long  long text};

\addplot[color=blue, mark=*] coordinates {
 (-3.79489, 0.607808) 
 (-3.54497, 0.39253) 
 (-3.51697, 0.366622) 
 (-3.3082, 0.165834) 
 (-0.165787, -0.165029) 
 (0.92704, 0.799847) 
};
\addlegendentry{Plot 1};

\addplot[color=yellow, mark=*] coordinates {
 (-3., -0.607808) 
 (-3.97, 0.39253) 
 (-3.1697, 0.366622) 
 (-3.082, 0.165834) 
 (-0.65787, -0.165029) 
 (0.2704, 0.799847) 
};
\addlegendentry{Plot 1 4 4 6465 46 46 46};

\addplot[color=black, mark=*] coordinates {
 (-3.9489, 0.07808) 
 (-3.4497, 0.9253) 
 (-3.1697, 0.66622) 
 (-3.082, 0.65834) 
 (-0.65787, -0.65029) 
 (0.2704, 0.99847) 
};
\addlegendentry{Plot new test};

\addplot[color=green, mark=*] coordinates {
 (-3.9489, 0.47808) 
 (-3.4497, -0.3253) 
 (-3.1697, 0.36622) 
 (-3.082, 0.15834) 
 (-0.65787, -0.05029) 
 (0.2704, 0.09847) 
};
\addlegendentry{Plot new test};

\addlegendentry{Plot 3};
\end{axis}
\end{tikzpicture}
\caption{Chart}
\end{figure}

\end{document}

enter image description here

share|improve this answer

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.