3

I have

\documentclass{article}

\usepackage{longtable}

\begin{document}

\begin{longtable}{|c|c|} 
\caption{A caption.}\label{tab1} \\ \hline
1 & 2 \\ \hline
3 & 4 \\ \hline
\end{longtable}

\end{document}

How can I get the caption to look like

{\bfseries Table 1.} {\itshape A caption.}

(so, "Table 1." in bold and "A caption." in italic)?

Even better, could I get {\bfseries Table 1} on one line, {\itshape A caption.} on the next line, and both lines centered?

(Edited to replace deprecated commands.)

MSC
  • 2,722
  • BTW: You should not use \bf and \it in modern LaTeX document. It's a deprecated command, it should not be used for multiple reasons - http://tex.stackexchange.com/questions/41681/correct-way-to-bold-italicize-text – Kiraa Jul 31 '15 at 15:28
  • Okay, I've replaced those commands. – MSC Aug 03 '15 at 12:37

2 Answers2

5

You can use caption package to do so. Then use captionsetup to format the caption to your likings.

\documentclass{article}

\usepackage{longtable}
\usepackage{caption}

\begin{document}

\captionsetup[longtable]{labelfont=bf,textfont=it,labelsep=newline}

\begin{longtable}{|c|c|} 
\caption{A caption.}\label{tab1} \\ \hline
1 & 2 \\ \hline
3 & 4 \\ \hline
\end{longtable}

\end{document}
Kiraa
  • 647
  • Thanks. Could anyone say how to do it without the caption package? With the actual document class I'm using, the caption package tends to cause trouble. – MSC Aug 03 '15 at 12:42
  • Can I ask what problems does caption package causes? It may be easier and cleaner to resolve those. – Kiraa Aug 03 '15 at 13:49
3

Per the query you left on @Kiraa's answer, here's a solution that modifies the macro \LT@makecaption of the longtable package directly. The solution uses the \patchcmd macro of the etoolbox package to perform the required surgery on the \LT@makecaption macro.

enter image description here

\documentclass{article}
\usepackage{longtable}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\LT@makecaption}%
   {\@tempboxa{#1{#2: }#3}}
   {\@tempboxa{#1{\textbf{#2. }}\itshape #3}}{}{}
\patchcmd{\LT@makecaption}%
   {#1{#2: }#3}
   {#1\textbf{#2. }{\itshape #3}}{}{}
\makeatother

\begin{document}
\begin{longtable}{|c|c|} 
\caption{A caption.} \label{tab1}  \\
\hline
1 & 2 \\ \hline
3 & 4 \\ \hline
\end{longtable}
\end{document}

To get a linebreak after the Table <x> part, you could use the following code (to be inserted in the preamble in lieu of the patch code shown above):

\usepackage{etoolbox,ragged2e}
\makeatletter
\patchcmd{\LT@makecaption}%
   {\@tempboxa{#1{#2: }#3}}
   {\@tempboxa{#1{\textbf{#2.\ }}\itshape #3}}{}{}
\patchcmd{\LT@makecaption}%
   {#1{#2: }#3}
   {\Centering #1\textbf{#2.}\par{\itshape #3}}{}{}
\patchcmd{\LT@makecaption}%
   {\hbox to\hsize{\hfil\box\@tempboxa\hfil}}
   {\Centering #1\textbf{#2.}\par{\itshape #3}}{}{}
\makeatother
Mico
  • 506,678