29

I have something like this:

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{tabular}{| c | C{3cm} | C{4cm} |}
Short Text & Short Text & Short Text \\ \hline
Short Text & Short Text & Loooooooooooooooooooooong Text \\ \hline 
\end{tabular}

which would make the second row have a wider height. Then how do I specify a fixed height for all rows in my table?

lockstep
  • 250,273
Covi
  • 7,055

3 Answers3

19

If you want to have the same height in each row, try one of these two methods:

  1. Using a \vbox for every cell:

    \documentclass[a4paper]{article}
    \usepackage{array}
    

    \begin{document}

    \newcolumntype{C}[1]{% >{\vbox to 5ex\bgroup\vfill\centering}% p{#1}% <{\egroup}}

    \begin{tabular}{|c | C{3cm} | C{5cm} |} \hline Short Text & Short Text & Short Text \tabularnewline \hline Short Text & Short Text & Loooooooooo oooooooooooong Text \tabularnewline \hline
    Short Text & Short Text & Loooooooooooooooooooooong Text \tabularnewline \hline \end{tabular}
    \end{document}

    enter image description here

    Interesting links about tables: TableTricks.pdf, and Tables on Wikibooks

  2. A new approach that is perhaps more easy to modify is:

    \documentclass[a4paper]{article}
    \usepackage{array}
    

    \begin{document} \def\mystrut(#1,#2){\vrule height #1 depth #2 width 0pt}

    \newcolumntype{C}[1]{% >{\mystrut(3ex,2ex)\centering}% p{#1}% <{}}

    \begin{tabular}{|c | C{3cm} | C{4cm} |} \hline Short Text & Short Text & Short Text \tabularnewline \hline Short Text & Short Text & Loooooooooooooooooong Text \tabularnewline \hline
    \end{tabular}
    \end{document}

    In this solution, it's easier to place the text. You only need to modify height and depth for the strut.

Alain Matthes
  • 95,075
  • @Covi: now the text are centered in the last columns – Alain Matthes Mar 06 '11 at 10:13
  • @Covi: you need to adapt the height by hand. I put 5ex but you can change this height. Other ways are to use Arraystretch or extrarowheight or Bigstruts with the package bigstrut but in each case, some specifications are needed to get exactly what you want. – Alain Matthes Mar 06 '11 at 10:24
  • @Altermundus Thanks for your method! This method only aligns text at horizontally middle of each cell, and sadly I'm very new to LaTeX, I can't do modifications so that it works perfectly. – Covi Mar 06 '11 at 10:52
  • 1
    @Covi: ok but what you want ? you need to center the text vertically too ? – Alain Matthes Mar 06 '11 at 10:56
  • @Altermundus Yes! :) Could you point out how to do that? – Covi Mar 06 '11 at 10:59
  • @Covi: Ok, I changed my answer – Alain Matthes Mar 06 '11 at 11:07
  • @Covi: I only add \vfill before \centering but my answer is tricky. Perhaps there is something better with the use of package. – Alain Matthes Mar 06 '11 at 11:28
  • This is a good answer to the problem. It might make the required parts more clear to remove the unnecessary pieces (such as the fontenc and inputenc packages and the \pagestyle). – TH. Mar 06 '11 at 12:02
  • @Covi: I added a new method, perhaps more easy to use. You place the text with the two parameters of \mystrut – Alain Matthes Mar 06 '11 at 22:36
  • @AlainMatthes great answer, there's just one thing in your code i don't understand: \newcolumntype{C}[1]{%

    {\vbox to 5ex\bgroup\vfill\centering}%

    p{#1}% <{\egroup}} -- is this a global command, will it affect every table in the document, or just a local thing?

    – essay May 19 '14 at 11:23
  • @AlainMatthes Your second solution doesn't work for me. I copied and pasted your second solution and compiled with latexmk -pdf and resulted rows have different heights. – patryk.beza Sep 20 '17 at 17:12
  • @AlainMatthes What's more, your solution doesn't center multi line text vertically. – patryk.beza Sep 20 '17 at 18:45
10

This is one of the places where I find ConTeXt's key value interface much more intuitive

\setupTABLE[c][2][width=3cm]
\setupTABLE[c][3][width=5cm]

\setupTABLE[each][each][height=3\lineheight,align={middle,middle},frame=on]

\starttext

\startTABLE
  \NC Short text \NC Short text \NC Short Text \NC \NR
  \NC Short text \NC Short text \NC Loooooooooo oooooooong teeeeeeeeeeeeeext \NC \NR
  \NC Short text \NC Short text \NC Loooooooooo oooooooong teeeeeeeeeeeeeext \NC \NR
\stopTABLE

\stoptext

enter image description here

I wish there were a LaTeX package that implemented such an interface for tables.

Aditya
  • 62,301
7

I would rather do it slightly different using a \vphantom, it requires a little more manual work, but is easier to understand.

The reason why you get different size columns is the column specifier m and what it does. This particular specifier is actually a parbox. Obviously, since your text is longer than the width of the parbox it will overflow or get wrapped to the following line. As TeX's hyphenation patterns do not have a pattern for ooooooooooooo it just overflows out of the table, but this is another story.

In order to have all the cells the same hight, I would use a strut or TeX's equivalent a \vphantom in this case. A phantom command will create an invisible box with width zero but the height of the enclosing text. We do this with the command:

\def\Z{\vphantom{\parbox[c]{1cm}{\Huge Something Long}}}

This strut like in manual typesetting will make all the rows equal. The final code is as follows:

\documentclass[11pt]{article} 
\usepackage{array}
\begin{document}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\def\Z{\vphantom{\parbox[c]{1cm}{\Huge Something Long}}}
\bigskip
\begin{tabular}{| c | C{3cm} | C{4cm} |}
\hline
\Z Short Text & Short Text & Short Text \\ \hline
\Z Short Text & Short Text & Loooooooooooooooothetheme Text \\ \hline 
\end{tabular}
\end{document}

If you have long repetitive tables of the same type, it might make it worthline to define a command addRow that can automate the addition of the strut.

By changing the parameter in the vphantom parbox to any of t,b,c, you can also get alignments at bottom, center or top.

As a sideline I changed Looo... to a word having an ending with a known hyphenation pattern and this time the overflow of text in the right margin also disappeared.

yannisl
  • 117,160
  • I'm not sure that \bigskipis necessary. A possibility is to replace in your code \Z by \mystrutfrom my last solution. – Alain Matthes Mar 06 '11 at 22:33
  • @Altermundus yes the bigskip is unecessary, I had some other stuff on top and forgot it in. The reason for making a strut with the \parbox, was to make it easier for the OP to change layout by using the [c] or [t] etc. – yannisl Mar 07 '11 at 04:06