[Tex/LaTex] Centering vertically in table cell

tablesvertical alignment

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}

Best Answer

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}