1

When the caption is too long to fit in just one line, I want something like this:

Table 1: whateverj lakjsdfñ lkjasdfl kjasñdlf j
         wastathahtaht

Table 1 (cont.): Wahtashetlkahkethjaklthjalkdthjalk lkjwñtjqñjeñtqljetñljk

I have tried using:

\begin{longtable}{c|c}
  \caption{whateverj lakjsdfñ lkjasdfl kjasñdlf j wastathahtaht}
  \label{tab:my}\\
  \toprule
  a & b \\
  \midrule
  \endfirsthead

\caption*{\textbf{Table \ref{tab:mytab} (cont.):} Wahtashetlkahkethjaklthjalkdthjalk lkjwñtjqñjeñtqljetñljk}\ \toprule a & b \ \midrule \endhead l1 & l2 \ \end{longtable}

But instead I'm getting:

Table 1: whateverj lakjsdfñ lkjasdfl kjasñdlf j
         wastathahtaht

Table 1 (cont.): Wahtashetlkahkethjaklthjalkdthjalk lkjwñtjqñjeñtqljetñljk

How can I indent the continuation caption as well? I'm using KOMA scrbook and longtable package.

jlanza
  • 1,249
  • 1
    Longtable uses a custom \caption (see page 4 of manual). – John Kormylo Jan 20 '22 at 16:12
  • @JohnKormylo could you give a hint? Do I have to set the caption as a multicolumn, with 2 columns and define the 1st column to include Talble x cont and the other as the real text caption? – jlanza Jan 21 '22 at 08:16

2 Answers2

2

You can do it with \multicolumn but it is not as good as using \caption*. However, \LTcapwidth was useful.

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

\newsavebox{\mycaptionbox}

\newcommand{\myfirstcaption}[1]{\caption*{\savebox{\mycaptionbox}{Table \thetable: }% \usebox\mycaptionbox\parbox[t]{\dimexpr \LTcapwidth-\wd\mycaptionbox}{#1}}}

\newcommand{\mycaption}[1]{\caption*{\savebox{\mycaptionbox}{Table \thetable~(cont.): }% \usebox\mycaptionbox\parbox[t]{\dimexpr \LTcapwidth-\wd\mycaptionbox}{#1}}}

\begin{document} \begin{longtable}{c|c} \myfirstcaption{\blindtext} \label{tab:my}\ \toprule a & b \ \midrule \endfirsthead \mycaption{\blindtext}\ \toprule a & b \ \midrule \endhead l1 & l2 \ \rule{1em}{29\normalbaselineskip}% too big for first page \end{longtable} \end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
1

Here is an attempt, which uses the longtblr environment from tabularray package.

table output

\documentclass{scrbook}

\usepackage{lipsum} \usepackage{booktabs} \usepackage{tabularray} \UseTblrLibrary{booktabs}

\ExplSyntaxOn \DefTblrTemplate { capcont } { mycaption } { \hbox_set:Nn \l__tblr_caption_box { \UseTblrTemplate { caption-tag } { default } \space \UseTblrTemplate { conthead-text } { default } \UseTblrTemplate { caption-sep } { default } \UseTblrTemplate { caption-text } { default }
} \dim_compare:nNnTF { \box_wd:N \l__tblr_caption_box } > { \hsize } { \UseTblrAlign { capcont } \UseTblrIndent { capcont } \hbox_set:Nn \l__tblr_caption_left_box { \UseTblrTemplate { caption-tag } { default } \space \UseTblrTemplate { conthead-text } { default } \UseTblrTemplate { caption-sep } { default } } \hangindent = \box_wd:N \l__tblr_caption_left_box \hangafter = 1 \UseTblrHang { capcont } \leavevmode \hbox_unpack:N \l__tblr_caption_box \par } { \centering \makebox [\hsize] [c] { \box_use:N \l__tblr_caption_box } \par } } \ExplSyntaxOff

\SetTblrTemplate{capcont}{mycaption}

\DefTblrTemplate{conthead-text}{default}{(cont.)}

\begin{document} Extra page to have the correct margins for two page layout \newpage

\noindent
\begin{longtblr}[ caption={This is very very very very very very very very very very very very very very very very very long caption}, label={tab:my} ]{ colspec={X|X}, rowhead=1, } \toprule a & b \ \midrule l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ l1 & l2 \ \bottomrule \end{longtblr}

\end{document}

However, the width of the caption is equal to the width of the table, which could be an issue, if your table is narrow. I used X column type in my example, which uses all available space for the columns. If the width of the caption is a problem for you, you can have a look at this question. Unfortunately, I have not been able to combine both the custom alignment and the full width caption yet. Maybe someone else has an idea...

marv
  • 2,099
  • 1
  • 7
  • 26