[Tex/LaTex] Pgfplots: put plot legend on the outside margin

classicthesislegendmarginspgfplots

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}

Best Answer

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.