3

I'm having a tricky problem with a longtabu. I have a program that produces LaTeX code for an accounting report, which is done with a longtabu where each row is either an account (with a total) or a title (identifying a group of accounts). Titles are special, in that they have an \hline on top and a bold font. Now, it may happen that a title finds itself at the bottom of the page, see the image below: report example

In this report, the last line "20 Dettes commerce" is a title, and the accounts under that title are appearing in the new page.

My problem is that I would like to avoid a title appearing alone at the bottom of a page, in our example I would like "20 Dettes commerce" to be pushed to the next page. I cannot insert a pagebreak manually, because as I said the LaTeX is produced by a program and I have no idea what will be on which page at generation time, so I would need LaTeX (xeLateX in my particular case) to figure out the fact that this line is appearing at the bottom, and insert a pagebreak automatically.

I tried looking at the \pagetotal macro, but it always seems to be 0.0pt in the table (I read somewhere that \pagetotal does not work inside tables). I also tried to use \needspace, but it is not giving me conclusive results.

So the questions are:

  • Is there a useful way of using pagetotal inside a table to detect how far down I am within the page? and/or
  • is there a useful way to use needspace to say that I don't want a title line at the bottom of the page? and/or
  • any other idea? :)

Thanks a lot!

Here's the code for the example:

 \documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{seqsplit}
