仅将标题文本置于表格中央

仅将标题文本置于表格中央

我需要你的帮助。我想让标题文本居中对齐,但表格的其余部分仅左对齐。我尝试了其他帖子中的一些建议:\multicolumn {number of columns}{type of align}{heading}但我不明白。我希望我的解决方案能达到相同的效果:

\documentclass[10pt,a4paper]{article}
\title{\textbf{DOC TITLE}}
\usepackage[spanish]{babel}
\usepackage[utf8]{inputenc}

\usepackage{pdflscape}
\usepackage{color, colortbl}
\definecolor{Gray}{gray}{0.9}
\usepackage{enumitem}
\usepackage{anysize}
\marginsize{3cm}{3cm}{2.5cm}{2.5cm}

\usepackage{xcolor}
\usepackage{caption}
\captionsetup[table]{skip=10pt}
\usepackage{threeparttable}
\usepackage{array}

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{\footnotesize{TEXT}}}
\lfoot{}
\cfoot{}
\rfoot{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\begin{document}

\begin{table} [h]
\caption{Title}
\centering
\begin{tabular}{|>{\centering\arraybackslash}m{23}|>{\centering\arraybackslash}m{60}|}
\hline\rowcolor{Gray}
\textbf{text} & \textbf{text}\\ [1ex]
\hline
\begin{flushleft} Text \end{flushleft}\\
\hline
\begin{flushleft} Text \end{flushleft}\\
\hline
text & text
\hline
text & text
\hline
\end{tabular}
\end{table}

\end{document}

我想要一个这样的表格,它有错误:桌子

请帮我!

答案1

定义表格的总体对齐方式并使用多列将标题文本居中更加容易:

\documentclass[10pt,a4paper]{article}
\usepackage{color, colortbl}
\definecolor{Gray}{gray}{0.9}

\begin{document}
\begin{table}
\caption{Title}
\centering
\begin{tabular}{|>{\raggedright\arraybackslash}m{23mm}|m{23mm}|m{60mm}|}
\hline
\multicolumn{1}{|>{\centering\arraybackslash}m{23mm}|}{\rowcolor{Gray}\textbf{Very long title}} 
    & \multicolumn{1}{>{\centering\arraybackslash}m{23mm}|}{\textbf{Very long title}} 
    & \multicolumn{1}{>{\centering\arraybackslash}m{60mm}|}{\textbf{Normal title}}\\
\hline
Text & Text 
    & Very very very very very very very very very very 
        very veryvery very very very very very very very long text \\
\hline
Very very very very very verryy very very very very very long text 
    & Very very very very very verryy very very very very very long text
    & Text \\
\hline
\end{tabular}
\end{table}
\end{document}

编辑:我已修改我的答案以反映您的示例。

  • 第 1 列按要求左对齐,使用\raggedright
  • 第 2 列对齐(普通段落),内容与第 1 列相同
  • 第 3 列用短标题对齐

桌子

答案2

\documentclass[10pt,a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{array,ragged2e}

\begin{document}

\begin{table} [!htb]
\caption{Title}
\centering
\begin{tabular}{| m{23pt} | m{60pt}|}\hline
\multicolumn{1}{c}{\cellcolor{black!30}\textbf{text}} & 
  \multicolumn{1}{c}{\cellcolor{black!30}\textbf{text}}\\\hline
Text & Text\\\hline
Text & T \\\hline
text & text\\\hline
text & t\\\hline
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

或者对于长标题:

\documentclass[10pt,a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{array,ragged2e}

\begin{document}

\begin{table} [!htb]
\caption{Title}
\centering
\begin{tabular}{| m{23pt} | m{60pt}|}\hline
\rowcolor{black!30}\Centering\bfseries foo
  & \Centering\bfseries Her comes some extremely long text for the first heading\\\hline
Text & Text\\\hline
Text & T \\\hline
text & text\\\hline
text & t\\\hline
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

答案3

这对我有用:

\newcommand*{\thead}[1]{\multicolumn{1}{|c|}{\bfseries #1}}

\begin{table}
    \centering
    \caption{caption} \label{tbl}
\begin{tabular}{|L{6cm}|L{8cm}|}
    \hline
    \thead{head 1} & \thead{head2} \\
    \hline
...
\end{tabular}
\end{table}

答案4

您还可以使用tabularray包中的单元格设置。我认为这更可靠一些。这里的关键是使用单元格命令,如下所示:cell{1}{1-number_of_columns} = {c}。这会将第一行和所有列设置为居中。您也可以将其他格式传递给此命令,下面的示例将标题文本加粗。

例子:

\documentclass{article}
\usepackage{tabularray}

\begin{document}
    \begin{tblr}{colspec = {
                X[l] X[c] X[r]}, 
                cell{1}{1-3} = {c, cmd=\textbf},}
        \hline
        Header1 & Header2 & Header3 \\
        \hline
        Data 1 & 1 & Note1 \\
        Data 2 & 2 & Note2 \\
        \hline
    \end{tblr}
\end{document}

带有居中标题的表格

查看文档这里了解更多信息(第 18 页)。

相关内容