2

As title says, I'm using "tabu" and I have 2 questions about cells width and height.

1) Width : The first example below doesn't require any width specification. The second one exceeds the page width even with the width specification. The third one fits the width specification. Can you tell me what's wrong with the second one, since it's built on the same principle as the third one ?

2) Height : a classical question about tables as you can see the line touching the exponents in the second and the third examples. However, I don't want to set arbitrary lengths such as rules of a few pt. I'd rather like to calculate the height of the text and add the precise amount of extra height (something like the method given by egreg in hline too low in tabu environment). How can I do this for each row in each table ?

Here's the code :

\documentclass[12pt,a4paper]{book}
\everymath{\displaystyle}
\usepackage[T1]{fontenc}
\usepackage{framed}
\usepackage[utf8]{inputenc}
\usepackage[fleqn]{mathtools}
\mathtoolsset{showonlyrefs}
\usepackage{tabu}

\begin{document}

\begin{minipage}{\textwidth}
\begin{framed}
Example 1 :
\begin{center}
\everyrow{\tabucline-}
\begin{tabu}{|*7{X[-1,c,m]|}}
$km$ & $hm$ & $dam$ & $m$ & $dm$ & $cm$ & $mm$\\
& & $1.$ & $2$ & $3$ & &\\
& & $1$ & $2$ & $3$ & &\\
& $0.$ & $1$ & $2$ & $3$ & &\\
\end{tabu}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam=123\ dm\\
&1.23\ dam=0.123\ hm
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\begin{minipage}{\textwidth}
\begin{framed}
Example 2 :
\begin{center}
\everyrow{\tabucline-}
\begin{tabu}to\dimexpr\textwidth-2\fboxrule-2\fboxsep{|*{14}{X[-1,c,m]|}}
\multicolumn{2}{|c|}{$km^2$} & \multicolumn{2}{c|}{$hm^2$} & \multicolumn{2}{c|}{$dam^2$} & \multicolumn{2}{c|}{$m^2$} & \multicolumn{2}{c|}{$dm^2$} & \multicolumn{2}{c|}{$cm^2$} & \multicolumn{2}{c|}{$mm^2$}\\
& & & & & $1.$ & $2$ & $3$ & & & & & &\\
& & & & & $1$ & $2$ & $3$ & $0$ & $0$ & & & &\\
& & & $0.$ & $0$ & $1$ & $2$ & $3$ & & & & & &\\
\end{tabu}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam^2=12300\ dm^2\\
&1.23\ dam^2=0.0123\ hm^2
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\begin{minipage}{\textwidth}
\begin{framed}
Example 3 :
\begin{center}
\everyrow{\tabucline-}
\begin{tabu}to\dimexpr\textwidth-2\fboxrule-2\fboxsep{|*{21}{X[-1,c,m]|}}
\multicolumn{3}{|c|}{$km^3$} & \multicolumn{3}{c|}{$hm^3$} & \multicolumn{3}{c|}{$dam^3$} & \multicolumn{3}{c|}{$m^3$} & \multicolumn{3}{c|}{$dm^3$} & \multicolumn{3}{c|}{$cm^3$} & \multicolumn{3}{c|}{$mm^3$}\\
& & & & & & & & $1.$ & $2$ & $3$ & & & & & & & & & &\\
& & & & & & & & $1$ & $2$ & $3$ & $0$ & $0$ & $0$ & $0$ & & & & & &\\
& & & & & $0.$ & $0$ & $0$ & $1$ & $2$ & $3$ & & & & & & & & & &\\
\end{tabu}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam^3=1230000\ dm^3\\
&1.23\ dam^3=0.00123\ hm^3
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\end{document}

And the result :

enter image description here

Thanks in advance for your answers.

1
  • Not really an answer to the second question, but I'm using \tabulinesep=\arraycolsep before \begin{tabu} in the second and third examples. It provides the same minimal spacing in four directions from the cell's content to its borders. Anyway, I still don't understand what's wrong with the width of the second table...
    – someone
    Jun 8, 2017 at 15:05

1 Answer 1

1

Since tabu package is outdated and unmaintained, I will first rewrite the above example with new LaTeX3 package tabularray, and then make some tests with explanation.

The height problem is naturally solved since tabularray has default rowsep. The width problem is about X columns with negative coefficients when there are multicolumn cells.


First Test

I added \SetTblrTracing{+target} in the code to log the width computation for X columns.

\documentclass[12pt,a4paper]{book}

\everymath{\displaystyle}
\usepackage{framed}
\usepackage[fleqn]{mathtools}
\mathtoolsset{showonlyrefs}
\usepackage{tabularray}
\SetTblrTracing{+target}

\begin{document}

\begin{minipage}{\textwidth}
\begin{framed}
Example 1 :
\begin{center}
\begin{tblr}{
  colspec = {|*7{X[-1,c,m]|}},
  hlines,
}
$km$ & $hm$ & $dam$ & $m$ & $dm$ & $cm$ & $mm$ \\
     &      & $1.$  & $2$ & $3$  &      &      \\
     &      & $1$   & $2$ & $3$  &      &      \\
     & $0.$ & $1$   & $2$ & $3$  &      &      \\
\end{tblr}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam=123\ dm\\
&1.23\ dam=0.123\ hm
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\begin{minipage}{\textwidth}
\begin{framed}
Example 2 :
\begin{center}
\begin{tblr}{
  colspec = {|*{14}{X[-1,c,m]|}},
  hlines,
  cell{1}{odd} = {c=2}{c}, % multicolumn
}
$km^2$ & & $hm^2$ &      & $dam^2$ &      & $m^2$ &     & $dm^2$ &     & $cm^2$ & & $mm^2$ & \\
       & &        &      &         & $1.$ & $2$   & $3$ &        &     &        & &        & \\
       & &        &      &         & $1$  & $2$   & $3$ & $0$    & $0$ &        & &        & \\
       & &        & $0.$ & $0$     & $1$  & $2$   & $3$ &        &     &        & &        & \\
\end{tblr}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam^2=12300\ dm^2\\
&1.23\ dam^2=0.0123\ hm^2
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\begin{minipage}{\textwidth}
\begin{framed}
Example 3 :
\begin{center}
\begin{tblr}{
  colspec={|*{21}{X[-1,c,m]|}},
  hlines,
  cell{1}{1,4,7,10,13,16,19} = {c=3}{c}, % multicolumn
}
$km^3$ & & & $hm^3$ & &      & $dam^3$ &     &      & $m^3$ &     &     & $dm^3$ &     &     & $cm^3$ & & & $mm^3$ & & \\
       & & &        & &      &         &     & $1.$ & $2$   & $3$ &     &        &     &     &        & & &        & & \\
       & & &        & &      &         &     & $1$  & $2$   & $3$ & $0$ & $0$    & $0$ & $0$ &        & & &        & & \\
       & & &        & & $0.$ & $0$     & $0$ & $1$  & $2$   & $3$ &     &        &     &     &        & & &        & & \\
\end{tblr}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam^3=1230000\ dm^3\\
&1.23\ dam^3=0.00123\ hm^3
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\end{document}

enter image description here

The tracing log for the second table shows the following:

The property list \l__column_natural_width_prop contains the pairs (without outer braces):
>  {1}  =>  {0pt}
>  {2}  =>  {0pt}
>  {3}  =>  {0pt}
>  {4}  =>  {9.1388pt}
>  {5}  =>  {5.87494pt}
>  {6}  =>  {9.1388pt}
>  {7}  =>  {5.87494pt}
>  {8}  =>  {5.87494pt}
>  {9}  =>  {5.87494pt}
>  {10}  =>  {5.87494pt}
>  {11}  =>  {0pt}
>  {12}  =>  {0pt}
>  {13}  =>  {0pt}
>  {14}  =>  {0pt}.
The property list \l__column_computed_width_prop contains the pairs (without outer braces):
>  {1}  =>  {0.0pt}
>  {2}  =>  {0.0pt}
>  {3}  =>  {0.0pt}
>  {4}  =>  {37.8192pt}
>  {5}  =>  {24.31235pt}
>  {6}  =>  {37.8192pt}
>  {7}  =>  {24.31235pt}
>  {8}  =>  {24.31235pt}
>  {9}  =>  {24.31235pt}
>  {10}  =>  {24.31235pt}
>  {11}  =>  {0.0pt}
>  {12}  =>  {0.0pt}
>  {13}  =>  {0.0pt}
>  {14}  =>  {0.0pt}.

Tabu and tabularray don't consider the widths of multicolumn cells when computing X widths. When all columns have negative coefficients and narrow cells, extra spaces are distributed in proportion to their natural widths.

Now the merged cell from column 1 and 2 has width 12.4pt (2 colsep + 1 rulewidth), and it is not enough to hold $km^2$. Therefore TeX (or tabularray package) extends the width of column 2 and makes the whole table wider. It is the same for cells $cm^2$ and $mm^2$.

In constrast, in Example 3, the merged cell from column 1, 2, 3 has width 24.8pt (4 colsep + 2 rulewidth) and it is enough to hold $km^2$. Therefore the width of table is correct.

And in Example 1, there are no merged cells and none of the natural widths of columns is 0pt, therefore all columns get enough space.


Second Test

Since tabularray has an option hspan=minimal to forbid the extension of multicolumn cells. We added it in the second test, so that you can see the real widths of X columns:

\documentclass[12pt,a4paper]{book}

\everymath{\displaystyle}
\usepackage{framed}
\usepackage[fleqn]{mathtools}
\mathtoolsset{showonlyrefs}
\usepackage{tabularray}
%\SetTblrTracing{+target}

\begin{document}

\begin{minipage}{\textwidth}
\begin{framed}
Example 1 :
\begin{center}
\begin{tblr}{
  colspec = {|*7{X[-1,c,m]|}},
  hlines,
}
$km$ & $hm$ & $dam$ & $m$ & $dm$ & $cm$ & $mm$ \\
     &      & $1.$  & $2$ & $3$  &      &      \\
     &      & $1$   & $2$ & $3$  &      &      \\
     & $0.$ & $1$   & $2$ & $3$  &      &      \\
\end{tblr}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam=123\ dm\\
&1.23\ dam=0.123\ hm
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\begin{minipage}{\textwidth}
\begin{framed}
Example 2 :
\begin{center}
\begin{tblr}{
  colspec = {|*{14}{X[-1,c,m]|}},
  hlines,
  cell{1}{odd} = {c=2}{c}, % multicolumn
  hspan = minimal,
}
$km^2$ & & $hm^2$ &      & $dam^2$ &      & $m^2$ &     & $dm^2$ &     & $cm^2$ & & $mm^2$ & \\
       & &        &      &         & $1.$ & $2$   & $3$ &        &     &        & &        & \\
       & &        &      &         & $1$  & $2$   & $3$ & $0$    & $0$ &        & &        & \\
       & &        & $0.$ & $0$     & $1$  & $2$   & $3$ &        &     &        & &        & \\
\end{tblr}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam^2=12300\ dm^2\\
&1.23\ dam^2=0.0123\ hm^2
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\begin{minipage}{\textwidth}
\begin{framed}
Example 3 :
\begin{center}
\begin{tblr}{
  colspec={|*{21}{X[-1,c,m]|}},
  hlines,
  cell{1}{1,4,7,10,13,16,19} = {c=3}{c}, % multicolumn
  hspan = minimal,
}
$km^3$ & & & $hm^3$ & &      & $dam^3$ &     &      & $m^3$ &     &     & $dm^3$ &     &     & $cm^3$ & & & $mm^3$ & & \\
       & & &        & &      &         &     & $1.$ & $2$   & $3$ &     &        &     &     &        & & &        & & \\
       & & &        & &      &         &     & $1$  & $2$   & $3$ & $0$ & $0$    & $0$ & $0$ &        & & &        & & \\
       & & &        & & $0.$ & $0$     & $0$ & $1$  & $2$   & $3$ &     &        &     &     &        & & &        & & \\
\end{tblr}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam^3=1230000\ dm^3\\
&1.23\ dam^3=0.00123\ hm^3
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\end{document}

enter image description here


Third Test

Now we knew the real problem of the extra width. And naturally we have the solution: add some space in cells below the merged cells $km^2$, $cm^2$ and $mm^2$.

\documentclass[12pt,a4paper]{book}

\everymath{\displaystyle}
\usepackage{framed}
\usepackage[fleqn]{mathtools}
\mathtoolsset{showonlyrefs}
\usepackage{tabularray}
%\SetTblrTracing{+target}

\begin{document}

\begin{minipage}{\textwidth}
\begin{framed}
Example 1 :
\begin{center}
\begin{tblr}{
  colspec = {|*7{X[-1,c,m]|}},
  hlines,
}
$km$ & $hm$ & $dam$ & $m$ & $dm$ & $cm$ & $mm$ \\
     &      & $1.$  & $2$ & $3$  &      &      \\
     &      & $1$   & $2$ & $3$  &      &      \\
     & $0.$ & $1$   & $2$ & $3$  &      &      \\
\end{tblr}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam=123\ dm\\
&1.23\ dam=0.123\ hm
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\begin{minipage}{\textwidth}
\begin{framed}
Example 2 :
\begin{center}
\begin{tblr}{
  colspec = {|*{14}{X[-1,c,m]|}},
  hlines,
  cell{1}{odd} = {c=2}{c}, % multicolumn
  hspan = minimal,
}
$km^2$ &    & $hm^2$ &      & $dam^2$ &      & $m^2$ &     & $dm^2$ &     & $cm^2$ &    & $mm^2$ &    \\
 \;    & \; &        &      &         & $1.$ & $2$   & $3$ &        &     &  \;    & \; &  \;    & \; \\
       &    &        &      &         & $1$  & $2$   & $3$ & $0$    & $0$ &        &    &        &    \\
       &    &        & $0.$ & $0$     & $1$  & $2$   & $3$ &        &     &        &    &        &    \\
\end{tblr}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam^2=12300\ dm^2\\
&1.23\ dam^2=0.0123\ hm^2
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\begin{minipage}{\textwidth}
\begin{framed}
Example 3 :
\begin{center}
\begin{tblr}{
  colspec={|*{21}{X[-1,c,m]|}},
  hlines,
  cell{1}{1,4,7,10,13,16,19} = {c=3}{c}, % multicolumn
  hspan = minimal,
}
$km^3$ & & & $hm^3$ & &      & $dam^3$ &     &      & $m^3$ &     &     & $dm^3$ &     &     & $cm^3$ & & & $mm^3$ & & \\
       & & &        & &      &         &     & $1.$ & $2$   & $3$ &     &        &     &     &        & & &        & & \\
       & & &        & &      &         &     & $1$  & $2$   & $3$ & $0$ & $0$    & $0$ & $0$ &        & & &        & & \\
       & & &        & & $0.$ & $0$     & $0$ & $1$  & $2$   & $3$ &     &        &     &     &        & & &        & & \\
\end{tblr}
\end{center}
\begin{equation}
\begin{split}
&1.23\ dam^3=1230000\ dm^3\\
&1.23\ dam^3=0.00123\ hm^3
\end{split}
\end{equation}
\end{framed}
\end{minipage}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .