我很难找到解决方案来理解row
宏最后一行中的以下代码有什么问题,特别是代码\xifinlist{\arabic{currow}}{\rowpagebreaks}{\pagebreak}{}
。我想要实现的是,如果行索引(存储在中currow
)位于需要从新页面(存储在中rowpagebreaks
)开始的行索引列表中,则会导致插入,\pagebreak
以便 longtable 知道从空白页上的这一特定行开始。虽然硬编码\pagebreak
按预期工作,但使用条件插入为\pagebreak
我提供了以下错误消息,我无法以有意义的方式解释这些错误消息:
Misplaced \noalign.
\pagebreak ->\noalign
{\ifnum `}=0\fi \@testopt {\LT@no@pgbk -}4
l.85 \row{\datespan{11/2008}{03/2013}}{b}
%
I expect to see \noalign only after the \cr of
an alignment. Proceed, and I'll ignore this case.
main.tex, line 88
Misplaced \omit.
\multispan ->\omit
\@multispan
l.88 \rowsection{Section B}
%
I expect to see \omit only after tab marks or the \cr of
an alignment. Proceed, and I'll ignore this case.
我的问题是,为什么插入/硬编码\pagebreak
工作正常,但使用条件会触发这种错误? 有没有办法解决这种错误和情况?
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\usepackage{etoolbox}
\setlength{\tabcolsep}{0pt}
% counting the rows
\newcounter{currow}
\newlength{\rowmargin}
\setlength{\rowmargin}{15pt}
\newtoggle{addrowmargin}
\togglefalse{addrowmargin}
\newtoggle{debug}
\togglefalse{debug}
% a list of row indexes when a pagebreak should be inserted
\newcommand*\rowpagebreaks{}
\forcsvlist{\listadd\rowpagebreaks}{3,6}
\newcommand{\row}[2]{%
\iftoggle{debug}{%
{\scriptsize\textbf{Row: \arabic{currow}}\newline}%
}{}%
%
{#1} & {#2}%
%
\stepcounter{currow}%
%
\tabularnewline%
% If the index of the current row is found in the list of rows that require to start at a new blank page, insert a \pagebreak or \newpage ...
%
% Manually inserting \pagebreak or \newpage works
%\pagebreak
%\newpage
% However, this does not work ...
\xifinlist{\arabic{currow}}{\rowpagebreaks}{\pagebreak}{}%
}
\newcommand{\rowsection}[1]{%
\rowspan{%
\begin{minipage}[t]{\columnwidth}%
\vspace{20pt}% top section margin
{\normalfont\sffamily\footnotesize #1 \par}%
\vspace{-\baselineskip}%
\vspace{6pt}% margin between text and line
\rule{\linewidth}{.5pt}%
\vspace{12pt}% bottom section margin
\end{minipage}%
}%
% reset row counter to zero
\stepcounter{currow}%
\togglefalse{addrowmargin}%
%
\tabularnewline%
\hline%
}
\newcommand{\rowspan}[1]{%
% no indent of column
\multicolumn{2}{@{}l@{}}{%
\iftoggle{debug}{%
{\scriptsize\textbf{Row: \arabic{currow}}~ }%
}{}%
#1%
}%
}
\newcommand{\colwidth}[1]{%
\dimexpr #1\textwidth-2\tabcolsep%
}%
\newcommand{\datespan}[2]{%
\begin{minipage}[t]{\colwidth{.23}}#1\hfill-\hfill#2\end{minipage}%
}%
\begin{document}
\begin{longtable}{@{}p{\colwidth{.25}}p{\colwidth{.75}}@{}}%
\rowsection{Section A}%
\row{\datespan{11/2008}{03/2013}}{b}%
\row{\datespan{11/2008}{03/2013}}{b}%
\row{a}{b}%
\row{a}{b}%
\rowsection{Section B}%
\row{\datespan{11/2008}{03/2013}}{b}%
\row{\datespan{11/2008}{03/2013}}{b}%
\row{a}{b}%
\row{a}{b}%
\end{longtable}%
\end{document}
答案1
我认为 \xifinlist 不可扩展。您也许可以使用 expl3 进行测试,但通常将测试移到之前的单元格中会更容易:
\newcommand\conditionalpagebreak{}
\newcommand{\row}[2]{%
\iftoggle{debug}{%
{\scriptsize\textbf{Row: \arabic{currow}}\newline}%
}{}%
%
{#1} & {#2}%
%
\stepcounter{currow}%
%
\xifinlist{\arabic{currow}}{\rowpagebreaks}
{\global\let\conditionalpagebreak\pagebreak}
{\gdef\conditionalpagebreak{}}%
\tabularnewline
\conditionalpagebreak
}