保留空间来确定表格最终会出现在奇数页还是偶数页

保留空间来确定表格最终会出现在奇数页还是偶数页

我有大量(约 1200 个)包含数字的单行tabular环境。根据奇数页或偶数页,我想更改列的顺序。我现在用它\Needspace{value}\checkoddpage来确定下一个表格的位置。

问题是,这并不完全准确,而且边缘情况会被误判。这会导致某些页面的第一个或最后一个表格格式错误。有没有办法让它\Needspace更准确,或者有其他方法可以像这样保留空间?也许是TikZ迷你页面?

创建一个可以重现该行为的 MWE 很困难,但这应该可以让您了解我在做什么。

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

实际数字包括:

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

答案1

可以使用基于方法\label以可靠的方式检测页码。但是,这需要至少两次 LaTeX 运行,第二次运行则使用第一次运行的标签。

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

第 1 页第2页

\label表内修改:

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

相关内容