8

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}

enter image description here enter image description here

2 Answers 2

6

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.

2
  • I wonder... which solution is in your opinion more economical in terms of resources and compiling times?
    – Pygmalion
    Commented Oct 27, 2013 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
    Commented Nov 8, 2013 at 6:34
7

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}}%
}

and add

\usepackage{calc}

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

in your preamble, so that your MWE becomes

\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:

enter image description here

2
  • 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
    Commented Oct 26, 2013 at 15:40
  • @Pygmalion Unfortunately \totalheightof comes from calc package and it is the mainstay of the code... Commented Oct 26, 2013 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .