# Reserving space to find out whether a tabular will end up on odd or even page

I have a large number (~1200) of single-row tabular environments which include figures. Depending on odd or even page, I want to change the order of the columns. I'm now using \Needspace{value}\checkoddpage to determine where the next tabular will be placed.

The problem is that this is not entirely accurate, and edge cases get misjudged. This results in either the first or the last tabulars of some pages to end up wrongly formatted. Is there a way to make \Needspace more accurate, or is there another way to reserve space like this? Perhaps a TikZ minipage?

It's difficult to create a MWE which reproduces the behavior, but this should give you an idea of what I'm doing.

\documentclass[11pt,a4paper,twoside]{book}
\usepackage{a4wide}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{array}
\usepackage{ifoddpage}
\usepackage{needspace}
\usepackage{todonotes}
\begin{document}
\chapter{Test}
% Here starts the block for a single object
\Needspace{0.185\textheight}\checkoddpage
\ifoddpage
\begin{tabular}{m{0.3\textwidth} m{0.3\textwidth} m{0.3\textwidth}}
\missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{} & \missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{} & {\huge \textbf{word}} \newline  \newline \newline {\huge other word} \\
\end{tabular}
\else
\begin{tabular}{m{0.3\textwidth} m{0.3\textwidth} m{0.3\textwidth}}
{\huge \textbf{word}} \newline \newline \newline {\huge other word} & \missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{} & \missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{} \\
\end{tabular}
\fi
% Repeat the above block ad nauseam
\end{document}


The actual figures are included with:

\includegraphics[width=0.3\textwidth, max height=0.182\textheight, keepaspectratio=true]{somefigure.png}

• A Minmal Working Example would be useful here. I take it \Needspace is a KOMA version of \needspace? How is value determined? – John Kormylo Sep 11 '16 at 15:18
• Have you read section 1 of changepage documentation? I specifies \strictpagecheck and \easypagecheck, and why the test might sometimes fail. Without seeing a MWE, I am sorry to say that it will be difficult to give you any more help than this. – Runar Sep 11 '16 at 16:52
• I've added a MWE, but it's hard to recreate the exact conditions for which it fails, since it happens when including many figures of varying sizes, which then adds up to some of them being at the edge of falling off the page. – user74375 Sep 11 '16 at 17:24
• @RunarTrollet I hadn't tried \strictpagecheck yet, I was using the ifoddpage package instead of changepage, thanks for that tip. Sadly, it doesn't seem to solve the problem. – user74375 Sep 11 '16 at 18:42
• Don't use a4wide, it's long deprecated. – egreg Sep 11 '16 at 20:17

A \label based approach can be used to detect the page number in a reliable way. However, this required at least two LaTeX runs, the second run then uses the labels of the first run.

\documentclass[11pt,a4paper,twoside]{book}
\usepackage{a4wide}
\usepackage{graphicx}
\usepackage{array}
\usepackage{todonotes}
\usepackage{tabularx}

\usepackage{zref-thepage}

\newcounter{IfOddPageCounter}
\renewcommand*{\theIfOddPageCounter}{TC\the\value{IfOddPageCounter}}

\makeatletter
\newcommand*{\IfOddPage}{%
\leavevmode
\stepcounter{IfOddPageCounter}%
\zref@labelbyprops{\theIfOddPageCounter}{pagevalue}%
\zref@refused{\theIfOddPageCounter}%
\ifodd\zref@extractdefault{\theIfOddPageCounter}{pagevalue}{1}\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother

\begin{document}
\chapter{Test}

\def\TestTable{%
\noindent
\def\tabularxcolumn{m}%
\IfOddPage{%
\begin{tabularx}{\linewidth}{XXX}
\missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{} &
\missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{} &
\huge \textbf{word} \vspace{5mm}\newline other word
\tabularnewline
\end{tabularx}%
}{%
\begin{tabularx}{\linewidth}{XXX}
\huge \textbf{word} \vspace{5mm}\newline other word &
\missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{} &
\missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{}
\tabularnewline
\end{tabularx}%
}%
\par
}

\TestTable
\TestTable
\TestTable
\TestTable
\TestTable
\TestTable
\end{document}


Modification with \label inside the table:

\documentclass[11pt,a4paper,twoside]{book}
\usepackage{a4wide}
\usepackage{graphicx}
\usepackage{array}
\usepackage{todonotes}
\usepackage{tabularx}

\usepackage{zref-thepage}

\newcounter{IfOddPageCounter}
\renewcommand*{\theIfOddPageCounter}{TC\the\value{IfOddPageCounter}}

\makeatletter
\newcommand*{\IfOddPage}{%
\leavevmode
\stepcounter{IfOddPageCounter}%
\zref@refused{\theIfOddPageCounter}%
\ifodd\zref@extractdefault{\theIfOddPageCounter}{pagevalue}{1}\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\newcommand*{\IfOddPageLabel}{%
\zref@labelbyprops{\theIfOddPageCounter}{pagevalue}%
}
\makeatother

\begin{document}
\chapter{Test}

\def\TestTable{%
\noindent
\def\tabularxcolumn{m}%
\IfOddPage{%
\begin{tabularx}{\linewidth}{XXX}
\IfOddPageLabel
\missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{} &
\missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{} &
\huge \textbf{word} \vspace{5mm}\newline other word
\tabularnewline
\end{tabularx}%
}{%
\begin{tabularx}{\linewidth}{XXX}
\IfOddPageLabel
\huge \textbf{word} \vspace{5mm}\newline other word &
\missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{} &
\missingfigure[figheight=0.182\textheight, figwidth=0.3\textwidth]{}
\tabularnewline
\end{tabularx}%
}%
\par
}
\TestTable
\TestTable
\TestTable
\TestTable
\TestTable
\TestTable
\end{document}

• In principle this works, yes. But this is the internal workings of the \checkoddpage command. It works fine for these nicely formatted example figures, but sometimes fails when they're a little more oddly shaped, and one is on the verge of falling off the page. – user74375 Sep 11 '16 at 20:10
• @user74375 The only restriction is, that the \label (\zref@labelbyprops) command must be on the same page as the tabular. If in doubt, put the label inside the tabular. If the table has different heights or different break points in the odd and even case, then the algorithm is not guaranteed to be stable (see package varioref for another example). If only the internal layout of the tabular changes, then the algorithm needs at most one more run than without, because the decisions of the odd page switches are not changing the page breaks. – Heiko Oberdiek Sep 11 '16 at 20:36