Skip to content

Instantly share code, notes, and snippets.

@olpa
Created March 3, 2019 18:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olpa/2a7096b231d0dbe2b022122b1674b4ff to your computer and use it in GitHub Desktop.
Save olpa/2a7096b231d0dbe2b022122b1674b4ff to your computer and use it in GitHub Desktop.
LaTeX multi-page table with inter-row page breaks
% First version published on https://groups.google.com/d/msg/comp.text.tex/gE80AAOQ83U/woX-CFfUQYEJ
% Several changes are applied
\documentclass{article}
\usepackage{lipsum}
\usepackage{cals}
\usepackage[latin]{babel}
\showboxbreadth=100
\showboxdepth=100
\makeatletter
\let\oldDispatch=\cals@row@dispatch
\newbox\rowBefore
\newbox\rowAfter
\newbox\decorationCopy
\newdimen\splitHeight
\def\cals@row@dispatch{%
\cals@ifbreak\iftrue % detect that a break is required
\splitHeight=\pagegoal \advance\splitHeight -\pagetotal
\ifdim \splitHeight>50pt % break inrow only if at least 100pt left
\advance\splitHeight -5pt % avoid pagebreak due to overflows
%
% Split the current row on two: before and after the break
%
\setbox\rowBefore=\hbox{}
\setbox\rowAfter=\hbox{}
\def\next{%
\setbox0=\lastbox
\ifvoid0
\def\next{\global\setbox\rowBefore=\box\rowBefore
\global\setbox\rowAfter=\box\rowAfter }%
\else
\setbox2=\vsplit0 to\splitHeight
\ifvoid0
\setbox0=\vbox{\hbox to \wd2{}}%
\fi
\setbox\rowBefore=\hbox{\box2 \unhbox\rowBefore
\hbox to 0pt{\hss\vbox{\hrule width\wd\cals@current@cs}}}%
\setbox\rowAfter=\hbox{\box0 \unhbox\rowAfter}%
\fi
\next}
\setbox0=\hbox{\unhbox\cals@current@row \next}
%
% Decoration backup, typeset the first row,
% restore context, typeset the second at the end of macro
%
\setbox\decorationCopy=\copy\cals@current@cs
\setbox\cals@current@row=\box\rowBefore
\ht\cals@current@cs=\ht\cals@current@row
\oldDispatch
\cals@issue@break
\cals@thead@tokens
\setbox\cals@current@row=\box\rowAfter
\cals@reheight@cells\cals@current@row
\setbox\cals@current@cs=\box\decorationCopy
\ht\cals@current@cs=\ht\cals@current@row
\let\cals@current@context=b
\fi
\fi
\oldDispatch}
\begin{document}
\begin{calstable}
\colwidths{{150pt}{150pt}{150pt}}
\thead{\brow \bfseries \cell{header1}\cell{header2}\cell{header3}
\mdseries \erow}
\brow \cell{\lipsum[ 1]}\cell{\lipsum[ 2]}\cell{\lipsum[ 3]} \erow
\brow \cell{\lipsum[ 4]}\cell{\lipsum[ 5]}\cell{\lipsum[ 6]} \erow
\brow \cell{\lipsum[ 7]}\cell{\lipsum[ 8]}\cell{\lipsum[ 9]} \erow
\brow \cell{\lipsum[10]}\cell{\lipsum[11]}\cell{\lipsum[12]} \erow
\brow \cell{\lipsum[13]}\cell{\lipsum[14]}\cell{\lipsum[15]} \erow
\brow \cell{\lipsum[16]}\cell{\lipsum[17]}\cell{\lipsum[18]} \erow
\end{calstable}
\end{document}
@olpa
Copy link
Author

olpa commented Sep 16, 2020

\def\cals@rs@width{0pt}
\def\cals@cs@width{0pt}

http://mirrors.ctan.org/macros/latex/contrib/cals/examples/demo.pdf

@peter-weis
Copy link

peter-weis commented Mar 31, 2023

Hi Oleg,
This is really a great solution! ...and yes I'm totally aware that table cells should not break over pages...
Is there a change to make this work with list items as well? It actually breaks correctly, but when a cell is split the indent or left padding of the 2nd and 3rd column gets wrong.

same code as above but with items
`
\usepackage{enumitem}

\begin{document}
\begin{calstable}
\colwidths{{150pt}{150pt}{150pt}}
\thead{\brow \bfseries \cell{header1}\cell{header2}\cell{header3}
\mdseries \erow}
\brow
\cell{\begin{itemize}[labelsep=0mm, leftmargin=3mm, rightmargin=1mm] \item[a)] \lipsum[ 1] \end{itemize}}
\cell{\begin{itemize}[labelsep=0mm, leftmargin=3mm, rightmargin=1mm] \item[a)] \lipsum[ 2] \end{itemize}}
\cell{\begin{itemize}[labelsep=0mm, leftmargin=3mm, rightmargin=1mm] \item[a)] \lipsum[ 3] \end{itemize}}
\erow
\brow
\cell{\begin{itemize}[labelsep=0mm, leftmargin=3mm, rightmargin=1mm] \item[a)] \lipsum[ 4] \end{itemize}}
\cell{\begin{itemize}[labelsep=0mm, leftmargin=3mm, rightmargin=1mm] \item[a)] \lipsum[ 5] \end{itemize}}
\cell{\begin{itemize}[labelsep=0mm, leftmargin=3mm, rightmargin=1mm] \item[a)] \lipsum[ 6] \end{itemize}}
\erow
\end{calstable}
\end{document}
`

@olpa
Copy link
Author

olpa commented Apr 4, 2023

Sorry Peter, I couldn't find the reason in fast. Unfortunately, I can't help in the near future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment