# How to minimize the row height in a latex table

I would like to know how I can minimize the height of a row with empty table cells to zero, or to put the question in another form: How to make the minimum height of a row zero, so it will exactly take on the height of anything that I pass in the table cells of that row, regardless of how tiny it is.

I know about the \\[...] syntax, but what is the right length to enter (actually: subtract) there (as it is depending on the font height obviously)? Can anybody tell me how the default height is constructed in tabular like environments, so I can use this formula to fill in the corresponding negative length via \\[...]?

Or is there a trick to remove the "default" strut in a cell (this is how I understand it is being done but I may be wrong as all of this how the array package constructs the \halign is way beyond my current understanding), so it collapses to whatever I put in there (e.g. a rule with a height of 2 pt or so)?

Because the understandable wish for an example was articulated I will try to explain the what and why a bit better, and give some example code that demonstrates what I want to achieve, and how this is done incorporating the solution given by Donald Arseneau.

The initial need is to typeset a table with chords and their allowed tensions, weak and strong harmonies and avoid notes. It doesn't matter if you don't get what I am talking about, just think of the periodic table of elements, this has a similar need for color and very selective borders around cells, only that in my case I would also want to vary the border thickness and border color (yes, I know about booktabs.sty - you simply have to believe me that the typographical remarks there don't apply at all to my situation.)

What I present is not my final product but still a synthetic example with an evolution of what I tried and what my current state is (with remarks given here referring to the 4 different approaches/evolutionary steps I show).

First the code

\documentclass[10pt]{article}

\usepackage{array}
\usepackage[table]{xcolor}
\usepackage{ehhline} % Important: this needs version >= 1.1
\usepackage{lipsum}

%%% just some shortcuts to make live easier
\definecolor{lred}{rgb}{1,0.85,0.85}
\definecolor{lgreen}{rgb}{0.85,1,0.85}
\definecolor{lblue}{rgb}{0.85,0.85,1}
\definecolor{lgray}{rgb}{0.85,0.85,0.85}

%%% define the thickness of inner lines
\def\irw{3pt}
\def\rrh{-\dimexpr\arraystretch\normalbaselineskip-\irw\relax} % used in example 4

% To make it possible to read the \\hhlines below
% w/o wanting to shoot yourself in the head:
\newcommand\arc[1]{\arrayrulecolor{#1}}
% Shortcut used for solution with !{...} of \ehhline: A coloured rule segment

\begin{document}
\begin{center}\Large
\begin{tabular}{| >{\columncolor{lred}}c>{\columncolor{lgreen}}c
>{\columncolor{lblue}}c >{\columncolor{lgray}}c |}
\hline
{\normalsize I} &  &  & \\
\hhline{|~>{\arc{red}}--~>{\arc{black}}|}
& \multicolumn{1}{!{\color{red}\vline} >{\cellcolor{lgreen}}c}{+}
& \multicolumn{1}{>{\cellcolor{lblue}}c !{\color{red}\vline}}{+}
& \\
\hhline{|~>{\arc{red}}--~>{\arc{black}}|}
{\normalsize II} &  &  & \\
\hhline{>{\arc{black}}|>{\arc{lred}}->{\arc{red}}-->{\arc{lgray}}->{\arc{black}}|}
& \multicolumn{1}{!{\color{red}\vrule width \irw} >{\cellcolor{lgreen}}c}{+}
& \multicolumn{1}{>{\cellcolor{lblue}}c !{\color{red}\vrule width \irw}}{+}
& \\
\hhline{>{\arc{black}}|>{\arc{lred}}->{\arc{red}}-->{\arc{lgray}}->{\arc{black}}|}
{\normalsize III} &  &  &  \\
\hhline{|!{\crs{lred}{\irw}}!{\crs{red}{\irw}}!{\crs{red}{\irw}}!{\crs{lgray}{\irw}}|}
& \multicolumn{1}{!{\color{red}\vrule width \irw}>{\cellcolor{lgreen}}c}{+}
& \multicolumn{1}{>{\cellcolor{lblue}}c!{\color{red}\vrule width \irw}}{+}
&  \\
\hhline{|!{\crs{lred}{\irw}}!{\crs{red}{\irw}}!{\crs{red}{\irw}}!{\crs{lgray}{\irw}}|}
{\normalsize IV} &  &  &  \\
& \multicolumn{2}{>{\cellcolor{red}}c}{} & \\[\rrh]
& \multicolumn{1}{!{\color{red}\vrule width \irw}>{\cellcolor{lgreen}}c}{+}
& \multicolumn{1}{>{\cellcolor{lblue}}c!{\color{red}\vrule width \irw}}{+}
&  \\
& \multicolumn{2}{>{\cellcolor{red}}c}{} & \\[\rrh]
&  &  &  \\
\hline
\end{tabular}

\end{center}
\end{document}


and this is the result:

Remarks:

I) This is the most naive way with \hhline. Features the dreaded white space where no rule segment should be. Also no increased thickness.

II) Using correctly colored line segments everywhere in the \hhline and placing fat \vstruts via \multicolumn{1}... helps to get there partially, but I see no way to make the horizontal part thicker with this approach. And already I have the impression that it gets cluttered and an excessive effort to handle the white space correctly.

III) Using ehhline.sty and \leaders in the \hhline. Works (I think the little missing part at the right is an artefact of the pdf viewer's zooming). It simply feels like an incredibly excessive effort to reach the result, and would be completely unreadable without the defined shortcuts.

Side question: Does anybody know a more elegant way than to stick tiny rules via \leaders together here (see the definition of \crs in my code)? How could I just put in one correctly sized rule in there? Is there any way to get to the computed column width?

IV) Trying to minimize the effort, I thought about skipping \hline and related altogether and instead construct the vertical rules as regular cells. This is where the question arose. With the right amount of skipping back and adding my thickness it works.

