
У меня есть следующая таблица деталей:
\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
. Для аккуратного пересечения вертикальных двойных линий и горизонтальных линий лучше использовать \hhline
s:
\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}