3

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?

3 Answers 3

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

2
  • Thanks! This worked well with the rest of my document - which has longtables, tabularxs with redefined tabularxcolumn, raggedright cells, etc. Commented Aug 6, 2017 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
    Commented Aug 6, 2017 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

5
  • 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. Commented Aug 6, 2017 at 17:29
  • It also did not work with the m column of the tabularx package. Commented Aug 6, 2017 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
    Commented Aug 6, 2017 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. Commented Aug 6, 2017 at 19:19
  • 1
    @GaborSzarnyas, i extend my note with some further information :) maybe it can help you. happy tex-ing!
    – Zarko
    Commented Aug 6, 2017 at 19:28
0

When loaded after array, the package varwidth defines a new columntype called V. With that column type, you have directly the expected output.

\documentclass{article}
\usepackage{array}
\usepackage{varwidth}% must be loaded after array

\begin{document}

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

\end{document}

Output of the above code

You must log in to answer this question.

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