3

The context

I'm trying to create a table that meets the following conditions

  1. the padding on all cells from the first row must be bigger than the default padding.
  2. the content of all cells must be vertically center aligned.

Regarding the condition no. 1: I know how to change the padding on all cells but don't know how to do it for all cells of an specific row without causing this effect on all the other rows (see document below)

\documentclass{article}

\begin{document}

\def\arraystretch{2}

\noindent
\begin{tabular}{|c|c|c|}
   \hline a & b & c
\\ \hline a & b & c
\\ \hline a & b & c
\\ \hline
\end{tabular}

\end{document}

enter image description here

Regarding the condition no. 2: I know how to do it.

\documentclass{article}

\usepackage{array, lipsum}

\begin{document}

\noindent
\begin{tabular}{|m{5cm}|m{5cm}|}
   \hline a & \lipsum[1][1-2]
\\ \hline \lipsum[2][1-2] & b
\\ \hline
\end{tabular}

\end{document}

enter image description here

The question

How can I create a table that meet condition no. 1 and condition no. 2?

1
  • Use \multicolumn{1}{|c|}{a} instead of a, \multicolumn{1}{c|}{b} instead of b to center them.
    – ZhiyuanLck
    Jun 16, 2020 at 14:38

4 Answers 4

1

I use zero width rules piped into a zero width m-column to set the height of the rows. In addition, I define a new column type, M, which vertically and horizontally centres its content.

In example 2, I have defined a new command \rl, which allows you to set the row height individually.

Example 1 - same height rows

enter image description here

\documentclass{article}

\usepackage{array}
\newcolumntype{M}{>{\centering\arraybackslash}m{15pt}}

\begin{document}

\noindent
\begin{tabular}{|*{3}{M|}>{\rule{0pt}{30pt}}m{0pt}}
   \cline{1-3} a & b & j &
\\ \cline{1-3}  a & b & j &
\\ \cline{1-3}  a & b & j &
\\ \cline{1-3} 
\end{tabular}

\end{document}

Example 2 - different height rows

enter image description here

\documentclass{article}

\usepackage{array}
\newcolumntype{M}{>{\centering\arraybackslash}m{15pt}}
\newcommand{\rl}[1]{\rule{0pt}{#1pt}}


\begin{document}


\noindent
\begin{tabular}{|*{3}{M|}m{0pt}}
   \cline{1-3} a & b & j & \rl{16}
\\ \cline{1-3}  a & b & j &
\\ \cline{1-3}  a & b & j &\rl{30}
\\ \cline{1-3} 
\end{tabular}

\end{document}
1
  • This answer doesn't meet the condition no. 1. The reason I posted this question is because I want to find a way to set the padding for the cells of an specific row without affecting the cells from the other rows.
    – gfe
    Jun 16, 2020 at 21:44
3

In the environment {NiceTabular} of nicematrix, you have two keys cell-space-top-limit and cell-space-bottom-limit and a key cell-space-limits for their conjonction.

With the latest version of nicematrix (v. 5.18 of 2021-07-15), it's possible to fix the value of those parameters for a single row with the command \RowStyle.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\newcolumntype{M}{>{\raggedright\arraybackslash}m{2.2cm}}

\begin{NiceTabular}{MMM}[hvlines,cell-space-limits=1mm]
some text & here also some text & text, text, text and text \\
some text & here also some text & text, text, text and text \\
\RowStyle[cell-space-limits=0pt]{}
some text & here also some text & text, text, text and text \\
some text & here also some text & text, text, text and text \\
\end{NiceTabular}

\end{document}

The package nicematrix also provides a key hvlines which draw all the rules.

Output of the above code

0

Both conditions are met with this approach

\documentclass{article}

\usepackage{tabu, stackengine}

\usepackage{lipsum}

\begin{document}

\begin{tabu}{|m{80pt}|m{80pt}|m{80pt}|}
\hline

\addstackgap[80pt]{a}
& b
& \lipsum[1][1-2]
\\ \hline

A
& B
& C
\\ \hline

\end{tabu}

\end{document}

enter image description here

0

I wouldn't use arraystretch to have vertically centered cells contents, as it asymmetrically expands the height and depth of cells. A better way uses package cellspace, which defines a minimal vertical padding at the top and bottom of cells in columns with specifier prefixed with the letter S (or C if you load siunitx).

Here is demo, with \cellspacetop-bottomlimit chosen so that the solution with arraystretch and the solution with cellspace result in tables of the same height:

\documentclass{article}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{8.5pt}
\setlength{\cellspacebottomlimit}{8.5pt}

\begin{document}

{\def\arraystretch{2}

\noindent
\begin{tabular}{|c|c|c|}
   \hline a & b & c
\\ \hline a & b & c
\\ \hline a & b & c
\\ \hline
\end{tabular}}
\qquad
\begin{tabular}{|Sc|Sc|Sc|}
   \hline a & b & c
\\ \hline a & b & c
\\ \hline a & b & c
\\ \hline
\end{tabular}

\end{document} 

enter image description here

5
  • Hm, it seems that in answer you not load your solution (the code for the right table on image)
    – Zarko
    Jun 16, 2020 at 14:41
  • @Zarko: I did, but there may be an optical effect due to b having an ascender, in the middle column.
    – Bernard
    Jun 16, 2020 at 14:55
  • Sorry to bother you, but your MWE doesn't reproduce showed image. In it is missing code for the right table (a solution for use cellspace package. Or I not understand your answer :-(
    – Zarko
    Jun 16, 2020 at 14:59
  • @Zarko: Oh! A copy-paste accident. I misunderstood what you meant. I should have checked. Thank you!
    – Bernard
    Jun 16, 2020 at 16:09
  • 2
    This answer doesn't meet the condition no. 1. The reason I posted this question is because I want to find a way to set the padding for the cells of an specific row without affecting the cells from the other rows.
    – gfe
    Jun 16, 2020 at 21:43

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.