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:


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.


\usepackage{varwidth} %for the varwidth minipage environment


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

  \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\\   

  • Welcome to TeX SX! Could post a minimal full example that illustrates your problem?
    – Bernard
    Commented Sep 14, 2017 at 21:40
  • I will edit. Apologies.
    – Nick
    Commented Sep 14, 2017 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? Commented Sep 14, 2017 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
    Commented Sep 15, 2017 at 14:40

2 Answers 2


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



 enhanced,frame hidden,interior hidden,%



  \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\\   


This yields:

enter image description here

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
    Commented Sep 15, 2017 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. Commented Sep 15, 2017 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
    Commented Sep 15, 2017 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:



\begin{tabular}{rr rr rr}   
\mcp{0.25}{This is sometimes long}  & 
\mcp{0.50}{\lipsum*[11]}            &  
\mcp{0.25}{and this can be long}            \\
key & value & key & value   & key  & value  \\
  0 & 1     & 0   & 1       & 0    & 1      \\ 

enter image description here

You must log in to answer this question.

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