0

When I use ThreePartTable from the threeparttablex package, then the caption does not respect the width of the tabular as it does with regular threeparttable. How could I accomplish that? Or am I doing something wrong...?

\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage[referable]{threeparttablex}
\begin{document}
\begin{ThreePartTable}
\begin{TableNotes}
\item[a] \label{tn:a} test test test test test test test test
\item[b] \label{tn:b} test2
\source Made up by daleif
\end{TableNotes}
\begin{longtable}{l l l}
\caption{A \texttt{ThreePartTable} with a title that is long enough to wrap}\\
\toprule
Coloum 1 & & Column 2 \\
\midrule
\endhead
\cmidrule{3-3}
\multicolumn{3}{r}{\textit{continued}}
\endfoot
\bottomrule
\insertTableNotes\\
\endlastfoot
AAAA\tnotex{tn:a} &
& BBBB \\
%\newpage
CC
&
& DD\tnotex{tn:b}
\\
\end{longtable}
\end{ThreePartTable}
\begin{center}
\begin{threeparttable}
\caption{A \texttt{threeparttable} with a title that is long enough to wrap}
\begin{tabular}{l l l}
\toprule
Coloum 1 & & Column 2 \\
\midrule
AAAA\tnote{c} &
& BBBB \\
CC
&
& DD\tnotex*{tn:c}
\\
\bottomrule
\end{tabular}
\begin{tablenotes}
\footnotesize
\item[c] test test test test test test test test
\item[d] \label{tn:c} the reference to this is not a link
\note Some general note
\end{tablenotes}
\end{threeparttable}
\end{center}
\end{document}

Output

3
  • There is not much you can do about that. Here ThreePartTable actually does nothing, it just gives the doc a better structure. We cannot really set the width here in such a way that longtable can still break across pages. Personally I don't really see it as an issue to have a caption that is wider than the table. For the normal threeparttable env I will often have the caption outside.
    – daleif
    Oct 11, 2023 at 12:17
  • @daleif Personally, I really like the neatness of having all of a table’s components snuggly fit into a little visual box. I suppose that I will have to look into if this can be done with tabularray.
    – Fredrik P
    Oct 11, 2023 at 12:25
  • @daleif I guess an alternative could be using a tabularx for the tabular data and then the caption package and something like \caption[width=4cm]{…}, but I would have to check…
    – Fredrik P
    Oct 11, 2023 at 12:30

2 Answers 2

2

It is not pretty, but it can be done. You need to extract the width from longtable. And then set the caption width explicitly. This is how the \insertTableNotes knows the width of the table.

