# shrinkable glue in table

I doubt that there's a way to do this, and I've only run into a situation that needs this feature only once, at that time there was a simple workaround, but I've been amazed at what people here come up with, so I'm asking anyway.

Consider the following simple document:

\documentclass[12pt, twocolumn]{article}
\usepackage{microtype}
\begin{document}
\noindent
zero one two three four five six seven eight~nine

\noindent
\begin{tabular}{@{}l@{}}
zero one two three four five six seven eight nine
\end{tabular}
\end{document}


The first paragraph is just the words for counting from 0 to 9. It fits on one line, because spaces in a paragraph can stretch and shrink. The second paragraph is the same text, but embedded in table. Unfortunately, this causes the entire thing to be typeset at its natural width and thus overflow into the next column and were it not for the fact that the other column happens to be empty here, we would have an unreadable mess.

Of course, in this contrived example, the answer is "don't do that"; a table with one column and one row is pretty silly. But consider the following more realistic example:

short      LLLOOONNNG
LLLOOONNNG short


Here, the only simple workaround I can think of is to visually examine the longest line in each column, and manually wrap it in a parbox that's narrow enough to induce compression, but not so narrow as to induce line breaking. This requires quite a bit of tedious trial and error.

When it comes to page breaking, LaTeX has \enlargethispage*{0pt}, which forces the page to be typeset as tightly as possible. It would be neat if some analogue worked here. Even better would be if there were no need to make things as tight as possible in the cases where just a bit more room is needed.

TeX never use the shrink and stretch part of interword glue when typesetting a table cell declared with l, c or r, because it adds infinite glue at one or either end.

You can typeset a cell with the interword glue shrinked at the maximum allowed by the font parameters by using \spaceskip:

\documentclass[12pt,draft]{article}
\usepackage{microtype}
\usepackage{array}

\newcommand{\maxshrink}{%
\spaceskip=\dimexpr\fontdimen2\font-\fontdimen4\font\relax
}

\newlength{\testwd} % for testing

\begin{document}

\settowidth{\testwd}{zero one two three four five six seven eight~nine}
\addtolength{\testwd}{-8pt} % make TeX shrink the first paragraph

\parbox{\testwd}{
zero one two three four five six seven eight~nine

\begin{tabular}{@{}>{\maxshrink}l@{}}
zero one two three four five six seven eight nine
\end{tabular}

\begin{tabular}{@{}l@{}}
zero one two three four five six seven eight nine
\end{tabular}

}
\end{document}


You might add an optional parameter, say

\newcommand{\maxshrink}[1][1]{%
\spaceskip=\dimexpr\fontdimen2\font-#1\fontdimen4\font\relax
}


and call it like \maxshrink[0.8] if you don't want the shrink to be the maximum allowed, but only 80%.

• Not bad. An "impossible" problem 90% solved within half an hour, using a solution that I even mostly understand. A few shortcomings exist, such as: does not work with math ($1+1$ is still set at natural width), microtype doesn't get the hint so only the spaces shrink, not the characters, and the user needs to manually fine-tune the shrink percentage, rather than TeX inferring it from context automatically. But still! This is awesome! This would have totally solved my problem that time! (It's not even too late: it seems likely I'll need to typeset that table again in the next few months.) – Mark May 14 '16 at 21:24
• @Mark Spaces in math use a different model than interword space, so they aren't influenced by \spaceskip. I'm afraid microtype doesn't use character expansion when TeX is typesetting a box (in particular a table cell): this feature only comes into action during paragraph typesetting, when the line breaks are decided upon. – egreg May 14 '16 at 21:34