2

To set a maximum column width, I use varwidth as suggested by this answer. However, the vertical padding is only correct for single line cells, and is too little for multiline ones. Minimal example:

\documentclass{article}

\usepackage{varwidth}
\usepackage{array}

\newcolumntype{M}{>{\begin{varwidth}{1cm}}l<{\end{varwidth}}}

\begin{document}

\begin{tabular}{|M|c|}
    \hline
     asdf      & \\ \hline
     asdf asdf & \\ \hline
\end{tabular}

\end{document}

This results is:

enter image description here

I tried to use varwidth inside the cells (instead of defining a new column type with the array package), but this yielded the same result:

\documentclass{article}

\usepackage{varwidth}

\begin{document}

\begin{tabular}{|c|c|}
    \hline
     \begin{varwidth}{1cm}asdf     \end{varwidth} & \\ \hline
     \begin{varwidth}{1cm}asdf asdf\end{varwidth} & \\ \hline
\end{tabular}

\end{document}

I also tried adding \par or double newlines to the cells, but none of my attempts resulted in proper vertical padding. Adding manual vspaces also did not work.

Is there a way to guarantee the same vertical padding for both single and multiline cells?

2

Load the cellspace package, which aims at ensuring a minimal vertical padding at the top and bottom of cells and (slightly) change the definition of the M column type with the S prespecifier:

\documentclass{article}

\usepackage{varwidth}
\usepackage{array}
\usepackage{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{6pt}

\newcolumntype{M}{>{\begin{varwidth}{1cm}}Sl<{\end{varwidth}}}

\begin{document}

\begin{tabular}{|M|c|}
    \hline
     asdf & \\ \hline
     asdf asdf & \\ \hline
\end{tabular}

\end{document} 

enter image description here

| improve this answer | |
  • Thanks! This worked well with the rest of my document - which has longtables, tabularxs with redefined tabularxcolumn, raggedright cells, etc. – Gabor Szarnyas Aug 6 '17 at 17:34
  • 1
    Just beware: if you load siunitx, which defines an S column type, you'll have to replace S with C. – Bernard Aug 6 '17 at 17:48
1

try

\documentclass{article}
\usepackage{varwidth}
\usepackage{makecell}% added
\setcellgapes{5pt}   % added

\begin{document}

{\makegapedcells    % added
\begin{tabular}{|c|c|}
    \hline
     \begin{varwidth}{1cm}asdf     \end{varwidth} & \\ \hline
     \begin{varwidth}{1cm}asdf asdf\end{varwidth} & \\ \hline
\end{tabular}
}

\end{document}

note: this solution -- using \makegapedcells -- not work with column type m nor enable coloring table using colortbl or [table]{xcolor} packages. but this not degrade usefulness of this solution a lot since use of them is rare.

benefits of its use is that you not need to care about adding S attribute to cells type nor specific of its use in combination of S column type from siunitx package (also rare happen, but it is good to know, that there instead of S you should use C, what can be again limitation in case of use tabularx ... etc :) )

enter image description here

| improve this answer | |
  • Thanks! This works well with the minimal example, but has some issues with my full document. Chief among these is the incompatibility with cell colors: I added \usepackage[table]{xcolor} to the preamble and added \cellcolor{green} to the first cell, but it had no effect. – Gabor Szarnyas Aug 6 '17 at 17:29
  • It also did not work with the m column of the tabularx package. – Gabor Szarnyas Aug 6 '17 at 17:35
  • @GaborSzarnyas, in your question is not mentioned, that you need mcolumn type! it is well known, that macro \makegapedcells is not compatible with it, so in this case i would recommended solution with cellspace package as Bernard do. – Zarko Aug 6 '17 at 19:01
  • I tried to make the MWE as compact as possible and did not think that cell color or the m column type makes a difference for cell padding. Sorry for this. Your answer is a quality one, and thank you for editing it to include the aforementioned limitations. Too bad I cannot accept both answers. – Gabor Szarnyas Aug 6 '17 at 19:19
  • 1
    @GaborSzarnyas, i extend my note with some further information :) maybe it can help you. happy tex-ing! – Zarko Aug 6 '17 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.