将 longtabu 保存在盒子中,以便检查和重复使用

将 longtabu 保存在盒子中,以便检查和重复使用

我想测量一张桌子的高度,\newpage如果桌子太高,则在它前面插入一个。我的桌子已经用完了。我应该从哪里开始longtabutabu

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

答案1

这里,我使用\vbox\bgroup...\egroup方法来保存环境中的框。在环境 aftercode 中,我检查框的高度,并且只有当其小于7\baselineskip高度时才输出。否则,我会将其保存到下一页。

在 MWE 中,我打印出有用的诊断信息来显示正在展开的逻辑,但显然您会将其从生产版本中删除。

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

第 1 页

在此处输入图片描述

和第 2 页

在此处输入图片描述

答案2

只要将它放入table环境中,它就会执行您想要的操作:

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

如果我理解错了,请告诉我。

相关内容