![如何在表格中将列名称居中](https://linux22.com/image/444230/%E5%A6%82%E4%BD%95%E5%9C%A8%E8%A1%A8%E6%A0%BC%E4%B8%AD%E5%B0%86%E5%88%97%E5%90%8D%E7%A7%B0%E5%B1%85%E4%B8%AD.png)
我想将列名居中,因为它们当前是左对齐的。
我使用的代码是:
\begin{tabular}{@{\extracolsep{0pt}} lcccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& N & \vtop{\hbox{\strut Green}\hbox{\strut bond issuers (1)}} & \vtop{\hbox{\strut Non-green}\hbox{\strut bond issuers (2)}} & \vtop{\hbox{\strut p-value}\hbox{\strut (diff. in means) (3)}}\\
\hline \\[-1.8ex]
Log(assets) & 001 & 1.621 & 6.666 & 0.000\textasteriskcentered \textasteriskcentered \textasteriskcentered \\
& & (0.999) & (0.025) & \\
Return on assets & 1321 & 0.099 & 0.000 & 0.000\textasteriskcentered \textasteriskcentered \textasteriskcentered \\
& & (0.888) & (0.101) & \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
答案1
你想要这样的东西吗?
它是使用makecell
和booktabs
包生成的。包thead
中的宏makecell
允许在表头中插入手动换行符,并且还可以水平和垂直居中内容(但是也可以使用可选参数进行不同的对齐)。
\documentclass{article}
\usepackage{makecell}
\usepackage{booktabs}
\begin{document}
\begin{table}
\centering
\begin{tabular}{@{}l*{4}{c}}
\toprule
& \thead{N} & \thead{Green\\bond issues\\(1)} & \thead{Non-green\\bond issues\\(2)} & \thead{p-value\\(diff. in means)\\(3)}
\\\midrule
Log(assets) & 001 & 1.621 & 6.666 & 0.000\textasteriskcentered \textasteriskcentered \textasteriskcentered \\
& & (0.999) & (0.025) & \\
Return on assets & 1321 & 0.099 & 0.000 & 0.000\textasteriskcentered \textasteriskcentered \textasteriskcentered \\
& & (0.888) & (0.101) &
\\\bottomrule
\end{tabular}
\caption{Caption}
\label{tab:my_label}
\end{table}
\end{document}
答案2
这是一个稍微复杂一些的解决方案,但它可以用于类似类型的更复杂的表格:
\documentclass[border=3.131592]{standalone}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx, varwidth}
\sisetup{
input-open-uncertainty =,
input-close-uncertainty=,
}
\ExplSyntaxOn
\NewChildSelector{eachtwo}
{
\int_step_inline:nnnn {2}{2}{\l_tblr_childs_total_tl}
{ \clist_put_right:Nn \l_tblr_childs_clist {##1} }
}
\ExplSyntaxOff
\begin{document}
\begin{talltblr}[
caption = {caption title},
label = {tab:???},
note{} = {***: some explanation},
]{colspec = {l c
*{2}{S[table-format={(}1.3{)}]}
S[table-format=1.3{***}]},
colsep = {4pt},
row{2-Z} = {rowsep=0pt},
row{eachtwo} = {abovesep=3pt},
measure = vbox
}
\toprule
& N & {{{Green\\ bond issuers\\ (1)}}}
& {{{Non-green\\ bond issuers\\ (2)}}}
& {{{$p$-value\\ {(diff. in means)}\\ (3)}}}
\\
\midrule
Log(assets)
& 001 & 1.621 & 6.666 & 0.000\TblrNote{***} \\
& & (0.999) & (0.025) & \\
Return on assets
& 1321 & 0.099 & 0.000 & 0.000\TblrNote{***} \\
& & (0.888) & (0.101) & \\
\bottomrule
\end{talltblr}
\end{document}
编辑:tabularray
一个改进表格注释的解决方案(正如我在询问时向 软件包作者建议的那样)github)。现在表格的注释可以按照 enimitem
包定义的列表进行组织:
\documentclass[border=3.131592]{standalone}
\usepackage{enumitem} % <---
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx, varwidth}
\NewTblrTheme{itemize}% you can select different name of theme
{
\DefTblrTemplate{note}{default}{%
\setlength{\linewidth}{\tablewidth}%
\begin{enumerate}[nosep]
\MapTblrNotes {
\item[\UseTblrTemplate{note-tag}{default}{:}]
\UseTblrTemplate{note-target}{default}
\UseTblrTemplate{note-text}{default}
}
\end{enumerate} }% end of template
} % end of theme
\sisetup{
input-open-uncertainty =,
input-close-uncertainty=,
}
\ExplSyntaxOn
\NewChildSelector{eachtwo}
{
\int_step_inline:nnnn {2}{2}{\l_tblr_childs_total_tl}
{ \clist_put_right:Nn \l_tblr_childs_clist {##1} }
}
\ExplSyntaxOff
\begin{document}
\begin{talltblr}[
theme = itemize,
caption = {Caption title},
label = {tab:talltblr},
note{**} = {first explanation},
note{***} = {second explanation},
]{colspec = {l c
*{2}{S[table-format={(}1.3{)}]}
S[table-format=1.3{***}]},
colsep = {4pt},
row{2-Z} = {rowsep=0pt},
row{eachtwo} = {abovesep=3pt},
measure = vbox
}
\toprule
& N & {{{Green\\ bond issuers\\ (1)}}}
& {{{Non-green\\ bond issuers\\ (2)}}}
& {{{$p$-value\\ {(diff. in means)}\\ (3)}}}
\\
\midrule
Log (assets)
& 001 & 1.621 & 6.666 & 0.000\TblrNote{**} \\
& & (0.999) & (0.025) & \\
Return on assets
& 1321 & 0.099 & 0.000 & 0.000\TblrNote{***} \\
& & (0.888) & (0.101) & \\
\bottomrule
\end{talltblr}
\end{document}
答案3
有很多方法可以实现这种效果。 \multicolumn{1}{p{...}}{...}
将一c
列替换为一p
列,但您必须指定宽度。您可以将一个表格放在一个表格中。但最简单、最不令人困惑的解决方案是简单地将文本分成三行(两行对于页面来说太宽了)。
\documentclass{article}
\usepackage{showframe}
\begin{document}
\begin{table}
\centering
\begin{tabular}{lcccc}% \extracolsep is only used with tabular*
\\[-1.8ex]% adds \arrarystretch\baselineskip - 1.8ex
\hline\hline
\\[-1.8ex]
& N & Green & Non-green & p-value \\
& & bond issuers & bond issuers & (diff. in means) \\
& & (1) & (2) & (3) \\
\hline
\\[-1.8ex]
Log(assets) & 001 & 1.621 & 6.666 & 0.000\textasteriskcentered \textasteriskcentered \textasteriskcentered \\
& & (0.999) & (0.025) & \\
Return on assets & 1321 & 0.099 & 0.000 & 0.000\textasteriskcentered \textasteriskcentered \textasteriskcentered \\
& & (0.888) & (0.101) & \\
\hline
\\[-1.8ex]
\end{tabular}
\end{table}
\end{document}