So I have two possible ways now, but unfortunately they don't yet solve all cases. I need in one table bordered cells with differing widths. Obviously both III) and IV) assume one thickness for horizontal borders per row. So I am trying to stop using the cell itself as a rule, but instead use the cells only for background coloring and again sticking in rules for the borders (which would need to be aligned at the bottom for top borders and aligned to the top for bottom borders). But here I am back at my side question above: I would need a way to insert colored rules of the computed column width and the desired thickness into cells that hold the border. This is currently unsolved for me.

• Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Apr 8, 2020 at 13:27
• THis is hard to do - as there is no "problem" in a sense. I don't exactly know what I could add as a MWE other than any bare bone tabular environment. The question is about how to achieve a certain effect that I can't demonstrate in the MWE because - and that's circular problem here - I don't know how to achieve the effect :-) Apr 8, 2020 at 13:32
• Could you maybe add a sketch of the desired output or at least a more in depth description of/background information on the desired effect? Probably a tabular is not the best tool here? Apr 8, 2020 at 13:57
• I did... but I fear not everybody wants to follow my journey through that topic :-). I didn't remove the previous examples from my code people understand where I come from. Apr 9, 2020 at 10:33
• I don't undertand why you need various column widths. The example shows three mostly equivalnet columns (lred, lgreen, lblue) with plus in them. Will be more plus signs in one column? If not then we can set fixed colums widths and we need not \halign primitive. The various border width plays a minor role because we can set outer widths of all bordered elements the same (as the picture shows). Apr 9, 2020 at 12:20

The example to provide would be a simple tabular that has the extra blank line, but which you'd like to collapse. For example, I see it would be easy to detect a completely blank row

hello & world \\
\\


and suppress that (ignore the row entirely), but it would not collapse things like

hello & world \\
& \\


Anyway, and the reason this incomplete answer is entered as an answer, is your request for the explicit spacing is simple but the answer is somewhat obscure

\\[-\arraystretch\normalbaselineskip]