\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable,caption}
\usepackage[referable]{threeparttablex}
\makeatletter
\newcommand{\FixCaptionWidth}{\noalign{\begingroup%
  \setlength\TPTL@width{0pt}%
  \renewcommand\LT@entry[2]{\global\advance\TPTL@width by ##2}%
  \@nameuse{LT@\romannumeral\c@LT@tables}%
  \ifdim\TPTL@width<\TPTminimum\relax\global\TPTL@width=\TPTminimum\fi%
  \endgroup}%
  \captionsetup{width=\TPTL@width}%
}
\makeatother
\begin{document}
\begin{ThreePartTable}
\begin{TableNotes}
\item[a] \label{tn:a} test test test test test test test test
\item[b] \label{tn:b} test2
\source Made up by daleif
\end{TableNotes}
\begin{longtable}{l l l}
\FixCaptionWidth
\caption{A \texttt{ThreePartTable} with a title that is long enough to wrap}\\
\toprule
Coloum 1 & & Column 2 \\
\midrule
\endhead
\cmidrule{3-3}
\multicolumn{3}{r}{\textit{continued}}
\endfoot
\bottomrule
\insertTableNotes\\
\endlastfoot
AAAA\tnotex{tn:a} &
& BBBB \\
%\newpage
CC
&
& DD\tnotex{tn:b}
\\
\end{longtable}
\end{ThreePartTable}  
\begin{center}
\begin{threeparttable}
\caption{A \texttt{threeparttable} with a title that is long enough to wrap}
\begin{tabular}{l l l}
\toprule
Coloum 1 & & Column 2 \\
\midrule
AAAA\tnote{c} &
& BBBB \\
CC
&
& DD\tnotex*{tn:c}
\\
\bottomrule
\end{tabular}
\begin{tablenotes}
\footnotesize
\item[c] test test test test test test test test
\item[d] \label{tn:c} the reference to this is not a link
\note Some general note
\end{tablenotes}
\end{threeparttable}
\end{center}
\end{document}
2
  • Could the ugliness (\noalign…) be wrapped in a command? So that I could reuse it and not bother with the \makeat… commands?
    – Fredrik P
    Oct 11, 2023 at 12:46
  • @FredrikP Sure, I was just in a hurry
    – daleif
    Oct 11, 2023 at 14:41
2

I suspect that you after something like this:

enter image description here

This can be simple achieve by use of the longtblr of tabularray package:

\documentclass{article}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%
\usepackage{lipsum}             % for dummy text
%---------------------------------------------------------------%

\usepackage{tabularray}
\UseTblrLibrary{booktabs}
    \SetTblrStyle{contfoot-text}{font=\footnotesize\itshape}
    \SetTblrStyle{note}{font=\footnotesize}
    \SetTblrStyle{remark}{font=\footnotesize}
    
\begin{document}
\lipsum[1-4]

    \begin{longtblr}[
caption = {A \texttt{ThreePartTable} with a title that is long enough to wrap},
  label = {longtab:?},
%
note{a} = {test test test test test test test test} ,
note{b} = {test 2},
remark{Source} = {Made up by daleif.}
                    ]{colspec = {*{2}{Q[l, wd=6em]}},
                      rowhead = 1
                      }
    \toprule
Colum 1             &   Column 2           \\
    \midrule
AAAA\TblrNote{a}    &   BBBB               \\
CC                  &   DD DdD DD\TblrNote{b}    \\
    \bottomrule
    \end{longtblr}
\end{document}¸

The longtblr combine features of the threeparttable and longtable packages. Based on LaTeX3 has a bit different syntax known at "classic" tables. It is thoroughly described in the package documentation and can be easy to learn.

From above MWE can be seen that code is simple, concise and short, however it not enable referencing the table notes in document text.

Edit: Tables caption so far consider default formation (which support only hanging format) but it can be changed that caption can consider formatting defined in the caption packages:

\documentclass{article}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%
\usepackage{lipsum}             % for dummy text
%---------------------------------------------------------------%

\usepackage{tabularray}
\UseTblrLibrary{booktabs}
    \SetTblrStyle{contfoot-text}{font=\footnotesize\itshape}
    \SetTblrStyle{note}{font=\footnotesize}
    \SetTblrStyle{remark}{font=\footnotesize}
\usepackage{caption}   % <---
\captionsetup[table]{skip=0pt,
                     font = {small, sf},
                     labelfont = bf,
                    %singlelinecheck = off
                    }
\NewTblrTheme{captionof}% <---
{\DefTblrTemplate{caption}{default}%
    {\addtocounter{table}{-1}%
     \captionof{table}{\InsertTblrText{caption}}
    }
 \DefTblrTemplate{capcont}{default}%
    {\addtocounter{table}{-1}%
     \captionof{table}{\InsertTblrText{caption} (continue)}
    }
}
    
\begin{document}
\lipsum[1-4]

A case when caption is defined by \verb+caption+ package:
    \begin{longtblr}[
  theme = captionof,  % <---
caption = {A \texttt{ThreePartTable} with a title that is long enough to wrap.},
  label = {longtab:?},
%
note{a} = {test test test test test test test test  \label{tn:a}} ,
note{b} = {test 2  \label{tn:b}},
remark{Source} = {Made up by daleif.}
                    ]{colspec = {*{2}{Q[l, wd=7em]}},
                      rowhead = 1
                      }
    \toprule
Colum 1             &   Column 2           \\
    \midrule
AAAA\TblrNote{a}    &   BBBB               \\
CC                  &   DDD DDD DD\TblrNote{b}    \\
    \bottomrule
    \end{longtblr}  
\end{document}

enter image description here

You must log in to answer this question.

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