# Pgfplots: put plot legend on the outside margin?

I use classsicthesis style for my PhD. Since this style is somehow geared towards using the outside margin for additional notes and stuff, I thought I could also place the legends of my plots there. I use pgfplots to create them. I styled my plots so that the legend is placed outside the plot. The problem I don't know how to solve (and I really wonder if it can be done) is:

How can it be automatically determined if the plot is on the even or odd page so that the legend always appears on the outside margin?

At the moment I can only do this manually by placing the following two lines in the code of each plot:

 legend style={at={(-0.1,1)}, anchor=north east}, %for even pages
%legend style={at={( 1.1,1)}, anchor=north west}, %for odd pages


and (un)commenting the appropriate line - which, of course, is not practical at all. An automated solution would be great - otherwise it's just not worth it for a lengthy document with lots of plots.

P.S.: I am using TexLive 2011 on Ubuntu 11.10

EDIT: I thought of the question as kind of a general problem, but I was probably wrong, so I created the following MWE to illustrate it a help with the alternative answers:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% legend style={at={(-0.1,1)}, anchor=north east}, %for even pages
legend style={at={( 1.1,1)}, anchor=north west}, %for odd pages
]
\addplot coordinates { (0,0) (1,1) };
\addlegendentry{Legend entry}
\end{axis}
\end{tikzpicture}
\end{document}

-
Code snippets are useful to explaining things, but it is always best to compose a MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it, and so they can easily test the solution as well. – Peter Grill Nov 5 '11 at 17:39
This question is now answered, but as Peter says it would be good to include a minimal working example. That way alternative answers can be tested easily. – qubyte Nov 6 '11 at 0:18

## 2 Answers

Assuming that odd pages are always on one side and even on the other, you can add the package ifthen

\usepackage{ifthen}


and then use the page number to determine the side

\ifthenelse{\isodd{\value{page}}}{\def\eastwest{north east}}{\def\eastwest{north west}}


i.e. use that line just inside your figure environment if you're wrapping a plot in one. Now replace the line you have with

legend style={at={(-0.1,1)}, anchor=\eastwest},


I'm not sure how this will interact with floats, but I imagine it'll work.

-

This answer relies on the ifoddpage package. I put the yaxis label in the outside (or is it inside?) margin while the legend is on the opposite side. To create the mwe, I'll use the two following files (to make the code shorter). First the main one:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{float}
\usepackage{ifoddpage}

\begin{document}

\begin{figure}[H]
\input{figure}
\caption{first figure}
\end{figure}

\newpage

\begin{figure}[H]
\input{figure}
\caption{first figure}
\end{figure}

\end{document}


And the figure.tex file:

\checkoddpage
\ifoddpage
\def\eastweststyle{legend style={at={(1.05,0)}, anchor=north west},ylabel near ticks, yticklabel pos=left}
\else
\def\eastweststyle{legend style={at={(-0.05,0)}, anchor=north east}, ylabel near ticks, yticklabel pos=right}
\fi

\centering
\begin{tikzpicture}[trim axis left,trim axis right]

\begin{axis}[
\eastweststyle, %for odd pages
ylabel=y,
]
\addplot coordinates { (0,0) (1,1) };
\addlegendentry{Legend entry}
\end{axis}
\end{tikzpicture}


It works well but there remain some issues.

• I can't use the \checkoddpage and further in a new command or inside a pgfplotsstyle. If I do so, I get a missing endscname error. So I have to rewrite the couple of lines every time...
• To use it with groupplots, some changes are needed. See this question
-