1

I want to measure the height of a table and insert a \newpage before it if the table is too high. My table is done with longtabu of the tabu package. Where should I start?

\documentclass{article}
\usepackage{longtable}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage[demo]{graphicx}


\newenvironment{versionhistory}%
{
  \begin{lrbox}{\tablebox}\begin{longtabu}{@{}llXX[2]@{}}%
  \toprule%
}%
{%
  \bottomrule%
  \end{longtabu}\end{lrbox}%
}

\newcommand{\vhEntry}[4]{%
#1 & #2 & #3 & #4 \\
}

\begin{document}

\begin{versionhistory}
\vhEntry{0.4}{2015-03-02}{A. Uthor}{Bump to version 0.4.}
\vhEntry{0.3}{2015-03-02}{A. Uthor}{Update data of  experiment.}
\vhEntry{0.2}{2015-02-12}{A. Uthor}{Add intensity plot and photos.}
\vhEntry{0.1}{2015-02-02}{A. Uthor}{First version.}
\end{versionhistory}

\end{document}

2 Answers 2

1

Here, I use the \vbox\bgroup ... \egroup approach to saving the box across the environment. In the environment aftercode, I check the box height and only output it if it is less than 7\baselineskip high. Otherwise, I save it for the next page.

In the MWE, I print out helpful diagnostics to show what logic is unfolding, but obviously you would remove this from the production version.

\documentclass{article}
\usepackage{longtable}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage[demo]{graphicx}

\newsavebox\tablebox
\newenvironment{versionhistory}%
{
  \global\setbox\tablebox=
\vbox\bgroup\begin{longtabu}{@{}llXX[2]@{}}%
  \toprule%
}%
{%
  \bottomrule%
  \end{longtabu}\egroup%
  \ifdim\ht\tablebox<7\baselineskip\relax
    Height is less than 7 baselineskip\par
    \usebox{\tablebox}
  \else
    Box is too high.
  \fi
}

\newcommand{\vhEntry}[4]{%
#1 & #2 & #3 & #4 \\
}

\begin{document}

\begin{versionhistory}
\vhEntry{0.4}{2015-03-02}{A. Uthor}{Bump to version 0.4.}
\vhEntry{0.3}{2015-03-02}{A. Uthor}{Update data of  experiment.}
\vhEntry{0.2}{2015-02-12}{A. Uthor}{Add intensity plot and photos.}
\vhEntry{0.1}{2015-02-02}{A. Uthor}{First version.}
\end{versionhistory}

Try it with a larger box now.

\begin{versionhistory}
\vhEntry{0.4}{2015-03-02}{A. Uthor}{Bump to version 0.4.}
\vhEntry{0.3}{2015-03-02}{A. Uthor}{Update data of  experiment.}
\vhEntry{0.2}{2015-02-12}{A. Uthor}{Add intensity plot and photos.}
\vhEntry{0.1}{2015-02-02}{A. Uthor}{First version.}
\vhEntry{0.1}{2015-02-02}{A. Uthor}{First version.}
\vhEntry{0.1}{2015-02-02}{A. Uthor}{First version.}
\vhEntry{0.1}{2015-02-02}{A. Uthor}{First version.}
\vhEntry{0.1}{2015-02-02}{A. Uthor}{First version.}
\vhEntry{0.1}{2015-02-02}{A. Uthor}{First version.}
\vhEntry{0.1}{2015-02-02}{A. Uthor}{First version.}
\end{versionhistory}

But I can use it later

\clearpage
\usebox{\tablebox}


\end{document}

Page 1

enter image description here

and page 2

enter image description here

0
0

Just put it in a table environment and it will do what you want:

% arara: pdflatex

\documentclass{article}
\usepackage{longtable}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{blindtext}

\newenvironment{versionhistory}%
{
    \begin{table*}[h]
    \begin{longtabu}{@{}S[table-format=1.1]lXX[2]@{}}%
            \toprule%
        }%
        {%
            \bottomrule%
        \end{longtabu}%
    \end{table*}
    }

    \newcommand{\vhEntry}[4]{%
        #1 & #2 & #3 & #4 \\
    }

\begin{document}
\blindtext[4]
\begin{versionhistory}
    \vhEntry{0.4}{2015-03-02}{A.\ Uthor}{Bump to version $0.4$.}
    \vhEntry{0.3}{2015-03-02}{A.\ Uthor}{Update data of  experiment.}
    \vhEntry{0.2}{2015-02-12}{A.\ Uthor}{Add intensity plot and photos.}
    \vhEntry{0.1}{2015-02-02}{A.\ Uthor}{First version.}
\end{versionhistory}    
\blindtext  
\end{document}

Please let me know, if I understood you wrong.

1
  • Sorry for me not being clear. I dont want to be a float. My goal is to pit this table in the front page of a report only if is not higher of, say, 7\baselineskip. I want to be able to check this height and programaticaly insert a newpage.
    – TeXtnik
    Commented Mar 16, 2015 at 15:19

You must log in to answer this question.

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