如何使列标题居中?

如何使列标题居中?

我正在尝试用乳胶制作一个表格,我需要将标题居中对齐,将文本居左对齐

\begin{table}[H]
\caption{The Goal Question Metric model}
\label{T:gqm}
\begin{tabular}{ p{2cm} p{11cm}  }
\toprule
 Metric & Statement\\
 \midrule
Purpose & Systematic literature review \\
Object & Peer review publications in computer science and software engineering \\
Issue & Task ranking approaches in software development \\
Viewpoint & Software engineers and industry practitioners \\
 \bottomrule
\end{tabular}
\end{table}

答案1

正如@imran 在他的评论中所建议的:

\documentclass{article}
\usepackage{booktabs}
\NewExpandableDocumentCommand\mcc{m}{\multicolumn{1}{c}{#1}}

\begin{document}
\begin{tabular}{ p{2cm} p{11cm}  }
    \toprule
\mcc{Metric}& \mcc{Statement}  \\
    \midrule
Purpose     & Systematic literature review \\
Object      & Peer review publications in computer science and software engineering \\
Issue       & Task ranking approaches in software development \\
Viewpoint   & Software engineers and industry practitioners \\
    \bottomrule
\end{tabular}
    \end{table}
\end{document}

在此处输入图片描述

或者使用以下tabularray包:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
    \begin{table}[ht]
\caption{The Goal Question Metric model}
\label{T:gqm}
\begin{tblr}{colspec={@{} l X[l] @{}},
             row{1} = {c}
             }
    \toprule
Metric      & Statement     \\
    \midrule
Purpose     & Systematic literature review \\
Object      & Peer review publications in computer science and software engineering \\
Issue       & Task ranking approaches in software development \\
Viewpoint   & Software engineers and industry practitioners \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

同时还确保表格不会突出右文本边框:

在此处输入图片描述

笔记: 请始终提供完整的小文档来重现您的问题。例如,从您的代码片段中,我们无法提取有关使用\documentclass和文档布局的信息。两者都会影响表格外观。例如,在第一个 MWE(最小工作示例)中,您的表格溢出了右侧文本边框。

答案2

另一种可能性是使用\thead以下命令makecell

\documentclass{article}
\usepackage{makecell}
\usepackage{booktabs}

\begin{document}

\begin{table}
\renewcommand{\theadfont}{\normalsize\bfseries}
\caption{The Goal Question Metric model}
\label{T:gqm}
\begin{tabular}{ p{2cm} p{11cm} }
\toprule
 \thead{Metric} & \thead{Statement}\\
 \midrule
Purpose & Systematic literature review \\
Object & Peer review publications in computer science and software engineering \\
Issue & Task ranking approaches in software development \\
Viewpoint & Software engineers and industry practitioners \\
 \bottomrule
\end{tabular}
\end{table}

\end{document} 

在此处输入图片描述

答案3

与。{NiceTabular}nicematrix

\documentclass{article}
\usepackage{nicematrix,booktabs}

\begin{document}
\begin{table}[ht]
\caption{The Goal Question Metric model}
\label{T:gqm}
\begin{NiceTabular}{@{} l X[l] @{}}
    \toprule
\Block[c]{}{Metric}      & \Block[c]{}{Statement}     \\
    \midrule
Purpose     & Systematic literature review \\
Object      & Peer review publications in computer science and software engineering \\
Issue       & Task ranking approaches in software development \\
Viewpoint   & Software engineers and industry practitioners \\
    \bottomrule
\end{NiceTabular}
\end{table}
\end{document}

上述代码的输出

相关内容