在阅读了这里的大量答案、文档和日志输出后,我不知道如何解决我的 \nopagebreak 问题。
根据如何在 \multicolumn 之前插入 \pagebreak?回答我构建了这个 MWE:
\documentclass{article}
\usepackage{xltabular}
\usepackage[table]{xcolor}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\colorlet{sectionbg}{gray!20}
\let\jakunTXtest\write
\newcommand{\ltpagebreak}{\ifx\jakunTXtest\write \pagebreak[2]\fi}
\newcommand{\ltnopagebreak}{\ifx\jakunTXtest\write \nopagebreak\fi}
\renewcommand{\arraystretch}{1.5}
\newcommand{\eol}{\cr\hline}
\renewcommand{\section}[1]{\rowcolor{sectionbg}\multicolumn2{|l|}{#1}\eol}
\keepXColumns
\begin{document}
\begin{xltabular}{.5\linewidth}{|lL|}
\hline\endhead
\section{Section~1}
1 & Line A \eol
2 & Line B \eol
3 & Line C \eol
\section{Section~2}
4 & Line A \eol
5 & Line B \eol
6 & Line C \eol
\section{Section~3}
7 & Line A \eol
8 & Line B \eol
9 & Line C \eol
\section{Section~4}
10 & Line A \eol
11 & Line B \eol
12 & Line C \eol
\section{Section~5}
13 & Line A \eol
14 & Line B \eol
15 & Line C \eol
\section{Section~6}
16 & Line A \eol
17 & Line B \eol
18 & Line C \eol
\section{Section~7}
19 & Line A \eol
20 & Line B \eol
21 & Line C \eol
\section{Section~8}\ltnopagebreak
22 & Line A \eol\ltnopagebreak
23 & Line B \eol\ltnopagebreak
24 & Line C \eol\ltnopagebreak
\section{Section~9}
25 & Line A \eol
26 & Line B \eol
27 & Line C \eol
\end{xltabular}
\end{document}
我需要将该部分和后面的行放在一起,只允许在完整的部分之后进行分页。
我尝试将 移到\nopagebreak
不同的地方,例如 之后\cr
,但没有成功。
有机会让它发挥作用吗?
答案1
不要试图避免在错误的地方发生中断,而要强制在正确的地方发生中断。
例如,如果您想避免在页面底部出现孤独的第 8 节,该命令\sectionxy
将在所需的位置添加一个空白行,从而使(longtable)成为分页符并在新页面上开始第 8 节。
我将您的命令名称更改为,\section
以\sectionx
避免与正确的发生冲突\section
。
\documentclass{article}
\usepackage{xltabular}
\usepackage[table]{xcolor}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\colorlet{sectionbg}{gray!20}
\renewcommand{\arraystretch}{1.5}
\newcommand{\eol}{\\ \hline}
\newcommand{\sectionx}[1]{\rowcolor{sectionbg}\multicolumn{2}{|l|}{#1}\eol} % old \section
\newcommand{\sectionxy}[1]{\multicolumn{2}{c}{} \\ \rowcolor{sectionbg}\multicolumn{2}{|l|}{#1}\eol} % added <<<
\begin{document}
\begin{xltabular}{.5\linewidth}{|lL|}
\hline\endhead
\sectionx{Section~1}
1 & Line A \eol
2 & Line B \eol
3 & Line C \eol
\sectionx{Section~2}
4 & Line A \eol
5 & Line B \eol
6 & Line C \eol
\sectionx{Section~3}
7 & Line A \eol
8 & Line B \eol
9 & Line C \eol
\sectionx{Section~4}
10 & Line A \eol
11 & Line B \eol
12 & Line C \eol
\sectionx{Section~5}
13 & Line A \eol
14 & Line B \eol
15 & Line C \eol
\sectionx{Section~6}
16 & Line A \eol
17 & Line B \eol
18 & Line C \eol
\sectionx{Section~7}
19 & Line A \eol
20 & Line B \eol
21 & Line C \eol
\sectionxy{Section~8} % added <<<<<<
22 & Line A \eol
23 & Line B \eol
24 & Line C \eol
\sectionx{Section~9}
25 & Line A \eol
26 & Line B \eol
27 & Line C \eol
\end{xltabular}
\end{document}
答案2
longtblr
环境的替代解决方案tabularray
包装:\nopagebreak
按预期工作。
\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}
\NewTableCommand{\mysection}{\SetCell[c=2]{gray9}}
\begin{document}
\begin{longtblr}[
caption = {Long Table},
]{
width = 0.5\linewidth,
colspec = {|lX[l]|},
hlines,
}
\mysection Section~1 & \\
1 & Line A \\
2 & Line B \\
3 & Line C \\
\mysection Section~2 & \\
4 & Line A \\
5 & Line B \\
6 & Line C \\
\mysection Section~3 & \\
7 & Line A \\
8 & Line B \\
9 & Line C \\
\mysection Section~4 & \\
10 & Line A \\
11 & Line B \\
12 & Line C \\
\mysection Section~5 & \\
13 & Line A \\
14 & Line B \\
15 & Line C \\
\mysection Section~6 & \\
16 & Line A \\
17 & Line B \\
18 & Line C \\
\mysection Section~7 & \\
19 & Line A \\
20 & Line B \\
21 & Line C \\
\mysection Section~8 & \\ \nopagebreak
22 & Line A \\ \nopagebreak
23 & Line B \\ \nopagebreak
24 & Line C \\ \nopagebreak
\mysection Section~9 & \\
25 & Line A \\
26 & Line B \\
27 & Line C \\
\end{longtblr}
\end{document}