1

This question is a follow-on from Automatically bold first sentence of a floats caption. I can automatically bold the first sentence of the caption in my normal tabulars, but the code doesn't seem to work when using longtable (used together with threeparttablex). Obviously I would like all my tables to look the same, irrespective of their method of construction. Does anyone have a solution?

\documentclass[a4paper,12pt]{scrreprt}

\usepackage[greek, english]{babel} 
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage[labelfont=bf, font=small ]{caption}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage{longtable}
\usepackage{adjustbox}
\usepackage{xstring}
\usepackage{etoolbox}
\usepackage[a4paper, bindingoffset=0.5cm, tmargin=2.4cm, bmargin=3.4cm, lmargin=3.5cm, rmargin=2.5cm, footskip=2.4cm]{geometry}

% ---------------------------------------------------------------------------------------------------------------------
% Make first sentence of figure and table captions bold
\makeatletter
\newcommand\formatlabel[1]{%
    \noexpandarg
    \IfSubStr{#1}{.}{%
      \StrBefore{#1}{.}[\firstcaption]%
      \StrBehind{#1}{.}[\secondcaption]%
      \textbf{\firstcaption.} \secondcaption}{%
      #1}%
      }
\patchcmd{\@caption}{#3}{\formatlabel{#3}}
\makeatother
% ---------------------------------------------------------------------------------------------------------------------

\AtBeginEnvironment{longtable}{\small}

\begin{document} 

\begin{table}
\centering
\caption[Table 1]{The first table. Note that the first sentence is in \textbf{bold}.}
\label{tab:Table1}
\begin{adjustbox}{max width=\textwidth}
\begin{threeparttable}
\begin{tabular}{*8{c}}
\toprule
A & B & C & D & E & F & G & H \\
\midrule
Item 1\tnote{a} & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 2 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 3 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
\bottomrule
\end{tabular}
\begin{tablenotes}
\small
\item[a] Item 1 is only available from June to August..
\end{tablenotes}
\end{threeparttable}
\end{adjustbox}
\end{table}

\begin{ThreePartTable}
\begin{TableNotes}
\small
\item[a] Item 1 is only available from June to August.
\end{TableNotes}
\begin{longtable}{*8{c}}
\caption[Table 2]{The second table. Note that the first sentence is \textbf{not} in bold.}
\label{tab:Table2} \\
\toprule
A & B & C & D & E & F & G & H \\
\midrule
\endfirsthead
\caption[Table 2]{The second table (continued).} \\
\toprule
A & B & C & D & E & F & G & H \\
\midrule
\endhead
\midrule
\multicolumn{8}{r}{\textit{Continued \ldots}}
\endfoot
\bottomrule
\insertTableNotes
\endlastfoot
Item 1\tnote{a} & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 2 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 3 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 4 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 5 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 6 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 7 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 8 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 9 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
\end{longtable}
\end{ThreePartTable}

\end{document}

enter image description here

2

probably you should be able to use the caption package hooks, but since you are patching \@caption may as well patch the LT version too:

\documentclass[a4paper,12pt]{scrreprt}

\usepackage[greek, english]{babel} 
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage[labelfont=bf, font=small ]{caption}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage{longtable}
\usepackage{adjustbox}
\usepackage{xstring}
\usepackage{etoolbox}
\usepackage[a4paper, bindingoffset=0.5cm, tmargin=2.4cm, bmargin=3.4cm, lmargin=3.5cm, rmargin=2.5cm, footskip=2.4cm]{geometry}

% ---------------------------------------------------------------------------------------------------------------------
% Make first sentence of figure and table captions bold
\makeatletter
\newcommand\formatlabel[1]{%
    \noexpandarg
    \IfSubStr{#1}{.}{%
      \StrBefore{#1}{.}[\firstcaption]%
      \StrBehind{#1}{.}[\secondcaption]%
      \textbf{\firstcaption.} \secondcaption}{%
      #1}%
      }

\patchcmd{\@caption}{#3}{\formatlabel{#3}}{}{}

\AtBeginDocument{\long\def\LT@c@ption#1[#2]#3{%
\LT@makecaption#1{\csname fnum@\LTcaptype\endcsname}{\formatlabel{#3}}%
\LT@captionlistentry {#2}}}


\makeatother
% ---------------------------------------------------------------------------------------------------------------------

\AtBeginEnvironment{longtable}{\small}

\begin{document} 



\begin{table}
\centering
\caption[Table 1]{The first table. Note that the first sentence is in \textbf{bold}.}
\label{tab:Table1}
\begin{adjustbox}{max width=\textwidth}
\begin{threeparttable}
\begin{tabular}{*8{c}}
\toprule
A & B & C & D & E & F & G & H \\
\midrule
Item 1\tnote{a} & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 2 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 3 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
\bottomrule
\end{tabular}
\begin{tablenotes}
\small
\item[a] Item 1 is only available from June to August..
\end{tablenotes}
\end{threeparttable}
\end{adjustbox}
\end{table}

\begin{ThreePartTable}
\begin{TableNotes}
\small
\item[a] Item 1 is only available from June to August.
\end{TableNotes}
\begin{longtable}{*8{c}}
\caption[Table 2]{The second table. Note that the first sentence is \textbf{not} in bold.}
\label{tab:Table2} \\
\toprule
A & B & C & D & E & F & G & H \\
\midrule
\endfirsthead
\caption[Table 2]{The second table (continued).} \\
\toprule
A & B & C & D & E & F & G & H \\
\midrule
\endhead
\midrule
\multicolumn{8}{r}{\textit{Continued \ldots}}
\endfoot
\bottomrule
\insertTableNotes
\endlastfoot
Item 1\tnote{a} & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 2 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 3 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 4 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 5 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 6 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 7 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 8 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
Item 9 & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX & XXXX \\
\end{longtable}
\end{ThreePartTable}

\end{document}
  • Asked and answered – I love this forum! – Mark Birtwistle Oct 18 '14 at 9:34

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.