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?

3 Answers 3

19

I don't know what are the specifications you want but if you want to have the same height in each rows :

1)

\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 : firstly, a document can be found here TableTricks.pdf, interesting also is the wiki page Tables on wiki

2) A new approach 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 more easy to place the text. You need only to modify heightand depth for the strut.

12
  • @Covi: now the text are centered in the last columns Mar 6, 2011 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. Mar 6, 2011 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 6, 2011 at 10:52
  • 1
    @Covi: ok but what you want ? you need to center the text vertically too ? Mar 6, 2011 at 10:56
  • @Altermundus Yes! :) Could you point out how to do that?
    – Covi
    Mar 6, 2011 at 10:59
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.

3
  • I wish it, too.
    – lockstep
    Mar 7, 2011 at 0:10
  • My new LaTeX package tabularray implements key-value interfaces for tables.
    – L.J.R.
    Aug 2, 2021 at 3:38
  • @L.J.R: Great to know!
    – Aditya
    Aug 3, 2021 at 2:11
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.

2
  • I'm not sure that \bigskipis necessary. A possibility is to replace in your code \Z by \mystrutfrom my last solution. Mar 6, 2011 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. Mar 7, 2011 at 4:06

You must log in to answer this question.

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