# Extending figures into the margin on even vs. odd pages

I am using KOMA script for my document. Occasionally, I have a float that is wider than text width, and I want the float to be pushed into the outer margin. This is why I defined \widefloat command, that checks if page is odd using command \ifthispageodd and depending on the results pushes the float left or right. This works fine when float is in the middle of the page. However, when float is thrown into the next page, \ifthispageodd gives wrong result. Could you please tell me how to fix this up?

MWE: the first float is correctly pushed into outer margin, the second is pushed into inner margin.

\documentclass[11pt]{scrbook}
\usepackage{tikz,lipsum}

\newcommand{\widefloat}[1]{%
\ifthispageodd{\makebox[\textwidth][l]{#1}}{\makebox[\textwidth][r]{#1}}}

\begin{document}

\lipsum[1]

\widefloat{
\begin{tikzpicture}
\draw (0,0) rectangle (16cm,10cm);
\end{tikzpicture}}

\lipsum[3]

\widefloat{
\begin{tikzpicture}
\draw (0,0) rectangle (16cm,10cm);
\end{tikzpicture}}

\lipsum[2]

\end{document}


The problem arises because the \ifthispageodd first writes a \label to the .aux file and then (essentially) does a \pageref of the label to get the current page. Unfortunately, the label and the reference can get separated from each other and from the figure you are placing. One way to prevent this is to wrap the whole thing in an \mbox:

\newcommand{\widefloat}[1]{%
\mbox{%
\ifthispageodd%
{%
\makebox[\textwidth][l]{#1}%
}{%
\makebox[\textwidth][r]{#1}
}
}%
}


You might also want to put \noindent in front of \mbox to get proper margin alignment.

• I wonder... which solution is in your opinion more economical in terms of resources and compiling times? – Pygmalion Oct 27 '13 at 7:50
• @Pygmalion Well, adding a box around a figure should add only microscopicly to the time and memory used. On the other hand, the calc package doesn't add a great deal compared to what LaTex consumes all by itself. – Dan Nov 8 '13 at 6:34

This happens because when the second \widefloat is issued, the page is still odd...

Define it as

\newcommand{\widefloat}[1]{%
\setlength{\alength}{\textheight-\pagetotal}%
\setlength{\blength}{\totalheightof{\parbox{\linewidth}{#1}}}%
\ifdim\alength<\blength\clearpage\fi%
\ifthispageodd{\noindent\makebox[\textwidth][l]{#1}}{\noindent\makebox[\textwidth][r]{#1}}%
}


\usepackage{calc}

\newlength{\alength}
\newlength{\blength}


\documentclass[11pt]{scrbook}
\usepackage{tikz,lipsum}
\usepackage{calc}

\newlength{\alength}
\newlength{\blength}

\newcommand{\widefloat}[1]{%
\setlength{\alength}{\textheight-\pagetotal}%
\setlength{\blength}{\totalheightof{\parbox{\linewidth}{#1}}}%
\ifdim\alength<\blength\clearpage\fi%
\ifthispageodd{\noindent\makebox[\textwidth][l]{#1}}{\noindent\makebox[\textwidth][r]{#1}}%
}

\begin{document}

\lipsum[1]

\widefloat{%
\begin{tikzpicture}
\draw (0,0) rectangle (16cm,10cm);
\end{tikzpicture}}

\lipsum[3]

\widefloat{%
\begin{tikzpicture}
\draw (0,0) rectangle (16cm,10cm);
\end{tikzpicture}}

\lipsum[2]

\end{document}


Output:

• Thanks for the answer. I know I am a bit tedious, but is it possible to perform that without using calc package. I avoid additional packages, if possible. – Pygmalion Oct 26 '13 at 15:40
• @Pygmalion Unfortunately \totalheightof comes from calc package and it is the mainstay of the code... – karlkoeller Oct 26 '13 at 15:45