\usepackage{helvet}
\usepackage{graphicx}
\usepackage{grfext}
\usepackage{tabu}
\usepackage[francais,germanb,english,dutch]{babel}
\usepackage[margin=1.5cm,portrait,includehead,includefoot]{geometry}
\usepackage{longtable}
\usepackage{fancyhdr,lastpage}
\usepackage{pgfpages}
\usepackage{verbatim}
\usepackage{needspace}
\usepackage[export]{adjustbox} 
\usepackage{hanging}\newlength{\hangwidth}
\newcommand{\myhang}[2]{\settowidth{\hangwidth}{#1 }\addtolength{\hangwidth}{#2}\hangpara{\hangwidth}{1}#1}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand{\arraystretch}{1.5}
\newcommand{\tabledefine}{\rowcolors{2}{white}{white}}
\newcommand{\myalign}[1]{\multicolumn{1}{>{\columncolor{gray!50}}l}{#1}}
\newcommand{\rowdefine}{\rowcolor{gray!50}}



\pagestyle{fancy}
\lhead{Balance bidon\_1(CHF)}
\rhead{01/07/2011 $-$ 31/12/2012}
\lfoot{24/04/2014}
\cfoot{\thepage\ / \pageref{LastPage}}
\rfoot{Balance.pdf}
\begin{document}
\nonstopmode
\footnotesize 
\definecolor{lightBlue}{rgb}{0.93,0.95,1.0}
\definecolor{lightGray}{rgb}{0.9,0.9,0.9}
\definecolor{darkerGray}{rgb}{0.7,0.7,0.7}
\selectlanguage{francais}
\tabledefine
\tabulinesep=1.2mm
\begin{longtabu} to \textwidth {X[l]X[r]X[r]}
\hline
\rowcolor{lightBlue}Compte & D\'ebit & Cr\'edit\\\hline\endhead
\\\hline\rowcolor{black}\parindent=1.5mm\indent \textbf{{\color{white}\myhang{BILAN}{1.5mm}}} & \textbf{{\color{white} }} & \parindent=1.5mm\indent \textbf{{\color{white} }}\\
\\\hline\parindent=3mm\indent \textbf{\myhang{1}{3mm} ACTIFS} & \textbf{ } & \parindent=3mm\indent \textbf{ }\\
\\\hline\parindent=4.5mm\indent \textbf{\myhang{10}{4.5mm} Liquidit\'es} & \textbf{ } & \parindent=4.5mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{1000}{6mm} Caisse & \myhang{256.06}{0mm} & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1001}{6mm} Caisse EUR &   & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1010}{6mm} Poste &   & \parindent=6mm\indent \myhang{111.00}{0mm}\\
\parindent=6mm\indent \myhang{1020}{6mm} Banque d\'ep\^ot & \myhang{27'946.76}{0mm} & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1030}{6mm} Banque Carte cr\'edit & \myhang{288.47}{0mm} & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1060}{6mm} Titres & \myhang{1'000.00}{0mm} & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1070}{6mm} Rabobank &   & \parindent=6mm\indent \myhang{32.00}{0mm}\\
\rowcolor{lightGray} Total \parindent=4.5mm\indent \textit{\myhang{10}{4.5mm} Liquidit\'es} & \textit{\myhang{29'348.29}{0mm}} & \parindent=4.5mm\indent \textit{ }\\\hline
\\\hline\parindent=4.5mm\indent \textbf{\myhang{11}{4.5mm} Cr\'eances} & \textbf{ } & \parindent=4.5mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{1100.1001}{6mm} UBS &   & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1120}{6mm} Actif transitoire & \myhang{0.74}{0mm} & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1170}{6mm} TVA d\'eductible sur mat\'eriel et service &   & \parindent=6mm\indent \myhang{2.56}{0mm}\\
\parindent=6mm\indent \myhang{1171}{6mm} TVA d\'eductible sur investissement & \myhang{17.37}{0mm} & \parindent=6mm\indent  \\
\rowcolor{lightGray} Total \parindent=4.5mm\indent \textit{\myhang{11}{4.5mm} Cr\'eances} & \textit{\myhang{15.55}{0mm}} & \parindent=4.5mm\indent \textit{ }\\\hline
\\\hline\parindent=4.5mm\indent \textbf{\myhang{12}{4.5mm} Stocks} & \textbf{ } & \parindent=4.5mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{1200}{6mm} Stock marchandises &   & \parindent=6mm\indent \myhang{12.00}{0mm}\\
\rowcolor{lightGray} Total \parindent=4.5mm\indent \textit{\myhang{12}{4.5mm} Stocks} & \textit{ } & \parindent=4.5mm\indent \textit{\myhang{12.00}{0mm}}\\\hline
\\\hline\parindent=4.5mm\indent \textbf{\myhang{13}{4.5mm} R\'egulation} & \textbf{ } & \parindent=4.5mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{1300}{6mm} Charges constat\'ees en avance & \myhang{26'099.00}{0mm} & \parindent=6mm\indent  \\
\rowcolor{lightGray} Total \parindent=4.5mm\indent \textit{\myhang{13}{4.5mm} R\'egulation} & \textit{\myhang{26'099.00}{0mm}} & \parindent=4.5mm\indent \textit{ }\\\hline
\rowcolor{lightGray} Total \parindent=3mm\indent \textit{\myhang{1}{3mm} ACTIFS} & \textit{\myhang{55'450.84}{0mm}} & \parindent=3mm\indent \textit{ }\\\hline
\\\hline\parindent=3mm\indent \textbf{\myhang{2}{3mm} PASSIF} & \textbf{ } & \parindent=3mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{2.1}{6mm} Passif 1 &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\parindent=6mm\indent \myhang{2.2}{6mm} Passif 2 &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\parindent=6mm\indent \myhang{2.3}{6mm} Passif 3 &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\parindent=6mm\indent \myhang{2.4}{6mm} Passif 4 &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\parindent=6mm\indent \myhang{2.5}{6mm} Passif 5 &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\\\hline\parindent=4.5mm\indent \textbf{\myhang{20}{4.5mm} Dettes commerce } & \textbf{ } & \parindent=4.5mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{2000.1001}{6mm} Bloomberg &   & \parindent=6mm\indent \myhang{27'098.71}{0mm}\\
\parindent=6mm\indent \myhang{2000.1002}{6mm} Goldentech &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\end{longtabu}
\end{document}

1 Answer 1

2

In tabu and long table you can use \\* for non page breaking end of row. You may have also problem when page ends on \hline. If it is the case put

\makeatletter
\newcommand\nobreakhline{%
\multispan\LT@cols
\unskip\leaders\hrule\@height\arrayrulewidth\hfill\\*}
\makeatother

in your preamble and use \nobreakhline instead of \hline.

Below is your MME with mentioned changes. I hope it is the result you wanted:

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{seqsplit}
\usepackage{helvet}
\usepackage{graphicx}
\usepackage{grfext}
\usepackage{tabu}
\usepackage[francais,germanb,english,dutch]{babel}
\usepackage[margin=1.5cm,portrait,includehead,includefoot]{geometry}
\usepackage{longtable}
\usepackage{fancyhdr,lastpage}
\usepackage{pgfpages}
\usepackage{verbatim}
\usepackage{needspace}
\usepackage[export]{adjustbox} 
\usepackage{hanging}\newlength{\hangwidth}
\newcommand{\myhang}[2]{\settowidth{\hangwidth}{#1 }\addtolength{\hangwidth}{#2}\hangpara{\hangwidth}{1}#1}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand{\arraystretch}{1.5}
\newcommand{\tabledefine}{\rowcolors{2}{white}{white}}
\newcommand{\myalign}[1]{\multicolumn{1}{>{\columncolor{gray!50}}l}{#1}}
\newcommand{\rowdefine}{\rowcolor{gray!50}}

\makeatletter
\newcommand\nobreakhline{%
\multispan\LT@cols
\unskip\leaders\hrule\@height\arrayrulewidth\hfill\\*}
\makeatother
\pagestyle{fancy}
\lhead{Balance bidon\_1(CHF)}
\rhead{01/07/2011 $-$ 31/12/2012}
\lfoot{24/04/2014}
\cfoot{\thepage\ / \pageref{LastPage}}
\rfoot{Balance.pdf}
\begin{document}
\nonstopmode
\footnotesize 
\definecolor{lightBlue}{rgb}{0.93,0.95,1.0}
\definecolor{lightGray}{rgb}{0.9,0.9,0.9}
\definecolor{darkerGray}{rgb}{0.7,0.7,0.7}
\selectlanguage{francais}
\tabledefine
\tabulinesep=1.2mm
\begin{longtabu} to \textwidth {X[l]X[r]X[r]}
\hline
\rowcolor{lightBlue}Compte & D\'ebit & Cr\'edit\\\hline\endhead
\\\hline\rowcolor{black}\parindent=1.5mm\indent \textbf{{\color{white}\myhang{BILAN}{1.5mm}}} & \textbf{{\color{white} }} & \parindent=1.5mm\indent \textbf{{\color{white} }}\\
\\\hline\parindent=3mm\indent \textbf{\myhang{1}{3mm} ACTIFS} & \textbf{ } & \parindent=3mm\indent \textbf{ }\\
\\\hline\parindent=4.5mm\indent \textbf{\myhang{10}{4.5mm} Liquidit\'es} & \textbf{ } & \parindent=4.5mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{1000}{6mm} Caisse & \myhang{256.06}{0mm} & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1001}{6mm} Caisse EUR &   & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1010}{6mm} Poste &   & \parindent=6mm\indent \myhang{111.00}{0mm}\\
\parindent=6mm\indent \myhang{1020}{6mm} Banque d\'ep\^ot & \myhang{27'946.76}{0mm} & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1030}{6mm} Banque Carte cr\'edit & \myhang{288.47}{0mm} & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1060}{6mm} Titres & \myhang{1'000.00}{0mm} & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1070}{6mm} Rabobank &   & \parindent=6mm\indent \myhang{32.00}{0mm}\\
\rowcolor{lightGray} Total \parindent=4.5mm\indent \textit{\myhang{10}{4.5mm} Liquidit\'es} & \textit{\myhang{29'348.29}{0mm}} & \parindent=4.5mm\indent \textit{ }\\\hline
\\\hline\parindent=4.5mm\indent \textbf{\myhang{11}{4.5mm} Cr\'eances} & \textbf{ } & \parindent=4.5mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{1100.1001}{6mm} UBS &   & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1120}{6mm} Actif transitoire & \myhang{0.74}{0mm} & \parindent=6mm\indent  \\
\parindent=6mm\indent \myhang{1170}{6mm} TVA d\'eductible sur mat\'eriel et service &   & \parindent=6mm\indent \myhang{2.56}{0mm}\\
\parindent=6mm\indent \myhang{1171}{6mm} TVA d\'eductible sur investissement & \myhang{17.37}{0mm} & \parindent=6mm\indent  \\
\rowcolor{lightGray} Total \parindent=4.5mm\indent \textit{\myhang{11}{4.5mm} Cr\'eances} & \textit{\myhang{15.55}{0mm}} & \parindent=4.5mm\indent \textit{ }\\\hline
\\\hline\parindent=4.5mm\indent \textbf{\myhang{12}{4.5mm} Stocks} & \textbf{ } & \parindent=4.5mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{1200}{6mm} Stock marchandises &   & \parindent=6mm\indent \myhang{12.00}{0mm}\\
\rowcolor{lightGray} Total \parindent=4.5mm\indent \textit{\myhang{12}{4.5mm} Stocks} & \textit{ } & \parindent=4.5mm\indent \textit{\myhang{12.00}{0mm}}\\\hline
\\\hline\parindent=4.5mm\indent \textbf{\myhang{13}{4.5mm} R\'egulation} & \textbf{ } & \parindent=4.5mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{1300}{6mm} Charges constat\'ees en avance & \myhang{26'099.00}{0mm} & \parindent=6mm\indent  \\
\rowcolor{lightGray} Total \parindent=4.5mm\indent \textit{\myhang{13}{4.5mm} R\'egulation} & \textit{\myhang{26'099.00}{0mm}} & \parindent=4.5mm\indent \textit{ }\\\hline
\rowcolor{lightGray} Total \parindent=3mm\indent \textit{\myhang{1}{3mm} ACTIFS} & \textit{\myhang{55'450.84}{0mm}} & \parindent=3mm\indent \textit{ }\\\hline
\\\hline\parindent=3mm\indent \textbf{\myhang{2}{3mm} PASSIF} & \textbf{ } & \parindent=3mm\indent \textbf{ }\\
\parindent=6mm\indent \myhang{2.1}{6mm} Passif 1 &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\parindent=6mm\indent \myhang{2.2}{6mm} Passif 2 &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\parindent=6mm\indent \myhang{2.3}{6mm} Passif 3 &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\parindent=6mm\indent \myhang{2.4}{6mm} Passif 4 &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\parindent=6mm\indent \myhang{2.5}{6mm} Passif 5 &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\\\nobreakhline\parindent=4.5mm\indent \textbf{\myhang{20}{4.5mm} Dettes commerce } &
\textbf{ } & \parindent=4.5mm\indent \textbf{ }\\*
\parindent=6mm\indent \myhang{2000.1001}{6mm} Bloomberg &   & \parindent=6mm\indent \myhang{27'098.71}{0mm}\\
\parindent=6mm\indent \myhang{2000.1002}{6mm} Goldentech &   & \parindent=6mm\indent \myhang{2'000.00}{0mm}\\
\end{longtabu}
\end{document}    

enter image description here

1
  • Yes! That's exactly what I needed, I can consistently use \nobreakhline for title rows, and it will only have an effect when the title row is at the end of the page. Thanks a lot!
    – risoldi
    Apr 24, 2014 at 12:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .