# Troubles with column width in the tables headings

Well, we all know that compose tables in LaTeX is not easy, however, despite the large number of existing packages and options to facilitate this, I still have some problems I could not solve, and I want to fix.

The first one has to do with the column width. Especially when the cell has a lot of text or more frequently when the header has a longer title than the content width of that same column. This causes the contents of the tables present an awful justification, like it was made ​​in M\$ Word.

My second problem is the vertical alignment, because I do not always seem right that the text aligns with the top edge of the cell.

An example of that would be the following table:

\begin{table}[!htb]
\caption{Mezclas de Óxidos de Ir y Ru.} %Tabla 6.2.
\label{taula:oximixs2}
\begin{center}
\begin{tabular}{cp{30pt}p{30pt}p{55pt}p{45pt}p{43pt}} \hline
\textbf{Óxido}  & \textbf{Sal Precursora} & \textbf{Peso molecular (\si[detect-weight= true]{\g\per\mole})} & \textbf{Volumen de \BPChem{H\_2O} deionizada (\si[detect-weight= true]{\litre})} & \textbf{M (\si[detect-weight= true]{\mole\per\litre})} & \textbf{Cantidad de sal (\si[detect-weight= true]{\g})} \\ \hline
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 80:20} & \BPChem{H\_2IrCl\_6} & 515 & 0.02 & 0.01 & 0.1030 \\
& \BPChem{RuCl\_3} & 261.42 & 0.005 & 0.01 & 0.0131 \\
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 50:50} & \BPChem{H\_2IrCl\_6} & 515 & 0.0125 & 0.01 & 0.0644 \\
& \BPChem{RuCl\_3} & 261.42 & 0.0125 & 0.01 & 0.0327 \\
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 20:80} & \BPChem{H\_2IrCl\_6} & 515    & 0.005  & 0.01 & 0.0258 \\
& \BPChem{RuCl\_3} & 261.42 & 0.02 & 0.01 & 0.0523 \\ \hline
\end{tabular}
\end{center}
\end{table}


Well, maybe without a preamble to build a minimal working example that does not tell you anything, so taking the minimal packages from the original document, this would be like:

\documentclass[notitlepage,openbib]{book}
\usepackage[utf8]{inputenc}
\usepackage[TS1,T1]{fontenc}
\usepackage[spanish,activeacute,mexico]{babel}
\usepackage[osf]{mathpazo}
\usepackage{amsmath,amsthm,amstext,amssymb}
\usepackage{multicol}
\usepackage[cbgreek]{bpchem}
\usepackage[per-mode=symbol]{siunitx}
\sisetup{
output-decimal-marker = {,},
range-units = brackets,
list-final-separator = { \translate{and} },
list-pair-separator = { \translate{and} },
range-phrase = { \translate{to (numerical range)} },
}

\begin{document}
\begin{table}[!htb]
\caption{Mezclas de Óxidos de Ir y Ru.} %Tabla 6.2.
\label{taula:oximixs2}
\begin{center}
\begin{tabular}{cp{30pt}p{30pt}p{55pt}p{45pt}p{43pt}} \hline
\textbf{Óxido}  & \textbf{Sal Precursora} & \textbf{Peso molecular (\si[detect-weight= true]{\g\per\mole})} & \textbf{Volumen de \BPChem{H\_2O} deionizada (\si[detect-weight= true]{\litre})} & \textbf{M (\si[detect-weight= true]{\mole\per\litre})} & \textbf{Cantidad de sal (\si[detect-weight= true]{\g})} \\ \hline
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 80:20} & \BPChem{H\_2IrCl\_6} & 515 & 0.02 & 0.01 & 0.1030 \\
& \BPChem{RuCl\_3} & 261.42 & 0.005 & 0.01 & 0.0131 \\
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 50:50} & \BPChem{H\_2IrCl\_6} & 515 & 0.0125 & 0.01 & 0.0644 \\
& \BPChem{RuCl\_3} & 261.42 & 0.0125 & 0.01 & 0.0327 \\
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 20:80} & \BPChem{H\_2IrCl\_6} & 515    & 0.005  & 0.01 & 0.0258 \\
& \BPChem{RuCl\_3} & 261.42 & 0.02 & 0.01 & 0.0523 \\ \hline
\end{tabular}
\end{center}
\end{table}
\end{document}


The way I work is trial and error it is not the most efficient way, so suggestions are accepted.

-
! Undefined control sequence. <recently read> \multirow –  David Carlisle Dec 13 '13 at 9:18
Ups, I forgot to include that package in the MWE. –  Aradnix Dec 14 '13 at 20:07

I suggest you make the following changes:

• Rather than calculate the widths of individual columns to make the entire tabular environment take up (more or less) the width of the text block, I'd suggest using the tabularx environment and, at least at first, assigning equal widths to the five data columns. Fine-tuning the widths of the columns should come toward the end.

• Set the contents "ragged-right" rather than fully justified, while still allowing hyphenation. One way to achieve this is through the \RaggedRight directive in the specification of the column properties. (I should note that I'm unfamiliar with Spanish hyphenation rules.)

• Use the line-drawing commands of the booktabs package, and also increase the separation between the three main groups of rows.

• You may want to ask yourself if it's necessary to typeset the table's header in bold. Just a thought.

\documentclass[notitlepage,openbib]{book}
\usepackage[utf8]{inputenc}
\usepackage[TS1,T1]{fontenc}
\usepackage[spanish,activeacute,mexico]{babel}
\usepackage[osf]{mathpazo}
\usepackage{amsmath,amsthm,amstext,amssymb}

\usepackage{multirow,tabularx,ragged2e,booktabs}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}

\usepackage[cbgreek]{bpchem}
\usepackage[per-mode=symbol]{siunitx}
\sisetup{ detect-weight = true,
output-decimal-marker = {,},
range-units = brackets,
list-final-separator = { \translate{and} },
list-pair-separator = { \translate{and} },
range-phrase = { \translate{to (numerical range)} } }
\begin{document}
\begin{table}
\caption{Mezclas de Óxidos de Ir y Ru.} %Tabla 6.2.
\label{taula:oximixs2}
\begin{tabularx}{\textwidth}{@{} l *{5}{Y} @{}}
\toprule
\textbf{Óxido}  & \textbf{Sal Precursora} & \textbf{Peso molecular (\si{\g\per\mole})} & \textbf{Volumen de \BPChem{H\_2O} deionizada (\si{\litre})} & \textbf{M (\si{\mole\per\litre})} & \textbf{Cantidad de sal (\si{\g})} \\
\midrule
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 80:20} & \BPChem{H\_2IrCl\_6} & 515 & 0.02 & 0.01 & 0.1030 \\
& \BPChem{RuCl\_3} & 261.42 & 0.005 & 0.01 & 0.0131 \\ \addlinespace
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 50:50} & \BPChem{H\_2IrCl\_6} & 515 & 0.0125 & 0.01 & 0.0644 \\
& \BPChem{RuCl\_3} & 261.42 & 0.0125 & 0.01 & 0.0327 \\ \addlinespace
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 20:80} & \BPChem{H\_2IrCl\_6} & 515    & 0.005  & 0.01 & 0.0258 \\
& \BPChem{RuCl\_3} & 261.42 & 0.02 & 0.01 & 0.0523 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}


Addendum: An important improvement would be to align the numbers in the data columns on their decimal markers. This may be done by using S column types overall, while using the previously defined Y column types for the headers. Doing so will also assure that the data columns all have the same width.

\begin{table}
\caption{Mezclas de Óxidos de Ir y Ru.} %Tabla 6.2.
\label{taula:oximixs2}
\begin{tabularx}{\textwidth}{@{} l Y S[table-format=3.2] S[table-format=1.4]
S[table-format=1.2] S[table-format=1.4]@{}}
\toprule
\textbf{Óxido}  & \textbf{Sal Precursora} &
\multicolumn{1}{Y}{\textbf{Peso molecular (\si{\g\per\mole})}} &
\multicolumn{1}{Y}{\textbf{Volumen de \BPChem{H\_2O} deionizada (\si{\litre})}} &
\multicolumn{1}{Y}{\textbf{M (\si{\mole\per\litre})}} &
\midrule
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 80:20} & \BPChem{H\_2IrCl\_6} & 515 & 0.02 & 0.01 & 0.1030 \\
& \BPChem{RuCl\_3} & 261.42 & 0.005 & 0.01 & 0.0131 \\ \addlinespace
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 50:50} & \BPChem{H\_2IrCl\_6} & 515 & 0.0125 & 0.01 & 0.0644 \\
& \BPChem{RuCl\_3} & 261.42 & 0.0125 & 0.01 & 0.0327 \\ \addlinespace
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 20:80} & \BPChem{H\_2IrCl\_6} & 515    & 0.005  & 0.01 & 0.0258 \\
& \BPChem{RuCl\_3} & 261.42 & 0.02 & 0.01 & 0.0523 \\
\bottomrule
\end{tabularx}
\end{table}


The S[table-format=3.2] directive tells LaTeX to align numerical input on the decimal marker, while creating enough space for three digits before and two digits after the decimal marker. I chose 3 and 2 because it matched the inputs you provided. Similarly, S[table-format=1.4] is optimized for numerical input with one digit before and four digits after the decimal marker. Because the S column type is meant for numerical inputs, care has to be taken with the formatting if some cells contain non-numerical inputs; in this table, the non-numerical cells (in the header row) are formatted via the Y column type. Separately, the @{} snippet instructs LaTeX not to insert whitespace before the first column and after the final column of the table. (Compare the look of "my" tables with that of, say, Andrew Swann's answer and decide which look you prefer.)

-
Well after reading the three answers, very interesting all I found your the more comformtable for me. In fact I compiled and I had not issues with the spanish hyphenatation. The only concern that I have is still from the vertical alignment of text within table cells, for even seen, especially in headers that the text is aligned relative to the top rule. Is there no way to control the vertical position of text in tables in LaTeX? –  Aradnix Dec 14 '13 at 23:10
What means the {@{} l Y S[table-format=3.2] S[table-format=1.4] S[table-format=1.2] S[table-format=1.4]@{}} after the tabularx definition? Why those values in the option table-format? Why can't I use directly the S columns of the siunitx package? –  Aradnix Dec 15 '13 at 4:36
Thanks I read them and now I understand better the solution. –  Aradnix Dec 16 '13 at 4:32

\documentclass[notitlepage,openbib]{book}
\usepackage[utf8]{inputenc}
\usepackage[TS1,T1]{fontenc}
\usepackage[spanish,activeacute,mexico]{babel}
\usepackage[osf]{mathpazo}
\usepackage{amsmath,amsthm,amstext,amssymb}
\usepackage{multirow,array,booktabs}
\usepackage[cbgreek]{bpchem}
\usepackage[per-mode=symbol]{siunitx}
\sisetup{
output-decimal-marker = {,},
range-units = brackets,
list-final-separator = { \translate{and} },
list-pair-separator = { \translate{and} },
range-phrase = { \translate{to (numerical range)} },}

\newcolumntype{R}[1]{>{\raggedright\arraybackslash}p{#1pt}}
\begin{document}
\begin{table}[!htb]
\caption{Mezclas de Óxidos de Ir y Ru.} %Tabla 6.2.
\label{taula:oximixs2}
\begin{center}
\begin{tabular}{cR{30}R{30}R{55}R{45}R{43}} \toprule
\textbf{Óxido}  & \textbf{Sal Precursora} & \textbf{Peso molecular (\si[detect-weight= true]{\g\per\mole})} & \textbf{Volumen de \BPChem{H\_2O} deionizada (\si[detect-weight= true]{\litre})} & \textbf{M (\si[detect-weight= true]{\mole\per\litre})} & \textbf{Cantidad de sal (\si[detect-weight= true]{\g})} \\ \midrule
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 80:20} & \BPChem{H\_2IrCl\_6} & 515 & 0.02 & 0.01 & 0.1030 \\
& \BPChem{RuCl\_3} & 261.42 & 0.005 & 0.01 & 0.0131 \\
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 50:50} & \BPChem{H\_2IrCl\_6} & 515 & 0.0125 & 0.01 & 0.0644 \\
& \BPChem{RuCl\_3} & 261.42 & 0.0125 & 0.01 & 0.0327 \\
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 20:80} & \BPChem{H\_2IrCl\_6} & 515    & 0.005  & 0.01 & 0.0258 \\
& \BPChem{RuCl\_3} & 261.42 & 0.02 & 0.01 & 0.0523 \\ \bottomrule
\end{tabular}
\end{center}
\end{table}
\end{document}

-

The main point is don't expect the formatting for column headings to be the same as for column entries. You can specify different formatting for the headings by using a \multicolumn command. Probably best wrapped up in a personal macro. Be prepared to break these headings manually. But format so that LaTeX does as much of the spacing as possible.

In the table, your decimals are best set with siunitx special columns of type S; then you get the correct decimal marker you specify and numbers are aligned on that.

\documentclass[notitlepage,openbib]{book}
\usepackage[utf8]{inputenc}
\usepackage[TS1,T1]{fontenc}
\usepackage[spanish,activeacute,mexico]{babel}
\usepackage[osf]{mathpazo}
\usepackage{amsmath,amsthm,amstext,amssymb}
\usepackage{multirow}
\usepackage[cbgreek]{bpchem}
\usepackage[per-mode=symbol]{siunitx}
\sisetup{
output-decimal-marker = {,},
range-units = brackets,
list-final-separator = { \translate{and} },
list-pair-separator = { \translate{and} },
range-phrase = { \translate{to (numerical range)} },
}

\begin{document}
\begin{table}[!htb]
\caption{Mezclas de Óxidos de Ir y Ru.} %Tabla 6.2.
\label{taula:oximixs2}
\begin{center}
\newcommand{\myhd}[1]{\multicolumn{1}{c}{\textbf{#1}}}
\begin{tabular}{clSSSS} \hline \myhd{Óxido} & \myhd{Sal} &
\myhd{Peso molecular}
& \myhd{Volumen de} & \myhd{M} & \myhd{Cantidad de} \\
& \myhd{Precursora} & \myhd{(\si[detect-weight=
(\si[detect-weight= true]{\litre})}& \myhd{ (\si[detect-weight=
true]{\mole\per\litre})} & \myhd{ sal (\si[detect-weight=
true]{\g})}\\
\hline
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 80:20} &
\BPChem{H\_2IrCl\_6} & 515 & 0.02 & 0.01 & 0.1030 \\
& \BPChem{RuCl\_3} & 261.42 & 0.005 & 0.01 & 0.0131 \\
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 50:50} & \BPChem{H\_2IrCl\_6}
& 515 & 0.0125 & 0.01 & 0.0644 \\
& \BPChem{RuCl\_3} & 261.42 & 0.0125 & 0.01 & 0.0327 \\
\multirow{2}{*}{\BPChem{Ir:Ru-O\_x} 20:80} & \BPChem{H\_2IrCl\_6}
& 515    & 0.005  & 0.01 & 0.0258 \\
& \BPChem{RuCl\_3} & 261.42 & 0.02 & 0.01 & 0.0523 \\ \hline
\end{tabular}
\end{center}
\end{table}

\end{document}

-
The numbers in the second data column that don't contain an explicit decimal marker don't seem to be lined up correctly. –  Mico Dec 13 '13 at 9:51
@Mico Indeed. Either one could set this as an l column, or probably more relevant make the data consistently of the same type with decimals. –  Andrew Swann Dec 13 '13 at 9:56
It could also be done, I think, by providing an explicit table-format option to the column in question. –  Mico Dec 13 '13 at 10:04