制作一个长表,长源位于表底部

制作一个长表,长源位于表底部

我想longtable在表格底部制作一个长源。我尝试编写如下代码。

\documentclass{book}
\usepackage{tabularx,cellspace,longtable,makecell}
\begin{document}
\begingroup
    \setcellgapes{5pt}
    \makegapedcells
    \begin{longtable}{|c|c|c|c|}
        \caption{Test Table.}
        \label{test} \\
        \hline
        \textbf{No} & \textbf{A} & \textbf{B} & \textbf{C} \\
        \hline
        \endhead
        \multicolumn{4}{@{} l}{\scriptsize {Source: Source 2000, Source 2001, Source 2002, Source 2003, Source 2004, Source 2005, Source 2006, Source 2007, Source 2008, Source 2009, Source 2010, Source 2011, Source 2012, Source 2013, Source 2014, Source 2015, Source 2016, Source 2017, Source 2018, Source 2019, Source 2020, Source 2021, Source 2022}} \\
        \endfoot
        \hline
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        \hline
    \end{longtable}
    \endgroup
\end{document}

在此处输入图片描述

在此处输入图片描述

源超出边界。如果超出边界,我希望源以换行符显示。如何实现?

答案1

一个快速的解决方案是使用\multicolumn{4}{@{} p{\textwidth} @{}}

多柱解决方案

\documentclass{book}
\usepackage{tabularx,cellspace,longtable,makecell}

\begin{document}
Dummy text for new page
\newpage

\begingroup
\setcellgapes{5pt}
\makegapedcells
\noindent
\begin{longtable}{|c|c|c|c|}
    \caption{Test Table.}
    \label{test} \\
    \hline
    \textbf{No} & \textbf{A} & \textbf{B} & \textbf{C} \\
    \hline
    \endhead
    \multicolumn{4}{@{} p{\textwidth} @{}}{\scriptsize {Source: Source 2000, Source 2001, Source 2002, Source 2003, Source 2004, Source 2005, Source 2006, Source 2007, Source 2008, Source 2009, Source 2010, Source 2011, Source 2012, Source 2013, Source 2014, Source 2015, Source 2016, Source 2017, Source 2018, Source 2019, Source 2020, Source 2021, Source 2022}} \\
    \endfoot
    \hline
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    \hline
\end{longtable}
\endgroup
\end{document}

我建议使用longtblr来自的环境tabularray。这样,您可以remark为源定义一个,它只会出现在最后一页。

tabularray 解决方案

\documentclass{book}

\usepackage{tabularray}

\begin{document}
Dummy text for new page
\newpage

\begin{longtblr}[
    caption={Test Table.},
    label={test},
    remark{Source}={Source 2000, Source 2001, Source 2002, Source 2003, Source 2004, Source 2005, Source 2006, Source 2007, Source 2008, Source 2009, Source 2010, Source 2011, Source 2012, Source 2013, Source 2014, Source 2015, Source 2016, Source 2017, Source 2018, Source 2019, Source 2020, Source 2021, Source 2022}
    ]{
        colspec={*{4}{X[c]}}, vlines,
        row{1}={font=\bfseries},
        rowhead=1,
    }
    \hline
    No & A & B & C \\
    \hline
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    \hline
\end{longtblr}
\end{document}

请注意,tabularray默认情况下具有更好的垂直间距。因此无需使用cellspacemakecell包。您可以查看tabularray 文档获得更多选项来自定义布局。

但是有一个警告。remark将根据表格的宽度进行排版,如果您的表格较窄,这可能会带来问题。我使用X列类型来人为地扩大表格。由于这只是一个示例,我无法判断这是否会在您的实际表格中带来问题。

编辑每页都有备注

如果你希望源代码出现在每个页面上,你可以使用以下代码(灵感来自邮政)。

\documentclass{book}
\usepackage{tabularx,cellspace,longtable,makecell}

\usepackage{tabularray}

% Notes and remarks on every page
\DefTblrTemplate{firstfoot, middlefoot}{default}{%
    \UseTblrTemplate{note}{default}
    \UseTblrTemplate{remark}{default}
    \UseTblrTemplate{contfoot}{default}
}


\begin{document}
Dummy text for new page
\newpage

\begin{longtblr}[
        caption={Test Table.},
        label={test},
        remark{Source}={Source 2000, Source 2001, Source 2002, Source 2003, Source 2004, Source 2005, Source 2006, Source 2007, Source 2008, Source 2009, Source 2010, Source 2011, Source 2012, Source 2013, Source 2014, Source 2015, Source 2016, Source 2017, Source 2018, Source 2019}
    ]{
        colspec={*{4}{X[c]}}, vlines,
        row{1}={font=\bfseries},
        rowhead=1,
    }
    \hline
    No & A & B & C \\
    \hline
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    1&A&B&C\\
    2&D&E&F\\
    \hline
\end{longtblr}

\end{document}

每页都备注

答案2

您需要 (a) 使用或pw类型为各个列设置固定宽度,以及 (b) 计算表的整体宽度,以便您可以将此信息传递给指令\endfoot

在此处输入图片描述

\documentclass{book}
\usepackage{array} % for 'w' column type
\usepackage{cellspace,longtable,makecell}

\newlength\colwidth % create a new length variable
\setlength\colwidth{2.5cm} % <-- set the column width as needed
\newlength\tablewidth % now calculate the width of the overall table:
\setlength\tablewidth{\dimexpr4\colwidth+8\tabcolsep+5\arrayrulewidth\relax}

\begin{document}
\begingroup
    \setcellgapes{5pt}
    \makegapedcells
    \begin{longtable}{| *{4}{wc{\colwidth}|}}

        %% header and footer
        \caption{Test Table.} \label{test} \\
        \hline
        \textbf{No} & \textbf{A} & \textbf{B} & \textbf{C} \\
        \hline
        \endhead

        \hline
        \multicolumn{4}{@{} p{\tablewidth} @{}}{\scriptsize Source: Source 2000, Source 2001, Source 2002, Source 2003, Source 2004, Source 2005, Source 2006, Source 2007, Source 2008, Source 2009, Source 2010, Source 2011, Source 2012, Source 2013, Source 2014, Source 2015, Source 2016, Source 2017, Source 2018, Source 2019, Source 2020, Source 2021, Source 2022} \\
        \endfoot

        %% body of table
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\
        1&A&B&C\\
        2&D&E&F\\

    \end{longtable}
\endgroup
\end{document}

相关内容