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?

  • Use \multicolumn{1}{|c|}{a} instead of a, \multicolumn{1}{c|}{b} instead of b to center them. – ZhiyuanLck Jun 16 at 14:38
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}
| improve this answer | |
  • 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 at 21:44
2

In the environment {NiceTabular} of nicematrix, you have two keys cell-space-top-limit and cell-space-bottom-limit.

You can set these keys with a command \NiceMatrixOptions. These options are set locally. However (even if it is not elegant) you can switch to a global affectation by using the TeX primitive \globaldefs.

In the following example, I have defined a command cellspace which changes globally the value of the parameters cell-space-top-limit and cell-space-bottom-limit.

\documentclass{article}
\usepackage{nicematrix,xparse}

\NewDocumentCommand \cellspace { m }
  { 
    { 
      \globaldefs=1
      \NiceMatrixOptions{cell-space-top-limit=#1,cell-space-bottom-limit=#1} 
    } 
  }

\NiceMatrixOptions{cell-space-top-limit=1mm,cell-space-bottom-limit=1mm}

\begin{document}

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

\begin{NiceTabular}{MMM}[hvlines]
some text & here also some text & text, text, text and text \tabularnewline
some text & here also some text & text, text, text and text \tabularnewline
\cellspace{0pt}
some text & here also some text & text, text, text and text \tabularnewline
\cellspace{1mm}
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

| improve this answer | |
1

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

| improve this answer | |
  • Hm, it seems that in answer you not load your solution (the code for the right table on image) – Zarko Jun 16 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 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 at 14:59
  • @Zarko: Oh! A copy-paste accident. I misunderstood what you meant. I should have checked. Thank you! – Bernard Jun 16 at 16:09
  • 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 at 21:43
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

| improve this answer | |

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.