# Easy vertical alignment in Tabulary balanced columns

I want to vertically center the text in all table cells. However, I do not wish to specify fixed column widths but rather leave it to tabulary to balance the column widths. How do I properly use column specifiers in this situation?

Easy solutions for regular use will be appreciated. Are there other LaTeX packages that do both column width balancing and better vertical alignment?

I do not know if the use of the IEEEtran class(v1.8b) is significant to my problem.

\documentclass[conference]{IEEEtran}
\usepackage{tabulary}
\usepackage{booktabs}
\begin{document}

\begin{table}[!h]
\renewcommand{\arraystretch}{1.3}
\caption{Comparison of Algorithm Results}
\centering

\begin{tabulary}{\columnwidth}{@{\hspace{.2em}}L*{4}{C}@{}}
\toprule
\textbf{Optimization Algorithm} & \textbf{Swarm Size} & \textbf{No. of Iterations} & \textbf{Final Wirelength} & \textbf{Best Value Cost Fn.}\\ \midrule
Short Text                      & 10    & 1000  & 100   & --    \\
Slightly Longer Text that Wraps & 20    & 2000  & 200   & 10.5  \\
Slightly Longer Text that Wraps & 30    & 3000  & 300   & 20.55 \\
Short                           & 40    & 4000  & 400   & 30.5  \\
\bottomrule
\end{tabulary}

\end{table}
\end{document}


You can "hack" the tabulary definition to vertically align all LRCJ columns.

\makeatletter
\def\TY@box@v#1{%
$\vcenter \@startpbox{\csname TY@F\the\TY@count\endcsname}% #1\arraybackslash\tyformat \insert@column\@endpbox$}
\makeatother


As the author of both tabularx and tabulary packages, I wouldn't use either of them for a data table such as this. they are about line breaking within cells and here, apart from the headings, you do not want line breaking.

I would use tabular* to force the overall width (if you must, or a centred tabular often works well) use the m column for the first column so that its reference point is vertically centred. For the numeric columns I use dcolumn for decimal alignment (siunitx is also good at this).

Finally I changed [h!] to [htp] (using h on its own increases the chance the table is forced to the end of the document and usually generates a warning from latex that it is being changed to ht)

\documentclass[conference]{IEEEtran}
\usepackage{dcolumn}
\usepackage{booktabs}

