[Tex/LaTex] A gap between text and line in table

spacingtables

To eliminate the problem I have fboxed shortstack with zero fboxsep:

\documentclass[a4paper,11pt]{extarticle}
\usepackage{array}
\begin{document}

\newcommand{\mystr}{9:00\\10:30}

\setlength{\fboxsep}{0pt}

\begin{tabular}{m{5mm}|m{15mm}|m{5mm}}
  &&\\\hline
  &\fbox{\bf\shortstack{\mystr}}&\\\hline
  &&\\%
\end{tabular}
\end{document}

and get as result:
enter image description here
so question: What is nature of the gap between shortstack bottom
and rows separtion line? and how the gap can be avoided?

Best Answer

In the middle cell there are only a line with a box. That is, for LaTeX the stacked text is just like a huge character. In fact, the problem is exactly the same with a cell with \huge X. Remember that any character is a box, and that a box is managed like a character.

The table rows have a always a minimal vertical space, to ensure that any normal font fit perfectly, no matter if the character have ascender (like "f"), descender (like "g") or none (like "m"), or are only small dots. As you know, they are not aligned at the top, bottom or the middle of the row , as vectorial objects in a drawing program, but lined up at their baseline (red lines in the MWE), where most letters "sit" in this baseline, except those with a descender:

mwe

This cannot be done with 2D boxes with only a total height and width, so TeX boxes are pseudo 3D rentangles with a width, a height (upon the baseline) and a depth (the height under the baseline). That is, height + depth is the total height that we see.

If there are a box with higher height or depth than the normal font in a table row, then TeX make room to fit one or both dimensions but unfortunately, not enough to avoid colliding with top or bottom lines, or both. Note in the MWE that a medium "p" can touch both lines, while a huge "X" cannot touch the bottom line, no matter how big it is, because the depth of X is always 0pt.

In your case, the \shortstack box increase considerably only the height, as a huge "X" does, thus touching the top line, but left unchanged the depth, so the your bottom gap is simply the default space reserved for descenders. If you add to the same \shortstack box a final "p", the apparent gap is reduced considerably.

Therefore, although visually unpleasant ,your bottom gap is correct. The real problem is the lack of padding above the \shortstack box. You can solve this locally with \\[<length>], or with two global approaches:

mwe2

1) Increase a bit the height of the rows, with \extrarowheight.

2) Change the minimum vertical space with the array stretch factor (\arraystretch) that is 1 by default, that mean increase both height and depth proportionally. At low values could produce odd or similar similar result that \extrarowheight but this have the effect of a bottom align, whereas \arraystretch tend to a balanced vertical padding (or a balanced lack of padding when less of 1).

The complete code of the above images:

\documentclass[a4paper,11pt]{extarticle}
\usepackage{array,xcolor}
\newcommand\mystr{\colorbox{cyan!50}{\bfseries\shortstack{9:00\\10:30}}}
\newcommand\mystrpm{\colorbox{orange!50}{\bfseries\shortstack{9:00pm\\10:30pm}}}
\newcommand\baseline{\leavevmode\rlap{\color{red!50}\rule{.3\textwidth}{.4pt}}}
\setlength{\parskip}{1em}
\setlength{\fboxsep}{0pt}

\newlength\myhe
\settoheight{\myhe}{X}
\newlength\myde
\settodepth{\myde}{p}
\newlength\myhede
\setlength{\myhede}{\dimexpr\myhe+\myde}

\begin{document}

\twocolumn[\centering
\begin{tabular}{m{3mm}||m{20mm}|m{3mm}}\hline
\baseline x & \fbox{x} x x x &  x \\\hline
\baseline . & \fbox{.} . . . &  . \\\hline
\baseline x & \fbox{h} h \fbox{g} g &  x \\\hline
\baseline x & \baseline h \fbox{\Huge X} &  x \\\hline
\baseline x & \baseline\fbox{h} h \fbox{\huge p} &  x \\\hline
\end{tabular}\bigskip]

{\Large\bfseries \verb|\extrarowheight|}

 +0pt (default) \par 

%\setlength{\extrarowheight}{0pt} % default
\begin{tabular}{m{3mm}||m{20mm}|m{3mm}}\hline
\baseline . & ... &  . \\\hline
\baseline x & \baseline\hfil\mystr &  x \\\hline
\baseline x & \baseline\hfil\mystrpm &  x \\\hline
\end{tabular} 

-\the\numexpr\number\myde/65536\relax pt (-depth)\par

\setlength{\extrarowheight}{\dimexpr-\myde} 
\begin{tabular}{m{3mm}|m{20mm}|m{3mm}}\hline
\baseline . & ... &  . \\\hline
\baseline x & \hfil\mystr &  x \\\hline
\baseline x & \hfil\mystrpm &  x \\\hline
\end{tabular} 

+\the\numexpr\number\myhe/65536\relax pt (heigth)\par

\setlength{\extrarowheight}{\myhe} 
\begin{tabular}{m{3mm}|m{20mm}|m{3mm}}\hline
\baseline . & ... &  . \\\hline
\baseline x & \hfil\mystr &  x \\\hline
\baseline x & \hfil\mystrpm &  x \\\hline
\end{tabular} 

+\the\numexpr\number\myhede/65536\relax pt (heigth + depth)\par

\setlength{\extrarowheight}{\dimexpr\myhe+\myde} 
\begin{tabular}{m{3mm}|m{20mm}|m{3mm}}\hline
\baseline . & ... &  . \\\hline
\baseline x & \hfil\mystr &  x \\\hline
\baseline x & \hfil\mystrpm &  x \\\hline
\end{tabular} 

\newpage
{\Large\bfseries \verb|\arraystretch|}

\setlength{\extrarowheight}{0pt} % return to default

0 (no default space)\par 
\renewcommand\arraystretch{0}
\begin{tabular}{m{3mm}|m{20mm}|m{3mm}}\hline
\baseline . & ... &  . \\\hline
\baseline x & \hfil\mystr &  x \\\hline
\baseline x & \hfil\mystrpm &  x \\\hline
\end{tabular} 

$\times1$ (default)\par 
\renewcommand\arraystretch{1}
\begin{tabular}{m{3mm}|m{20mm}|m{3mm}}\hline
\baseline . & ... &  . \\\hline
\baseline x & \hfil\mystr &  x \\\hline
\baseline x & \hfil\mystrpm &  x \\\hline
\end{tabular} 

$\times2$ \par 
\renewcommand\arraystretch{2}
\begin{tabular}{m{3mm}|m{20mm}|m{3mm}}\hline
\baseline . & ... &  . \\\hline
\baseline x & \hfil\mystr &  x \\\hline
\baseline x & \hfil\mystrpm &  x \\\hline
\end{tabular} 

$\times2.5$ \par 
\renewcommand\arraystretch{2.5}
\begin{tabular}{m{3mm}|m{20mm}|m{3mm}}\hline
\baseline . & ... &  . \\\hline
\baseline x & \hfil\mystr &  x \\\hline
\baseline x & \hfil\mystrpm &  x \\\hline
\end{tabular} 


\end{document}