5

I have the following part table:

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

I would like to align the name "Element Name" and "Description" at the center of the cell without affecting the rest of the rows.

  • 3
    You can put each of the header cell contents into a \multicolumn environment such as \multicolumn{1}{c|}{\textbf{Description}}. Be aware, however, that it affects the vertical rules. Hence the {c|} argument. – Ondrian Feb 18 '16 at 20:43
4

You need to replace

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

with

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

Instead of using \adjustbox to cram the table into width of the text block, do consider using a tabularx environment instead of the tabular environment and using a column of type X for the second column. Text in an X column will can wrap, as needed. You may also want to consider giving the table a more "open" look, by getting rid of all vertical lines and using horizontal lines more sparingly. In the following screenshot, the second table is drawn with the help of the line-drawing macros of the booktabs package.

enter image description here

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

You can use the makecell package: its \thead command (and some other commands) allow for a common formatting of its contents, and line breaks. By default it is both vertically and horizontally centred. You can give cells some vertical padding, with \setcellgapes and \makegapedcells commands. For neatly intersecting vertical double rules and horizontal rules, it is better to use \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}

enter image description here

  • thead doesn't make hyphenation. – buhtz Aug 28 '16 at 20:22
  • Of course: you have to make line breaks by hand. Its main usage it to allow line breaks is the standard r,c,l type columns, and to format column heads. For hyphenation, cells must be in paragraph mode. – Bernard Aug 28 '16 at 20:27
  • I am not sure if I understood you right. \newline is allowed in \thead? Here it has no effect. – buhtz Aug 28 '16 at 20:32
  • In thead and \makecell, you use a plain \\ . If you want to change the interline skip, it's via \renewcommand{\arraystretch}{some value}. – Bernard Aug 28 '16 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.