4

I want a caption of two lines, with the second line not centered but aligned to the left.

enter image description here

This is the result of the following code, \captionsetup{justification=raggedright} doesn't seem to work. What am I doing wrong?

\documentclass[a4paper,twoside,12pt]{scrreprt}
\usepackage{fontspec}
\usepackage{caption}
\usepackage{booktabs}
\begin{document}
\begin{table}
  \centering
  \captionsetup{justification=raggedright}
\begin{tabular}{cc}
  \toprule
  A & B 
\\ \midrule
  11111111111111111  & 1111111111111111\\
  22222222222222222  & 2222222222222222\\
  33333333333333333  & 3333333333333333\\
  44444444444444444  & 4444444444444444\\
  55555555555555555  & 5555555555555555\\
  66666666666666666  & 6666666666666666\\
\bottomrule
\end{tabular}
\caption{Caption caption caption caption\\
caption caption}
\end{table}
\end{document}
  • Did you try \captionsetup{width=0.5\linewidth,justification=raggedright} – Jesse Jun 20 '14 at 10:18
  • 1
    Would this help? tex.stackexchange.com/q/128485/34618 – Jesse Jun 20 '14 at 10:19
  • Interestingly it also seems to depend on the length of the caption. If I add two extra caption after the `\` then it is left aligned, but only one, and the last line is centered. – daleif Jun 20 '14 at 13:21
  • The trick from this question tex.stackexchange.com/questions/120411/…, seems to help. No idea though why this check interfere – daleif Jun 20 '14 at 13:24
  • or you could just use a \parbox inside the caption. – John Kormylo Jun 23 '14 at 14:28
1

If you use a threeparttable environment, it computes the width of your table, and it works either with the plain or the hang formats:

 \documentclass[a4paper,twoside,12pt]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{fourier, heuristica}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{threeparttablex}

\begin{document}

\begin{table}[!h]
\captionsetup{format =plain}%
\centering
\begin{threeparttable}
\begin{tabular}{cc}
\toprule
A & B
\\ \midrule
11111111111111111 & 1111111111111111\\
22222222222222222 & 2222222222222222\\
33333333333333333 & 3333333333333333\\
44444444444444444 & 4444444444444444\\
55555555555555555 & 5555555555555555\\
66666666666666666 & 6666666666666666\\
\bottomrule
\end{tabular}
\caption{Caption caption caption caption\\
caption caption. }
\end{threeparttable}
\end{table}

\begin{table}[!h]
\captionsetup{format =hang}%
\centering
\begin{threeparttable}
\begin{tabular}{cc}
\toprule
A & B
\\ \midrule
11111111111111111 & 1111111111111111\\
22222222222222222 & 2222222222222222\\
33333333333333333 & 3333333333333333\\
44444444444444444 & 4444444444444444\\
55555555555555555 & 5555555555555555\\
66666666666666666 & 6666666666666666\\
\bottomrule
\end{tabular}
\caption{Caption caption caption caption\\
caption caption. }
\end{threeparttable}
\end{table}\end{document} 

Note that it doesn't seem to work with the ThreePartTableenvironment (from threeparttablex), so that you can't break your tables through pages.

enter image description here

1

Hang is the standard caption format in KOMA-Script classes. Therefore you only have to measure the width of the float und to set \setcapwidth to the result. The caption package is not needed.

\documentclass[twoside,12pt]{scrreprt}
\usepackage{fontspec}
\usepackage{booktabs}

\newlength\floatwidth
\newcommand\MyFloat[1]{%
  \settowidth\floatwidth{#1}%
  \setcapwidth[c]{\floatwidth}%
  #1%
}

\begin{document}
\begin{table}
  \centering
  \MyFloat{%
    \begin{tabular}{cc}
      \toprule
      A & B\\
      \midrule
      11111111111111111  & 1111111111111111\\
      22222222222222222  & 2222222222222222\\
      33333333333333333  & 3333333333333333\\
      44444444444444444  & 4444444444444444\\
      55555555555555555  & 5555555555555555\\
      66666666666666666  & 6666666666666666\\
      \bottomrule
    \end{tabular}%
  }
  \caption{Caption caption caption caption caption caption.}
\end{table}
\end{document}

enter image description here

Note: If there should be a manual line break inside the caption you have to use

  \caption{Caption caption caption caption \protect\\ caption caption.}

or maybe better

  \caption[Caption caption caption caption caption caption.]{%
           Caption caption caption caption \\ caption caption.}

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.