2

A document of mine ignored the \setcapwidth parameters that I set in order to get a more narrow caption. After a long search, I found that it started to work when I commented out the line \setkomafont{caption}{\sffamily\footnotesize}.

This is the output with the line not commented out: Line not commented out.

This is the output with the line commented out:

Line commented out.

Am I doing something wrong? What do I have to do to use both?

Here is the Minimal Working Example:

\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{booktabs}

\setkomafont{caption}{\sffamily\footnotesize} % This is the line in question

\begin{document}

\begin{table}[tbp]
\footnotesize
  \centering
  \setcapwidth[c]{8cm}
  \caption{This is a rather long caption for a table that is comparatively narrow --- but it's a test}  
  \label{table:setcapwidth_table}  
  \begin{tabular}{@{} l r r @{}}
    \toprule
        Row 1 & Row 2 & Row 3 \\
    \midrule
        2011& 10,000 & 10,000 \\ 
        2012 & 10,000 & 10,000 \\
        2013 & 10,000 & 10,000 \\
    \bottomrule
  \end{tabular}
\end{table}

\end{document}

EDIT: What I marked as "Rows" are obviously "Columns"...

1

1 Answer 1

2

Adding the option:

[captions=nooneline]

fixes the problem.

I removed captions=, when I revised the answer (added more caption option), and it stopped working again. Confusing. If you remove \footnotesize, it also works as expected. This is not a bug, as @esdd explain in the comment below. With default captions=oneline, \setcapwidth only effects captions that is more than one line, i.e: wider than the text width. So when you reduce font size as you did, the caption fits within the text width again, and becomes a one line caption, even if it is wider than the value you gave \setcapwidth. It will not be changed by \setcapwidth. Good to know!

In stead of [captions=nooneline], you may use a centred, \minipage-environment of the width you prefer (here 8cm), and centre the table within that environment:

Example 1 – centred minipage

enter image description here

\documentclass[captions=nooneline, captions=tableabove]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{booktabs, relsize}

\setkomafont{caption}{\sffamily\footnotesize} % This is the line in question

\begin{document}

\begin{table}[tbp]
\centering
  \footnotesize
  \begin{minipage}{8cm}
\centering
  \caption{This is a rather long caption for a table that is comparatively narrow --- but it's a test}  
  \label{table:setcapwidth_table}  
  \begin{tabular}{@{} l r r @{}}
    \toprule
        Row 1 & Row 2 & Row 3 \\
    \midrule
        2011& 10,000 & 10,000 \\ 
        2012 & 10,000 & 10,000 \\
        2013 & 10,000 & 10,000 \\
    \bottomrule
  \end{tabular}
\end{minipage}

\end{table}

\end{document}

Example 2 – threeparttable

Also, you may enclose the tabular-environment in a threeparttable if you prefer to match the width of the caption with the width of the tabular. As an added benefit, you may have nice, integrated table notes.

enter image description here

\documentclass[captions=nooneline, captions=tableabove]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{booktabs, threeparttable}

\setkomafont{caption}{\sffamily\footnotesize} % This is the line in question

\begin{document}

\begin{table}[tbp]
\footnotesize
  \centering
 \begin{threeparttable}
  \caption{This is a rather long caption for a table that is comparatively narrow --- but it's a test}  
  \label{table:setcapwidth_table}  
  \begin{tabular}{@{} l r r @{}}
    \toprule
        Row 1 & Row 2 & Row 3 \\
    \midrule
        2011& 10,000 & 10,000 \\ 
        2012 & 10,000 & 10,000 \\
        2013 & 10,000 & 10,000 \\
    \bottomrule
  \end{tabular}
\end{threeparttable}
\end{table}

\end{document}
4
  • Unfortunately, the threeparttable solution does not work for me: I want the caption to be wider than the table, just not as long as the whole line. I foundthese solutions, but when I adjusted \TPTminimum to give more space to the caption, the table was not centered anymore. And when I put the caption outside of threeparttable (one of the suggested solutions), I am back to square one.
    – anbeck
    Jan 5, 2020 at 19:05
  • 1
    @anbeck It is possible to use a \minipage-environment, see updated answer.
    – Sveinung
    Jan 5, 2020 at 19:30
  • 1
    I think adding captions=nooneline made it work for me. But as you said you had problems with it, I'll do some more tests.
    – anbeck
    Jan 5, 2020 at 19:34
  • With the default setting caption=oneline the usage of \setcapwidth affects only captions wider than \textwidth. And without \footnotesize (i.e. with \normalsize) the caption in the question is wider than \textwidth. While there is no really need to use a minipage with option captions=nooneline, the minipage solves the issue with the default captions=online. Then a caption smaller than the argument of \setcapwidth will be still centered above the table.
    – esdd
    Jan 5, 2020 at 22:26

You must log in to answer this question.

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