我有这个表格,我想让标题与表格对齐,而不是位于页面的中心。代码如下:
\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}
输出如下:
如何让标题与表格居中?我希望表格处于该位置,但标题与表格对齐,我不想移动表格。
谢谢阅读。
答案1
下面首先将您的排版tabular
到一个框中,然后调整可能需要的宽度\caption
(通过在里面使用它minipage
)使其与一样宽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}
答案2
我建议你加载三部分表tabular
将环境和命令打包并封闭\caption
在一个threeparttable
环境中。这样,标题的宽度将被限制为相关环境的宽度tabular
。
另外,我还将通过以下方式简化表格的外观:(a)删除双击垂直线;(b)用单个但间距适当的水平线替换双击水平线。
\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}
答案3
我建议使用{NiceTabular}
带有标题nicematrix
键的。标题会包裹在表格的宽度内。caption
\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}
答案4
使用talltblr
包tabularray
((练习如何将标题放在talltblr
表格下方并使用caption
包进行标题格式化)。表格设计符合我的口味;-) :
\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}