1

I'm using pgfplots, but i want to have my legends next to the plot.

This is easy by setting legend style and anchor. But i also want the plot to be as big as possible.

So if my labels are to long, they will not fit in the document.

See following code and plot:

\documentclass[a4paper,11pt]{article}
\usepackage{pgfplots}

\pgfplotsset{grid=both,width=\linewidth}    
\begin{document}

\begin{tikzpicture}
\begin{axis}[legend style={at={(1.02,1)},anchor=north west}]
  \addplot {x};
  \addlegendentry{long long long long long long}
  \addplot {x*2};
  \addlegendentry{not long}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

EDIT:

I have found a solution! there is a usepackage called tikzscale which makes it possible to use includegraphics with tikz. The usepackage need to be instaled first though, which I did through MikTex package manager

Here is an example on how it works:

first you make your plots in .tikz files:

temp.tikz

\begin{tikzpicture}
\begin{axis}[legend style={at={(1.02,1)},anchor=north west}]
  \addplot {x};
  \addlegendentry{short short}
  \addplot {x*2};
  \addlegendentry{not long}
\end{axis}
\end{tikzpicture}

temp2.tikz

\begin{tikzpicture}
\begin{axis}[legend style={at={(1.02,1)},anchor=north west}]
  \addplot {x};
  \addlegendentry{long long long long long long}
  \addplot {x*2};
  \addlegendentry{not long}
\end{axis}
\end{tikzpicture}

then include them to your .tex:

\documentclass[a4paper,11pt]{article}
\usepackage{pgfplots}
\usepackage{tikzscale}

\pgfplotsset{grid=both} 
\begin{document}

\includegraphics[width=\linewidth,height=10cm]{temp.tikz}

\includegraphics[width=\linewidth,height=10cm]{temp2.tikz}

\end{document}

which gives following result: enter image description here

Rasmus
  • 129
  • So how do you want the page to look like? Obviously, tikz cannot buy you larger paper... – jarauh Nov 27 '15 at 19:46
  • First, use \noindent. Second, try \resizebox{\textwidth}{!}{...} – John Kormylo Nov 28 '15 at 04:13
  • @jarauh I really don't have many demand on the layout of this document. It just need to fit in a A4 paper.Two possible ways i can think of is: Scaling the with of the pgfplot depending of the length of the labels. Or somehow making the legend show single labels in multiple lines – Rasmus Nov 30 '15 at 08:29
  • @JohnKormylo \noindent did shift the plot a little bit to the left, but isn't enough. And though \resizebox do the job, it scales everything, including the text, which doesn't look nice. – Rasmus Nov 30 '15 at 08:39
  • Alas \addlegendentry{\parbox ...} overlaps. However, you can create your own legend. See http://tex.stackexchange.com/questions/278530/how-i-can-customize-a-legend-on-pgfplots/278538?s=2|0.0000#278538 – John Kormylo Nov 30 '15 at 15:53
  • 4
    I'm voting to close this question because OP has answered the question himself in the edit of the question. – Stefan Pinnow Jun 06 '17 at 20:56

0 Answers0