(\baselineskip is zero while making the tabular.)

Oh yes, and if you just want to avoid the apparent line-skips enforced by hidden struts, throughout a whole tabular, just set the stretch to zero

\renewcommand\arraystretch{0}

• Thank you very much, I don't think the answer is obscure. I didn't know about \normalbaselineskip, but instead was finding out the hard way that \baselineskip is zero in tables... Apr 9, 2020 at 10:37
• Yes, @JensLautenbacher, it is just that that obscures the answer. Apr 9, 2020 at 20:35

Simply, don't use \strut when you are using \halign inside a \vbox where \offinterlineskip is set:

\vbox{\offinterlineskip\halign{&\ \hfil#\hfil\cr
abc & de \cr
xx  & vv \cr
Ayv \cr
}}

\bye


If you explicitly want to use LaTeX tabular environment, just deactivate \strut locally:

\documentclass{article}

\begin{document}

{\setbox\strutbox=\null
\begin{tabular}{cc}
abc & de \\
xx  & vv \\
Ayv
\end{tabular}
}

\end{document}


Your example does not show all complexity of your problems. So I don't show complete answer. I don't sure that all complicated things you mentioned are explicitly needed. My answer does not use \halign. When I will know why to use it then I am able to correct this answer (may be with \halign usage). My example shows how to do this at TeX primitive level (colors are from OPmac or OpTeX packages designed for plain TeX).

\input opmac

\newdimen\colw \colw=20pt
\newdimen\irw  \irw=3pt
\def\rulestrut{height.6\colw depth.25\colw}

\def\varth#1{\ifcase#1 .4pt\or \irw\fi}
\def\colfram#1#2{\vbox{\Red\hrule height\varth#1\vtop{%
\hbox to2\colw{\vrule \rulestrut width\varth#2\hss
\vrule width\varth#2}%
\hrule height\varth#1}}}
\def\redframe#1#2#3#4{\hbox{\rlap{\colfram#1#2}%
\typosize[14/]
\hbox to\colw{\hss\kern#2\irw#3\hss}\hbox to\colw{\hss#4\kern#2\irw\hss}}}

\def\row#1#2{\hbox to3.5\colw{%
{\LiRed\rule\LiGreen\rule\LiBlue\rule\LiGrey\vrule width.5\colw}%
\kern-3.5\colw
\vrule \rulestrut width0pt
\hbox to\colw{\hss #1\hss}#2\hss}\nointerlineskip
}
\def\rule{\vrule width\colw}

\def\LiRed   {\setrgbcolor{1 0.85 0.85}}
\def\LiGreen {\setrgbcolor{0.85 1 0.85}}
\def\LiBlue  {\setrgbcolor{0.85 0.85 1}}
\def\LiGrey  {\setrgbcolor{0.85 0.85 0.85}}
\localcolor

\frame{\vbox{
\row {I}   {}
\row {}    {\redframe 00++}
\row {II}  {}
\row {}    {\redframe 10++}
\row {III} {}
\row {}    {\redframe 11++}
\row {IV}  {}
}}

\bye


Note that the concept of macros must respect the usage of them. We can't dive to the complicated code inside tabular environment when we want to create the results.

• So essentially what you are saying is to forget about all the LaTex tabular and related stuff (and their features) and learn to use low-level stuff directly? I would have really wanted to avoid going down that road, but I come more and more to the opinion that this may be the only way... Apr 8, 2020 at 16:24
• @JensLautenbacher I just showed that your task is very natural from TeX primitive point of view: just don't use \strut. Apr 9, 2020 at 3:27
• Yes, I was assuming that. And maybe it is the best way for what I want (see my expanded question with example above) to simply learn the "natural" way instead of using the tons of abstraction piled on top by tabular, array.sty and friends... But I am not sure if I would also be able to do all the coloring and other stuff these abstractions give me in plain TeX Apr 9, 2020 at 10:36