Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm new with LaTeX and I'm trying to make a table with the elements centered vertically and horizontally. I've searched for this question, it seems like most people were trying to do things a little more complicated than this, resulting in complicated answers I couldn't understand. The table below is already centered horizontally, but the elements are aligned to the top of the cells instead of the center. How do I fix this?

    \begin{tabular}{*{5}{|c}|}
        \hline
        $p$ & $q$ & $p \land q$ & $p \lor q$ & $p \to q$\\
        \hline
        T & T & T & T & T\\
        \hline
        T & F & F & T & F\\
        \hline
        F & T & F & T & T\\
        \hline
        F & F & F & F & T\\
        \hline
    \end{tabular}
share|improve this question
    
I don't think there's an easy solution to align the rows to the centre. To see why this is usually not what you want, observe that in most tables you want to align the text in the rows by the base line. –  Marc van Dongen Jan 15 '12 at 21:20
    
@MarcvanDongen That type of alignment would be better than aligning to the top which is what's happening with this table. If that's simple, I'll take that for an answer. –  gsingh2011 Jan 15 '12 at 21:22
    
I think the horizontal \hline commands are the cause of the misconception about the alignment. I'm posting a non-solution further on that shows the rows are aligned to the base line. Please note that I've removed the vertical lines in the tabular environment because the booktabs package doesn't like them. Personally, I'd remove all the \midrule commands as well, except for the first. –  Marc van Dongen Jan 15 '12 at 21:27
1  
You should take a look at the array package and all comments in this tex.stackexchange.com/questions/40581/… Thread, i think they might help you. –  Ronny Jan 15 '12 at 22:06
    
Have a look at label in subcaption and customize cell size of an array in particular, the bit about mycell –  cmhughes Jan 15 '12 at 22:34
add comment

1 Answer

up vote 4 down vote accepted

The vertical skip from baseline to baseline within regular text is given by \baselineskip. Within tabular, this is 0pt but it is still accessible as \normalbaselineskip. Using your example as reference, it is clear to see that the vertical baseline skip still holds when drawing a vertical 1pt rule of height \normalbaselineskip:

enter image description here

\documentclass{article}
\begin{document}
\begin{tabular}{*{5}{|c}|}
  \hline
  $p$ & $q$ & $p \land q$ & $p \lor q$ & $p \to q$\\
  \hline
  T & T & T\smash{\rule{1pt}{\normalbaselineskip}} & T & T\\
  \hline
  T & F & F & T & F\\
  \hline
  F & T & $f$ & T & T\\
  \hline
  F & F & F & F & T\\
  \hline
\end{tabular}
\end{document}

If you have a very particular table, like the one in your example, where baseline alignment may seem to be inadequate, you could play around with the addition of a vertical "strut" like in the following example:

enter image description here

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\begin{document}
\newlength{\mylen}\settowidth{\mylen}{$p \to q$}% Widest element
\begin{tabular}{*{5}{|>{\centering\arraybackslash\rule{0pt}{1.05em}}m{\mylen}}|}
  \hline
  $p$ & $q$ & $p \land q$ & $p \lor q$ & $p \to q$\\ 
  \hline
  T & T & T & T & T\\
  \hline
  T & F & F & T & F\\
  \hline
  F & T & F & T & T\\
  \hline
  F & F & F & F & T\\
  \hline
\end{tabular}
\end{document}

Using the array package, it allows you to insert <stuff> before every table column entry using >{<stuff>}. Using the m{<width>} column specification, I've fixed the column widths (I think it looks cleaner that way) and reverted back to the centering supplied by c-columns.

Although this may be a moot point based on personal preference, consider using the booktabs package to typeset tables:

enter image description here

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\begin{document}
\newlength{\mylen}\settowidth{\mylen}{$p \to q$}% Widest element
\begin{tabular}{*{5}{>{\centering\arraybackslash}m{\mylen}}}
  \toprule
  $p$ & $q$ & $p \land q$ & $p \lor q$ & $p \to q$ \\ 
  \midrule
  T & T & T & T & T \\
  T & F & F & T & F \\
  F & T & F & T & T \\
  F & F & F & F & T \\
  \bottomrule
\end{tabular}
\end{document}
share|improve this answer
    
Thanks, this is the result I was looking for. However, I don't understand what \arraybackslash does. Could you explain that? –  gsingh2011 Jan 16 '12 at 0:44
2  
@gsingh2011: The array package allows for column specifications to include > and < for inclusion of stuff before/after a column entry. However, in doing so, it also modifies the traditional \` command for proceeding to a new line. \arraybackslash` restores this traditional use. –  Werner Jan 16 '12 at 1:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.