\newcommand\hd[1]{\multicolumn{1}{c}{%
\textbf{\begin{tabular}{@{}c@{}}#1\end{tabular}}}}

\begin{document}

\begin{table}[htp]
\renewcommand{\arraystretch}{1.3}
\caption{Comparison of Algorithm Results}
\centering

\begin{tabular*}{\columnwidth}{
@{\extracolsep{\fill}}
m{3cm}
D.{}{2.0}
D.{}{4.0}
D.{}{3.0}
D..{2.2}
@{}}
\toprule
\hd{Optimization Algorithm} & \hd{Swarm\\Size} &
\hd{No. of\\ Iterations} & \hd{Final\\ Wire-\\length} &
\hd{Best Value\\ Cost Fn.}\\ \midrule
Short Text                      & 10    & 1000  & 100   & \multicolumn{1}{c}{--}    \\
Slightly Longer Text that Wraps & 20    & 2000  & 200   & 10.5  \\
Slightly Longer Text that Wraps & 30    & 3000  & 300   & 20.55 \\
Short                           & 40    & 4000  & 400   & 30.5  \\
\bottomrule
\end{tabular*}

\end{table}

\end{document}


A solution with tabularx, defining new column L and C and changing the definition of \tabularxcolumn taken from this answer to obtain X column type vertically aligned

\documentclass[conference]{IEEEtran}
\usepackage{tabularx}
\usepackage{booktabs}

\renewcommand{\tabularxcolumn}[1]{m{#1}}

\newcolumntype{L}{>{\raggedright}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}[!h]
\renewcommand{\arraystretch}{1.3}
\caption{Comparison of Algorithm Results}
\centering

\begin{tabularx}{\columnwidth}{@{\hspace{.2em}}L*{4}{C}@{}}
\toprule
\textbf{Optimization Algorithm} & \textbf{Swarm Size} & \textbf{No. of Iterations} & \textbf{Final Wirelength} & \textbf{Best Value Cost Fn.}\\ \midrule
Short Text                      & 10    & 1000  & 100   & --    \\
Slightly Longer Text that Wraps & 20    & 2000  & 200   & 10.5  \\
Slightly Longer Text that Wraps & 30    & 3000  & 300   & 20.55 \\
Short                           & 40    & 4000  & 400   & 30.5  \\
\bottomrule
\end{tabularx}

\end{table}

\end{document}


Here's a solution that uses (a) a tabularx environment, (b) the S column type (from the siunitx package) for the data columns and (c) a centered version of the X column type just for the header cells of the data columns.

Adding the instruction \renewcommand{\tabularxcolumn}[1]{m{#1}} centers the column contents vertically. Using the S column type for the data columns makes it almost trivially easy to align the numbers on their explicit or implicit decimal markers.

\documentclass[conference]{IEEEtran}
\usepackage{booktabs,siunitx,tabularx}
\renewcommand{\tabularxcolumn}[1]{m{#1}} % for vertical centering
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcommand\mC[1]{\multicolumn{1}{C}{#1}} % handy shortcut macro
\begin{document}

\begin{table}[!h]
\renewcommand{\arraystretch}{1.3}
\caption{Comparison of Algorithm Results}
\begin{tabularx}{\columnwidth}{@{}L
S[table-format=2.0]
S[table-format=4.0]
S[table-format=3.0]
S[table-format=2.2]@{}}
\toprule
\textbf{Optimization Algorithm} &
\mC{\textbf{Swarm Size}} &
\mC{\textbf{No.\ of Iterations}} &
\mC{\textbf{Final Wirelength}} &
\mC{\textbf{Best Value Cost Fn.}}\\
\midrule
Short Text                      & 10 & 1000 & 100 & {--} \\
Slightly Longer Text that Wraps & 20 & 20   & 200 & 10.5 \\
Slightly Longer Text that Wraps & 30 & 300  & 3   & 20.55\\
Short Text                      & 40 & 4000 & 40  & 30.5 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

• The query was about vertical alignment so this is not a solution. But I appreciate your help in scientific table formatting. Dec 27 '16 at 15:38
• @geekshift - Sorry for overlooking this aspect. Achieving your objective is a piece of cake when using a tabularx environment: Just add the instructon \renewcommand{\tabularxcolumn}[1]{m{#1}}. I'll edit my answer to reflect this piece of information.
– Mico
Dec 27 '16 at 15:44
• Please have a look at this question If possible, add decimal alignment to it as well. tabularx allows relative width specifications using X and siunitx allows decimal alignment using S. How to do both ? Dec 27 '16 at 20:19

Well, it is not automated, and thus may be found unacceptable. But, starting with the OP's MWE, once the auto-breakpoints are seen, one merely need wrap that text in \mycol with the breakpoints manually inserted.

\documentclass[conference]{IEEEtran}
\usepackage{tabulary}
\usepackage{booktabs}
\usepackage[usestackEOL]{stackengine}
\begin{document}

\begin{table}[!h]
\renewcommand{\arraystretch}{1.3}
\caption{Comparison of Algorithm Results}
\centering

\begin{tabulary}{\columnwidth}{@{\hspace{.2em}}L*{4}{C}@{}}
\toprule
\textbf{Optimization Algorithm} & \textbf{Swarm Size} & \textbf{No. of Iterations} & \textbf{Final Wirelength} & \textbf{Best Value Cost Fn.}\\ \midrule
Short Text                      & 10    & 1000  & 100   & --    \\
\mycol{Slightly Longer\\ Text that Wraps} & 20    & 2000  & 200   & 10.5  \\
\mycol{Slightly Longer\\ Text that Wraps} & 30    & 3000  & 300   & 20.55 \\
Short                           & 40    & 4000  & 400   & 30.5  \\
\bottomrule
\end{tabulary}

\end{table}
\end{document}