如何在表格中间使用samepage环境?

如何在表格中间使用samepage环境?

我使用以下代码来生成文档:

\documentclass[10pt]{article}

\usepackage{ltablex}
\usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry}
\usepackage{bibentry}
\usepackage{etoolbox}
\usepackage{helvet}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{color}  
\usepackage{colortbl}
\usepackage{url}
\usepackage{fancyhdr}
\usepackage{datenumber}
\usepackage{xspace}
\usepackage{blindtext}

%Column width for tables
\def \firstColumnWidth {3.5cm}
\def \secondColumnWidth {12.5cm}

%This is the headline
\newcommand{\headline}[1] {
\section*{#1}
\vspace{-1.75em}
\noindent\makebox[\linewidth]{\rule{\textwidth}{0.4pt}}
\vspace{-1.75em}
}

\begin{document}

\headline{Test A}
\noindent\begin{tabularx}{\textwidth}{p{\firstColumnWidth}p{\secondColumnWidth}}
\multicolumn{2}{c}{\cellcolor[gray]{0.75}2015}\\
Foo & \blindtext\\
Bar & \blindtext\\
\multicolumn{2}{c}{\cellcolor[gray]{0.75}2014}\\
Foo & \blindtext\\
Bar & \blindtext\\
\end{tabularx}

\headline{Test B}
\noindent\begin{tabularx}{\textwidth}{p{\firstColumnWidth}p{\secondColumnWidth}}
Foo & \blindtext\\
Bar & \blindtext\\
\end{tabularx}

\end{document}

现在,我想确保标题和表格的第一行始终位于同一页上。我尝试使用 和 来实现这一点\begin{samepage}\end{samepage}但问题是\end{samepage}不能位于表格中间。我还有什么其他选择?

答案1

您没有X列,所以tabularx在这里什么都不做也没用。您可以使用longtable并防止分页符,\\*不幸的是,longtable它总是允许在 a 处分页\hline,但定义一个不允许分页的变体很简单。

在此处输入图片描述

\documentclass[10pt]{article}

\usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry}
\usepackage{helvet}
\usepackage[table]{xcolor}
\usepackage{blindtext}
\usepackage{longtable}

\makeatletter
\def\nbhline{%no break hline
\noalign{%
  \global\let\tmp\@lowpenalty
  \gdef\@lowpenalty{-10000 }}%
\hline
\noalign{\global\let\@lowpenalty\tmp}}

\makeatother

\setlength\extrarowheight{2pt}
%Column width for tables
\def \firstColumnWidth {3.5cm}
\def \secondColumnWidth {12.5cm}

%This is the headline
\newcommand\headline[1]{%
\noalign{\medskip}%
\multicolumn{2}{@{}l}{\large\bfseries#1}\\*\nbhline
\noalign{\nobreak\smallskip}}

\begin{document}

\begin{longtable}{p{\firstColumnWidth}p{\secondColumnWidth}}
\headline{Test A}
\multicolumn{2}{c}{\cellcolor[gray]{0.75}2015}\\
Foo & \blindtext\\
Bar & \blindtext\\
\multicolumn{2}{c}{\cellcolor[gray]{0.75}2014}\\
Foo & \blindtext\\
Bar & \blindtext\\
\headline{Test B}
Foo & \blindtext\\
Bar & \blindtext\\
\end{longtable}

\end{document}

答案2

这里有一个解决方案。我将 \headline 设为 tabularx 的一部分。无需拆分 tabular

\documentclass[10pt]{article}

\usepackage{ltablex}
\usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry}
\usepackage{bibentry}
\usepackage{etoolbox}
\usepackage{helvet}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{color}  
\usepackage{colortbl}
\usepackage{url}
\usepackage{fancyhdr}
\usepackage{datenumber}
\usepackage{xspace}
\usepackage{blindtext}

%Column width for tables
\def \firstColumnWidth {3.5cm}
\def \secondColumnWidth {12.5cm}

%This is the headline
\newcommand{\headline}[1] {%
\multicolumn{2}{@{}p{\textwidth}}{\Large\bfseries #1\par
\vspace{-.75em}
\makebox[\linewidth]{\rule{\textwidth}{0.4pt}}}\\*}

\begin{document}

\begin{tabularx}{\textwidth}{p{\firstColumnWidth}p{\secondColumnWidth}}
\headline{Test A}
\multicolumn{2}{c}{\cellcolor[gray]{0.75}2015}\\
Foo & \blindtext\\
Bar & \blindtext\\
\multicolumn{2}{c}{\cellcolor[gray]{0.75}2014}\\
Foo & \blindtext\\
Bar & \blindtext\\
\end{tabularx}

\begin{tabularx}{\textwidth}{p{\firstColumnWidth}p{\secondColumnWidth}}
\headline{Test B}
Foo & \blindtext\\
Bar & \blindtext\\
\end{tabularx}



\begin{tabularx}{\textwidth}{p{\firstColumnWidth}p{\secondColumnWidth}}
\headline{Test A}
\multicolumn{2}{c}{\cellcolor[gray]{0.75}2015}\\
Foo & \blindtext\\
Bar & \blindtext\\
\multicolumn{2}{c}{\cellcolor[gray]{0.75}2014}\\
Foo & \blindtext\\
Bar & \blindtext\\
\headline{Test B}
Bar & \blindtext\\
Bar & \blindtext\\
Foo & \blindtext\\
Bar & \blindtext\\
Bar & \blindtext\\
Bar & \blindtext\\
Bar & \blindtext\\
Bar & \blindtext\\
\headline{Test C}
Foo & \blindtext\\
Bar & \blindtext\\
\end{tabularx}

\end{document}

相关内容