# How to force a maximum cell height?

I have a python script that spits out files that are subsequently read in by latex to compile a report. At times, user text labels are long and risk breaking my latex tables.

I'm using the following to limit table cell width:

\newcolumntype{M}{>{\begin{varwidth}{0.3\textwidth}}c<{\end{varwidth}}}


However, now long text spills onto a second (or third...) line which makes my table grow. The table still looks good, but I'd like to prevent it from growing so that the table size is consistent.

I tried \noindent\begin{minipage}[t][2.5cm][t]{\textwidth} ... \end{minipage} but that didn't seem to work.

I'd prefer to shrink the typeface to make it fit. Alternately, I'd accept truncation.

MWE:

\documentclass{article}
\usepackage{array}
\usepackage{varwidth} %for the varwidth minipage environment

\begin{document}

\newcolumntype{M}{>{\begin{varwidth}{0.3\textwidth}}c<{\end{varwidth}}} %M is for Maximal column

\begin{tabular}{rrrrrrrr}
\multicolumn{2}{M} {This is sometimes long} &  & \multicolumn{2}{M}{This is also sometimes long but I don't want the table to grow with it} &  & \multicolumn{2}{M}{and this can be long} \\
key & value &  & key & value &  & key & value \\
0 & 1 &  & 0 & 1 &  & 0 & 1\\
\end{tabular}

\end{document}

• Welcome to TeX SX! Could post a minimal full example that illustrates your problem? – Bernard Sep 14 '17 at 21:40
• I will edit. Apologies. – Nick Sep 14 '17 at 21:52
• Is there a reason why you don’t just use p{0.3\textwidth} as column type? Do I get it right, that the cell should automatically shrink the text (or truncate it) if it is longer than, say, two lines? – Jasper Habicht Sep 14 '17 at 22:44
• p{0.3\textwidth} would largely do the same as my newcolumntype scheme. Both restrict the cell from growing too far horizontally (or, with p{}, changing horizontal shape at all). I want to prevent vertical growth beyond, say, 2 lines. – Nick Sep 15 '17 at 14:40

Hm, you could try tcolorbox using a tcboxfit like this (a little hacky and surely not the best solution from a typographic aspect):

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{fitting,skins}

\newtcboxfit{\cellbox}[1]{%
enhanced,frame hidden,interior hidden,%
boxrule=0pt,boxsep=0pt,arc=0pt,%
left=0pt,right=0pt,top=0pt,bottom=0pt,%
height=#1,width=.3\textwidth%
}

\newcommand{\autoshrinkcell}[2]{%
\multicolumn{#1}{l}{\cellbox{10mm}{#2}}%
}

\begin{document}

\begin{tabular}{rrrrrrrr}
\autoshrinkcell{2}{This is sometimes long} &  & \autoshrinkcell{2}{This is also sometimes long but I don't want the table to grow with it} &  & \autoshrinkcell{2}{and this can be long} \\
key & value &  & key & value &  & key & value \\
0 & 1 &  & 0 & 1 &  & 0 & 1\\
\end{tabular}

\end{document}


This yields:

The table row will never exceed the defined height of the box, which will decrease the font size in order to make it fit.

I don’t know, however, why there is this strange gap below the box and before the next row. I can’t figure out how to delete it … maybe someone else knows where this comes from?

Anyways, it would be better to think of another solution where you don’t need to squeeze the text (see for example Zarko’s suggestion).

• if the text in the second \autoshrinkcell is longer than 9 lines, it becomes unreadable small and also start to overlap next row. also aesthetically aspect of op wish lead to terrible appearance of table ... – Zarko Sep 15 '17 at 0:23
• @Zarko you're right, it is not beautiful. But if it only happens a few time it may be a reasonable solution. – Jasper Habicht Sep 15 '17 at 5:23
• @JasperHabicht: Thanks. This is what I'm looking for. For the most part, I hope this won't end up used but I don't want a user's verbosity to break my report generator. – Nick Sep 15 '17 at 14:42
• from technical aspect is possible design cells with automatically reduction of fonts when text in cell is longer than prescribed cell width
• from aesthetic point of view such a solution lead to (to my taste) to bad appearance of table

i would rather rethinking how to design table. for example:

• use smaller font size (\small)
• reduce line spread in multi column cells
• manually adjust width of multi column cells with goal that cells heights (i.e.: number of text lines in cells) become balanced as much as possible.

table, based on above presumption, is:

\documentclass{article}
\usepackage[showframe,
margin=25mm]{geometry}
\usepackage{array}
\newlength\colwidth
\newcommand\mcp[2]{\multicolumn{2}{%
p{\dimexpr#1\linewidth-2\tabcolsep}}{#2}}

\usepackage{lipsum}

\begin{document}
\begin{table}[htb]