1

I want to generate a report automatically. I use python to replace placeholders inside a LaTeX template. The report contains large complex tables that span multiple pages. I have found a packet cals https://groups.google.com/forum/#!topic/comp.text.tex/gE80AAOQ83U that with a small modification supports inter row page breaks.

My problem now is that if the content of one cell spans more than two pages there will one page break but there should have been two. Any idea how to fix this?

Here is my example code:

\documentclass{article}
\usepackage{lipsum}
\usepackage{cals}
\usepackage[latin]{babel}
\usepackage{blindtext}
\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=\copy2
        \fi
        \setbox\rowBefore=\hbox{\box2 \unhbox\rowBefore}%
        \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{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{\blindtext \blindtext \blindtext} \cell{foo3} \cell{foo3} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow
\brow \cell{foo} \cell{a2} \cell{b2} \erow

\end{calstable}
\end{document}
  • 1
    Welcome to TeX.SX! What about longtable package? – user31729 Oct 7 '14 at 10:09
  • most tex multi-page codes don't easily allow page breaking mid-cell (it isn't in general possible to find breakpoints at matching positions in each column, consider having an image in one cell a nested table in another and a paragraph in the third, you would have to find a page break that cuts all three – David Carlisle Oct 7 '14 at 10:09
  • @ChristianHupfer longtable won't help:-) – David Carlisle Oct 7 '14 at 10:09
  • If, as in your example, there is just one column with the bulk data and the other columns all have small one line items, it is usually best to set it as a list which naturally allows page breaking. – David Carlisle Oct 7 '14 at 10:13
  • That wont work, because in the report all columns may have bulk data. In addition to that I have cells that span over multiple rows and others that span over multiple columns. I'm not aware that list support that. – user63843 Oct 7 '14 at 10:22

Your Answer

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

Browse other questions tagged or ask your own question.