[Tex/LaTex] Left align caption of table

captionstables

I try to left align the caption to my table. When I use the according code it is indeed left-aligned but is more left than the table. How could I fix this?

Thanks a lot,

Julia

this is my code:

\documentclass{article}
\usepackage[utf8]{inputenc,caption}
\usepackage{booktabs}
\usepackage{siunitx}
  \captionsetup{labelfont=bf,
              justification=raggedright,
              singlelinecheck=false}
\begin{document}

\begin{table}[htbp]

  \centering

  \caption{Percentage of Missing Values in the Analysis Variables}
\begin{tabular}{lS}
\textbf{Variable} & {Percent missing}\\
\toprule
\textbf{x} & 9.37 \\
\textbf{Lxon} & 0.94 \\
\textbf{x} & 0 \\
\textbf{x} & 0.04 \\
\textbf{x} & 0.34 \\
\textbf{Exl} & 7.93 \\
\textbf{Rxe} & 15.32 \\
\textbf{Sxm} & 8.69 \\
\textbf{Txal} & 4.43 \\
\textbf{x} & 1.33 \\
\textbf{Nxnces} & 0.9 \\
\textbf{xs} & 1.12 \\
\textbf{Fxans} & 0 \\
\textbf{xxation} & 2.4 \\
\textbf{Cxon} & 0.58 \\
\textbf{xg} & 0 \\
\textbf{x x} & 0.22 \\
\textbf{x to x from x o. x} & 1.03 \\
\textbf{x to x} & 0.76 \\
\textbf{x} & 0.99 \\
\textbf{x} & 0.09 \\
\textbf{x} & 0.83 \\
\bottomrule
\end{tabular}%
  \label{tab:addlabel}%
\end{table}%

\end{document}

Best Answer

Citing from the threeparttable package:

This package facilitates tables with titles (captions) and notes. The title and notes are given a width equal to the body of the table (a tabular environment). By itself, a threeparttable does not float, but you can put it in a table or a table* or some other floating environment. (This causes extra typing, but gives more flexibility.)

So, threeparttable package directly addresses your problem. There are other options like \parbox or setting \captionsetup{width=..}, but are less flexible.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs,caption,threeparttable}
\usepackage{siunitx}
\captionsetup{labelfont=bf,
              justification=raggedright,
              singlelinecheck=false}
\begin{document}

\begin{table}[!htbp]
\centering
\begin{threeparttable}      % <===
\caption{Percentage of Missing Values in the Analysis Variables}
\begin{tabular}{>{\bfseries}lS}
    Variable           & {Percent missing} \\ \toprule
    x                  & 9.37              \\
    Lxon               & 0.94              \\
    x                  & 0                 \\
    x                  & 0.04              \\
    x                  & 0.34              \\
    Exl                & 7.93              \\
    Rxe                & 15.32             \\
    Sxm                & 8.69              \\
    Txal               & 4.43              \\
    x                  & 1.33              \\
    Nxnces             & 0.9               \\
    xs                 & 1.12              \\
    Fxans              & 0                 \\
    xxation            & 2.4               \\
    Cxon               & 0.58              \\
    xg                 & 0                 \\
    x x                & 0.22              \\
    x to x from x o. x & 1.03              \\
    x to x             & 0.76              \\
    x                  & 0.99              \\
    x                  & 0.09              \\
    x                  & 0.83              \\ \bottomrule
\end{tabular}%
\end{threeparttable}         % <===
\label{tab:addlabel}%
\end{table}%

\end{document}

enter image description here

Related Question