выравнивание заголовков в таблице по центру

выравнивание заголовков в таблице по центру

У меня есть следующая таблица деталей:

\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}

введите описание изображения здесь

Связанный контент