2

I want to make a longtable with long source in bottom of table. I try to make code as below.

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

enter image description here

enter image description here

The source is exceed margin. I want source in newline if exceed margin. How to make it?

3
  • Do you want the "Source: ..." statement to occur at the bottom of each page, or just at the very bottom of the entire table?
    – Mico
    Jan 21 at 8:03
  • 1
    I want "Source:" in every bottom of each page Jan 21 at 8:08
  • 1
    Please see the answer I just posted.
    – Mico
    Jan 21 at 8:24

2 Answers 2

6

A quick solution would be to use \multicolumn{4}{@{} p{\textwidth} @{}}

multicolumn solution

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

I would recommend to use the longtblr environment from tabularray for this. With that, you can define a remark for your source, which will only appear on the last page.

tabularray solution

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

Note that tabularray has better vertical spacing by default. Therefore there is no need to use cellspace or makecell package. You can have a look at the tabularray documentation for more options to customize the layout.

There is however one caveat. The remark will be typeset on the width of the table, which could be a problem, if your table is narrow. I used the X column type to artifically enlarge the table. Since this is just an example, I cannot tell if this will be a problem in your actual table.

Edit Remark on every page

If you want the source to appear on every page, you can use the following code (inspired by this post).

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

remark every page

4
  • Fourth column too wide. I want equal size for every column. Jan 21 at 8:09
  • @OngkyDennyWijaya are you sure you want all equal size? shouldn't the No column be much smaller?
    – marv
    Jan 21 at 8:16
  • @OngkyDennyWijaya i have edited the answer to include equal size columns. Also I added the option to have the source on every page with longtblr solution
    – marv
    Jan 21 at 8:27
  • Yes, I want equal size. Jan 21 at 8:28
3

You need to (a) set fixed widths for the individual columns, by using either the p or the w column type, and (b) calculate the overall width of the table, so that you can pass this information to the \endfoot directive.

enter image description here

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.