79

I want to create a LaTeX table with three rows which have different row heights. How can I do this?

The following code

\renewcommand\arraystretch{2.4} \setlength\minrowclearance{2.4pt}

seems make each row has the same height.

Display Name
  • 46,933
eva
  • 1,071

4 Answers4

85

Try this:

\documentclass{article}
\begin{document}
\begin{tabular}{cc}
\hline
hi & tall one\\[10ex]
\hline
hi & medium one\\[5ex]
\hline
hi & standard one\\
\hline
\end{tabular}
\end{document}

So you can specify the height of a row with the optional argument

David Carlisle
  • 757,742
Spike
  • 6,729
  • 29
    how do you vertically centralize the elements in a cell, using the [ex] optional argument?! – Ivan Machado Nov 21 '12 at 22:57
  • 12
    Using \\[<space>] adds space to the end of the last cell, so if the last cell of each row has significantly different contents, then the row height can vary. This is easily solved by adding an extra column, and keeping it empty. Then \\[<space>] always makes the row <space> as a minimum. – Nathanael Farley Dec 17 '12 at 11:03
  • 6
    @NathanaelFarley This won't help when you have hlines as then adding an additional column will produce extra lines at the end. Additionally, it still keeps the contents vertically spaced near the top rather than within the cell in the center vertically – Aram Papazian Aug 15 '14 at 13:07
  • 3
    @AramPapazian This can be fixed by using columns like {cc@{}c@{}} as the @{} will make the space either side of the empty column 0 pt. – Nathanael Farley Aug 18 '14 at 09:12
  • @NathanaelFarley you wrote "Then \\[<space>] always makes the row <space> as a minimum." but this is not what happens. If I replace your tabular with the following, \begin{tabular}{cc} 1 & \\[0mm] 2 & \\[5mm] \end{tabular} the first row is 4.36mm high while the second measures 9.36mm. The 5mm goes into some equation but it's not the minimum height when leaving the last column blank. – Bastian Jun 16 '21 at 14:56
12

As none of the current answers address the issue of how to center the content vertically, I here introduce \xrowht[<footprint>]{<length>} to add an extra .5<length> of height above and below a \vphantom of a <footprint>. The default footprint takes the height of a 0 character, which is great if the data has no descenders. For data with descenders, an optionally specified footprint of () makes more sense.

\xrowht can be added to any cell in the row. Here, I add it to the 1st column cells, for convenience.

In the MWE, the 1st row is unmodified, then for successive rows, 10pt, 20pt, 30pt, and 40pt are symmetrically added. I also use an optional footprint in the 2nd row, which contains descenders.

