1

I use the following code to generate a document:

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

Now, I want to make sure that the headline and the first row of a table is always on the same page. I tried to do that with \begin{samepage} and \end{samepage}, but the problem is that \end{samepage} cannot be in the middle of the table. What other options do I have?

  • ltablex somewhat confusingly turns tabularx into longtable so (a) you should not have \noindent before it and (b) you can use longtable commands such as \endfirsthead to control which lines stay together at the start – David Carlisle Feb 19 '15 at 11:12
  • Thanks for the hint. The problem is that the \headline is not part of the table and so I imho can't use the \endfirsthead command. – RoflcoptrException Feb 20 '15 at 7:47
  • latex section heading prevent page breaks after them but you have defeated that by \noindent\makebox[\linewidth]{\rule{\textwidth}{0.4pt}} that should never be after the section heading, if you want an underlined heading that should be in the specification of the heading style, not in the document. – David Carlisle Feb 20 '15 at 9:06
  • You can manually tweak it as \usepackage{needspace} and \needspace{10\baselineskip}\headline{Test B} – Steven B. Segletes Feb 27 '15 at 15:39
4

You have no X columns so tabularx is doing nothing useful here. You can just use longtable and prevent page breaks with \\* Unfortunately longtable always allows a break at a \hline but it is simple to define a variant that doesn't.

enter image description here

\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
+50

Here is a solution. I make \headline part of tabularx. There is no need to split 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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.