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...)

4
  • 3
    With use of threeparttable. Please extend your code snišped to complete small document (MWE) that we can more easy show how.
    – Zarko
    Feb 1, 2021 at 12:15
  • 1
    The caption always fills the available width (with white space). Another solution is to reduce the width using a minipage (once you know how wide the tabular is). Feb 1, 2021 at 12:29
  • Zarko, Like this? Sorry if I am mistaken - I am new to tex.stackexchange. Feb 1, 2021 at 12:55
  • @JohnKormylo, how can I obtain the width of the table? Feb 1, 2021 at 13:13

2 Answers 2

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

2
  • 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? Feb 1, 2021 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). Feb 1, 2021 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}

You must log in to answer this question.

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