\documentclass{article}
\usepackage{stackengine}
\newcommand\xrowht[2][0]{\addstackgap[.5\dimexpr#2\relax]{\vphantom{#1}}}
\begin{document}
    \begin{table}[h]
      \begin{tabular}{|c|l|}
        \hline
         col1 & col2 \\
        \hline\xrowht[()]{10pt}
        1 & 2g \\
        \hline\xrowht{20pt}
        3 & 4 \\
        \hline\xrowht{30pt}
        5 & 6 \\
        \hline\xrowht{40pt}
        7 & 8 \\
        \hline
      \end{tabular}
    \end{table}
\end{document}

enter image description here

  • How can I extend the first row? Just natively adding \xrowht{20pt} afterr the first \hline results in \xrowht{20pt}... – Theova Feb 27 '20 at 16:56
  • Sorry, copy paste error: ... it results in main.tex|56 error| Misplaced \noalign. main.tex|56 error| Misplaced \omit. – Theova Feb 27 '20 at 17:09
  • \documentclass{article} \usepackage{stackengine} \newcommand\xrowht[2][0]{\addstackgap[.5\dimexpr#2\relax]{\vphantom{#1}}} \begin{document} \begin{table}[h] \begin{tabular}{|c|l|} \hline\xrowht{20pt} col1 & col2 \\ \hline\xrowht[()]{10pt} 1 & 2g \\ \hline\xrowht{20pt} 3 & 4 \\ \hline\xrowht{30pt} 5 & 6 \\ \hline\xrowht{40pt} 7 & 8 \\ \hline \end{tabular} \end{table} \end{document} works for me @Theova – Steven B. Segletes Feb 27 '20 at 17:18
  • 1
    Right, the problem is with including multicol: `\documentclass{article} \usepackage{stackengine} \usepackage{multicol} \newcommand\xrowht[2][0]{\addstackgap[.5\dimexpr#2\relax]{\vphantom{#1}}}

    \begin{document} \begin{table}[h] \begin{tabular}{|c|l|} \xrowht{20pt} \multicolumn{2}{|c|}{ multicol } \ \hline\xrowht{20pt} col1 & col2 \ \hline\xrowht[()]{10pt} 1 & 2g \ \hline\xrowht{20pt} 3 & 4 \ \hline\xrowht{30pt} 5 & 6 \ \hline\xrowht{40pt} 7 & 8 \ \hline \end{tabular} \end{table} \end{document} `

    – Theova Feb 27 '20 at 17:37
  • 1
    @Theova Move the \xrowht macrow inside the \multicolumn, such as \multicolumn{2}{|c|}{ \xrowht{20pt} multicol } \\ – Steven B. Segletes Feb 27 '20 at 17:41
  • 1
    @ Steven: Thank you very much. This works as desired :-) – Theova Feb 29 '20 at 15:46
  • What's the purpose of "[()]" right next to those 10pt?? – Cristian Baeza Jul 17 '20 at 02:15
  • @CristianBaeza The [] specify, in LaTeX grammar, an optional argument to a macro, in this case \xrowht. The actual value of that optional argument is (). In this macro, the optional argument is added as a \vphantom to the table cell. That is to say, it becomes part of the vertical footprint inside the cell. I just happen to know that all font letters will always fit within the vertical footprint of (). If you remove it from the 1st row, you will see the cell shorten at the bottom. Because the row data contains a descending character g, the vertical balance would be thrown off – Steven B. Segletes Jul 17 '20 at 09:11
  • The command is very useful, but how can I automatically avoid that annoying spacing generated right before the elements in the first column? I've noticed that numbers 1,3,5,7 are not horizontally centered in the generated table and I'm having the same problem in my document, any ideas? – FacuO.Z. Jun 27 '22 at 05:26
  • This only vertically centers the first column for me... – Vinz Jul 13 '22 at 19:13
  • @Vinz Is your column content simple text on a single row, as in my example, or does your content naturally contain more complex vertical components, such as super/sub-scripting and or multi-row content? – Steven B. Segletes Jul 13 '22 at 20:04
4

It is easy peasy to set row heights and get perfect middle alignment for multirow cells with new LaTeX3 package tabularray:

\documentclass{article}

\usepackage{tabularray} \NewColumnType{M}[1]{Q[m,c,#1]}

\begin{document}

\begin{tblr}{colspec={M{3cm}M{3.5cm}M{3.5cm}},row{2-4}={7ex}} \hline Alpha & Beta & Gamma \ \hline \SetCell[r=3]{c} Text & Text & Text \ \hline & \SetCell[r=2]{c} Text Text Text Text Text Text Text Text & Text \ \hline & & Text Text Text Text Text Text Text Text \ \hline
\end{tblr}

\end{document}

enter image description here

L.J.R.
  • 10,932
2

Code for a latex table with adjustable row height. (Credits to previous posts)

    \begin{table}[h]
      \begin{tabular}{|c|l|}
        \hline
         col1 & col2 \\%[2ex]
        \hline
        & \\[-2ex]
        1 & 2 \\[0.75ex]
        \hline
        & \\[-2ex]
        3 & 4 \\[2ex]
        \hline
        & \\[2ex]
        5 & 6 \\[3ex]
        \hline
        & \\
        7 & 8 \\
        \hline
      \end{tabular}
    \end{table}

Your table will look like this.

vineeshvs
  • 121