4

I want to left-align the caption of a table.

I read How can I left-align a caption? and the manual without luck.

This code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[font=small,labelfont=bf]{caption}

\begin{document}

\begin{table} \centering \captionsetup{singlelinecheck = false, justification=justified} \caption{System specifications} \label{tab:specs} \begin{tabular}{ll} Processor & Intel(R) Core(TM) i7-7500U CPU @2.70GHz 2.90 GHz \ Installed RAM & 8.00 GB (7.89 GB usable) \end{tabular} \end{table}

\end{document}

Creates this:

enter image description here

I want the caption to be justified with the left border of the table itself and not the paragraph:

enter image description here

(NB: picture above is edited in MS paint...)

2 Answers2

3

Note the addition of @{} to each side of the tabular.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[font=small,labelfont=bf]{caption}

\begin{document}

\begin{table} \sbox0{\begin{tabular}{@{}ll@{}} Processor & Intel(R) Core(TM) i7-7500U CPU @2.70GHz 2.90 GHz \ Installed RAM & 8.00 GB (7.89 GB usable) \end{tabular}}% \centering \begin{minipage}{\wd0} \captionsetup{singlelinecheck = false, justification=justified} \caption{System specifications} \label{tab:specs} \usebox0 \end{minipage} \end{table}

\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Aha! Thanks! So the intuition is that you create a box with the tabular inside. This box is then referred to inside the centered minipage (relative to the paragraph), which allows the justification to refer to the end of the box instead of the paragraph? – DisabledWhale Feb 01 '21 at 13:41
  • 1
    You can also use \settowidth or \widthof (calc package), but internally they are using saveboxes. This way you only have to format the box once. Note: one should only use box registers 0-9 inside groups (environments). – John Kormylo Feb 01 '21 at 22:31
3

I suggest you (a) keep loading the caption package and (b) employ a threeparttable environment to encase both the \caption directive and the tabular environment. It measures the width of the tabular environment and limits the width of the caption string to that width.

enter image description here

\documentclass{article}
\usepackage{caption}
\usepackage{threeparttable} % see https://ctan.org/pkg/threeparttable
\begin{document}
\begin{table}[h!]
    \centering
    \captionsetup{singlelinecheck = false, justification=raggedright}
    \begin{threeparttable}
    \caption{System specifications}
    \begin{tabular}{@{} ll @{}}
         Processor & Intel(R) Core(TM) i7-7500U CPU @2.70\,GHz 2.90\,GHz \\
         Installed RAM & 8.00 GB (7.89 GB usable)
    \end{tabular}
    \end{threeparttable}
\end{table}
\end{document}
Mico
  • 506,678