4

I have this table and I want the caption to be aligned with the table and not on the center of the page. Here is the code:

\begin{table}[H]
    \begin{tabular}{c||c}
      Sistema       & $\delta W_{rev}$                   \\\hline\hline
      Hidrostático  & $-PdV$                             \\
      Elástico 1D   & $\tau dL$                          \\
      Laminas       & $\sigma dA$                        \\
      Dieléctricos  & $Ed\vec{P}$                        \\
      Paramagnético & $\underbrace{\mu_0H}_{B_0}\vec{M}$ \\
    \end{tabular}
    \caption{Trabajo reversible en diferentes sistemas}
\end{table}

Here is the output:

enter image description here

How do I center the caption with the table? I want the table to be in that position but the caption aligned with the table, I don't want to move the table.

Thanks for reading.

5 Answers 5

4

The following first typesets your tabular into a box, then adjusts the width the \caption might take (by using it inside a minipage) to be just as wide as the tabular.

\documentclass{article}

\newsavebox\tabularcaptionbox
\newenvironment{tabularcaption}[1]
  {%
    \edef\tabularcaptioncaption{\unexpanded{#1}}%
    \begin{lrbox}\tabularcaptionbox
  }
  {%
    \end{lrbox}%
    \begin{minipage}{\wd\tabularcaptionbox}%
      \usebox\tabularcaptionbox
      \caption{\tabularcaptioncaption}%
    \end{minipage}%
  }

\begin{document}
\begin{table}
  \begin{tabularcaption}{Trabajo reversible en diferentes sistemas}%
    \begin{tabular}{c||c}
      Sistema       & $\delta W_{rev}$                   \\\hline\hline
      Hidrostático  & $-PdV$                             \\
      Elástico 1D   & $\tau dL$                          \\
      Laminas       & $\sigma dA$                        \\
      Dieléctricos  & $Ed\vec{P}$                        \\
      Paramagnético & $\underbrace{\mu_0H}_{B_0}\vec{M}$ \\
    \end{tabular}
  \end{tabularcaption}
\end{table}
\end{document}

enter image description here

3
  • It gives two errors: Missing number treated as zero and LaTeX Error: Not in outer par mode. Nov 28, 2023 at 8:48
  • @alvaritomendez - I experience no problems at all when I compile the code shown above.
    – Mico
    Nov 28, 2023 at 9:15
  • @alvaritomendez just like Mico mentions, the code above compiles without error. If you get errors it's because of things you don't show in your (n)MWE. I'm afraid there's nothing we can do without further information. You could ask a follow-up question showing a minimal example of the your code in which you adapted the code from this answer that actually shows your problem.
    – Skillmon
    Nov 28, 2023 at 9:18
4

I suggest you load the threeparttable package and enclose both the tabular environment and the \caption command in a threeparttable environment. That way, the width of the caption will be constrained to the width of the associated tabular-like environment.

Separately, I would also simplify the table's look by (a) getting rid of the double-struck vertical rule and (b) replacing the double-struck horizontal rule with a single, but well-spaced, horizontal rule.

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage[T1]{fontenc}
\usepackage[spanish,es-tabla]{babel}

\usepackage{threeparttable} % for 'threeparttable' environment
\usepackage{booktabs}       % for '\midrule' directive
\usepackage{array,amsmath}

\begin{document}

\begin{table}[ht]
\begin{threeparttable}
    \begin{tabular}{ l >{$}c<{$} } % automatic math mode in 2nd column
      Sistema       & \delta W_{\textup{rev}}  \\
      \midrule % <-- create a single, but well-spaced, horizontal rule
      Hidrostático  & -P\,dV                           \\
      Elástico 1D   & \tau\,dL                         \\
      Laminas       & \sigma\,dA                       \\
      Dieléctricos  & E\,d\vec{P}                      \\
      Paramagnético & \underbrace{\mu_0H}_{B_0}\vec{M} 
    \end{tabular}
    \caption{Trabajo reversible en diferentes sistemas}
\end{threeparttable}
\end{table}

\end{document}
4

I suggest to use {NiceTabular} of nicematrix which has a key caption for the caption. The caption is wrapped to the width of the tabular.

\documentclass{article}
\usepackage{caption}

\usepackage{nicematrix}

\begin{document}

\begin{table}
\centering
    \begin{NiceTabular}{c||c}[caption = Trabajo reversible en diferentes sistemas]
      Sistema       & $\delta W_{rev}$                   \\\hline\hline
      Hidrostático  & $-PdV$                             \\
      Elástico 1D   & $\tau dL$                          \\
      Laminas       & $\sigma dA$                        \\
      Dieléctricos  & $Ed\vec{P}$                        \\
      Paramagnético & $\underbrace{\mu_0H}_{B_0}\vec{M}$ \\
    \end{NiceTabular}
\end{table}

\end{document}

Output of the above code

2

I'd like to suggest a couple of changes. Regarding the rules and bars, it's possible to adjust double-line crossing with hhline and for instance make it hollow. Another option is to use single lines with different line thickness, in which case array package is required.

The caption helps to format the caption, such that keep it left aligned in narrow environment or add a hanging indent (see the second table). More options are explain in documentation

The \xstrut is to increase space between rules and the second row.

Table with double lines and hollow crossing

\documentclass{article}
\usepackage{threeparttable}
\usepackage{array}     % For !{\vrule ...}
\usepackage{caption}   % Caption formatting
\usepackage{hhline}    % Double line formatting

% Adds extra space in rows--required only in one cell 
\newcommand\xstrut[1][\baselineskip]{\rule[-0.3#1]{0pt}{#1}}


\begin{document}
\begin{table}[tbh]
    \captionsetup{
        position=bottom,
        skip=3pt,
    }
    \begin{threeparttable}
        \begin{tabular}{c|| >{$}c<{$}}
          Sistema       & \delta W_{rev}                   \\\hhline{=::=}
          \xstrut[12pt]
          Hidrostático  & -PdV                             \\
          Elástico 1D   & \tau dL                          \\
          Laminas       & \sigma dA                        \\
          Dieléctricos  & Ed\vec{P}                        \\
          Paramagnético & \underbrace{\mu_0H}_{B_0}\vec{M}
        \end{tabular}
        \caption{Trabajo reversible en diferentes sistemas}
    \end{threeparttable}
\end{table}
\end{document}

enter image description here

The table with single rules

\documentclass{article}
\usepackage{threeparttable}
\usepackage{array}     % For !{\vrule ...}
\usepackage{caption}   % Caption formatting

% Adds extra space in rows--required only in one cell 
\newcommand\xstrut[1][\baselineskip]{\rule[-0.3#1]{0pt}{#1}}


\begin{document}
\begin{table}[tbh]
    \captionsetup{
        format=hang,
        justification=raggedright,
        position=bottom,
        skip=6pt,
    }
    \begin{threeparttable}
        \begin{tabular}{c !{\vrule width 0.4pt} >{$}c<{$}}
          Sistema       & \delta W_{rev}                   \\
          \noalign{\hrule height 0.8pt}
          \xstrut[12pt]
          Hidrostático  & -PdV                             \\
          Elástico 1D   & \tau dL                          \\
          Laminas       & \sigma dA                        \\
          Dieléctricos  & Ed\vec{P}                        \\
          Paramagnético & \underbrace{\mu_0H}_{B_0}\vec{M}
        \end{tabular}
        \caption{Trabajo reversible en diferentes sistemas}
    \end{threeparttable}
\end{table}
\end{document}

enter image description here

2

With use of the talltblr of tabularray package ((an exercise how to put caption below talltblr table and caption formatting by use of the caption package). Table design is adopted to my taste ;-) :

enter image description here

\documentclass{article}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\usepackage{caption}
\captionsetup[table]{skip=0pt,
                     font = {small, sf},
                     labelfont = bf,
                     justification=centerlast
                    }
\NewTblrTheme{captionof}%
{\DefTblrTemplate{caption}{default}%
    {\addtocounter{table}{-1}%
     \captionof{table}{\InsertTblrText{caption}}%
    }
}

\begin{document}
\begin{table}[ht]
\DefTblrTemplate{firsthead,middlehead,lasthead}{default}{}
\DefTblrTemplate{firstfoot,middlefoot}{default}{}  % <---
\DefTblrTemplate{lastfoot}{default}%               % <---
{
  \UseTblrTemplate{caption}{default}
}
\centering
    \begin{talltblr}[%
  theme = captionof,
caption = {Trabajo reversible en diferentes sistemas},
  label = {tblr:test}
              ]{colspec = {@{} r Q[l, mode=math] @{}}}
%
Sistema       & \delta W_{\mathrm{rev}}  \\
    \midrule % <-- create a single, but well-spaced, horizontal rule
Hidrostático  & -P\,dV                           \\
Elástico 1D   & \tau\,dL                         \\
Laminas       & \sigma\,dA                       \\
Dieléctricos  & E\,d\vec{P}                      \\
Paramagnético & \underbrace{\mu_0H}_{B_0}\vec{M}
    \end{talltblr}
\end{table}
\end{document} 
2
  • 1
    +1 for the r-l column layout.
    – Mico
    Nov 28, 2023 at 15:37
  • @Mico, thank you very much!
    – Zarko
    Nov 28, 2023 at 20:47

You must log in to answer this question.

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