表格中標題的居中對齊

表格中標題的居中對齊

我有以下零件表:

\begin{table}[!h]
\centering
\begin{adjustbox}{width=1\textwidth}
\small
\begin{tabular}{|l||l|} 
\hline
\textbf{Element Name} & \textbf{Description} \\
\hline
SemiMajorAxis & The length of the semi-major axis $a$, $b$ and $c$ of the 3D \\\hline
\end{tabular} 
\end{adjustbox}
\caption[A description of the XML elements containing the ellipsoid information]{A description of the XML elements containing the information of the ellipsoid.}
\label{tab:XML_defs}
\end{table}

我想在單元格的中心對齊名稱“元素名稱”和“描述”,而不影響其餘行。

答案1

你需要更換

\textbf{Element Name} & 
\textbf{Description}

\multicolumn{1}{|c||}{\textbf{Element Name}} & 
\multicolumn{1}{c|}{\textbf{Description}}

不要使用\adjustbox將表格填充到文字區塊的寬度,而是考慮使用tabularx環境而不是環境並使用第二列tabular的列類型。列X中的文字X可以根據需要換行。您可能還需要考慮通過去掉所有垂直線並更謹慎地使用水平線,使表格看起來更“開放”。在下面的螢幕截圖中,第二個表是在套件的畫線宏的幫助下繪製的booktabs

在此輸入影像描述

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

\begin{document}
\begin{table}[!h]

\begin{tabularx}{\textwidth}{|l||X|} 
\hline
\multicolumn{1}{|c||}{\textbf{Element Name}} & 
\multicolumn{1}{c|}{\textbf{Description}} \\
\hline
SemiMajorAxis & The length of the semi-major axis $a$, $b$ and $c$ of the 3D \\
\hline
\end{tabularx} 
\caption[A description of the XML elements containing the ellipsoid information]{A description of the XML elements containing the information of the ellipsoid.}
\label{tab:XML_defs}

\bigskip\bigskip
\begin{tabularx}{\textwidth}{lX} 
\toprule
\textbf{Element Name} & \textbf{Description} \\
\addlinespace
SemiMajorAxis & The length of the semi-major axis $a$, $b$ and $c$ of the 3D \\
\bottomrule
\end{tabularx} 
\caption{Another, more ``open'' form of the same table}
\end{table}
\end{document}

答案2

您可以使用該makecell套件:它的\thead命令(以及其他一些命令)允許對其內容和換行符號進行通用格式設定。預設情況下,它是垂直和水平居中的。您可以使用\setcellgapes和命令為單元格提供一些垂直填充\makegapedcells。對於整齊相交的垂直雙線和水平線,最好使用\hhlines:

\documentclass{article}
\usepackage{adjustbox, array, hhline}
\usepackage{makecell}
\renewcommand\theadfont{\normalfont\bfseries}
\setcellgapes{4pt}
\usepackage[showframe]{geometry}

\begin{document}

\vspace*{1cm}
\begin{table}[!h]
  \centering\makegapedcells
  \begin{tabular}{|l||l|}
    \hline
    \thead{Element Name} & \thead{Description} \\
    \hhline{|-||-|}
    SemiMajorAxis & The length of the semi-major axis $a$, $b$ and $c$ of the 3D \\\hline
  \end{tabular}%
  \caption[A description of the XML elements containing the ellipsoid information]{A description of the XML elements containing the information of the ellipsoid.}
  \label{tab:XML_defs}
\end{table}

\end{document}

在此輸入影像描述

相關內容