How to vertically-center the text of the cells?
One easy way to this would be to use the array
package, specifying your column width with m{...}
. For example:
\begin{tabular}{ m{4cm} m{1cm} }
... & ... \\end{tabular}
will give you a four centimeter-long column and a one centimeter-long column. In each cell, the contents will be vertically aligned to the center. Note, however, that the cell contents will be horizontally aligned left. If you also want to align all the cell contents toward the center in a horizontal sense, then you could do something like this:
\begin{tabular}{ >{\centering\arraybackslash} m{4cm} >{\centering\arraybackslash} m{4cm} }
... & ... \\end{tabular}
The point of \arraybackslash
is to return \\
to its original meaning because the \centering
command alters this and could possibly give you a noalign
error during compilation.
If you have several columns and do not want your source to look cluttered, you could define new columns before your tabular environment, for example:
\newcolumntype{C}{ >{\centering\arraybackslash} m{4cm} }
\newcolumntype{D}{ >{\centering\arraybackslash} m{1cm} }
\begin{tabular}{ C D }
... & ... \\end{tabular}
There is a lot of useful information on tables in the wiki LaTeX guide, if you want to explore this further.
\documentclass{article}
\usepackage[a4paper,vmargin=2cm,hmargin=1cm,showframe]{geometry}
\usepackage[demo]{graphicx}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{longtable}
\parindent=0pt
\def\correction#1{%
\abovedisplayshortskip=#1\baselineskip\relax\belowdisplayshortskip=#1\baselineskip\relax%
\abovedisplayskip=#1\baselineskip\relax\belowdisplayskip=#1\baselineskip\relax}
\arrayrulewidth=1pt\relax
\tabcolsep=5pt\relax
\arrayrulecolor{red}
\fboxsep=\tabcolsep\relax
\fboxrule=\arrayrulewidth\relax
\newcolumntype{A}[2]{%
>{\minipage{\dimexpr#1\linewidth-2\tabcolsep-#2\arrayrulewidth\relax}\vspace\tabcolsep}%
c<{\vspace\tabcolsep\endminipage}}
\newenvironment{Table}[4]{%
\longtable{%
|A{#1}{1.5}% for figure
|>{\centering$\displaystyle}A{#2}{1}<{$}% for inline equation
|>{\correction{-1}\strut\[}A{#3}{1}<{\]\strut}% for displayed equation
|>{\centering}A{#4}{1.5}% for text
|}\hline\ignorespaces}{%
\endlongtable\ignorespacesafterend}
\newcommand{\dummy}{%
It is practically a big lie that \LaTeX\
makes you focus on the content without
bothering about the layout.}
\newcommand{\Row}{%
\includegraphics[width=\linewidth]{newton}&
\frac{a+b}{a-b}=0&
\int_a^b f(x)\, \textrm{d}x=\frac{b-a}{b+a}&
\fcolorbox{cyan}{yellow}{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}{\dummy}}
\tabularnewline\hline}
\begin{document}
\begin{Table}{0.25}{0.25}{0.25}{0.25}
\Row
\Row
\end{Table}
\def\x{\centering$\displaystyle\int_a^bf(x)\,\textrm{d}x=\frac{a-b}{a+b}$}
\longtable{|A{0.2}{1.5}*2{|A{0.25}{1}}|A{0.3}{1.5}|}\hline
\x & \x & \multicolumn{2}{A{0.55}{1.5}|}{\x} \tabularnewline\hline
\multicolumn{2}{|A{0.45}{1.5}|}{\x} & \x & \x\tabularnewline\hline
\x & \multicolumn{2}{A{0.5}{1}|}{\x} & \x\tabularnewline\hline
\multicolumn{4}{|A{1}{2}|}{\x}\tabularnewline\hline
\endlongtable
\end{document}
Putting a tabular in the cell of a tabular centers the content of the cell horizontally and vertically.
\begin{tabular}{|l|c|c|}
\hline
\begin{tabular}{l}
text in cell 1
\end{tabular} &
\begin{tabular}{l}
first line of text in cell 2 \\
second line of text in cell 2
\end{tabular} &
\begin{tabular}{l}
first line of text in cell 3 \\
second line of text in cell 3 \\
third line of text in cell 3 \\
\end{tabular}
\\
\hline
\begin{tabular}{l}
first line of text in cell 4 \\
second line of text in cell 4
\end{tabular} &
\begin{tabular}{l}
first line of text in cell 5 \\
second line of text in cell 5 \\
third line of text in cell 5 \\
\end{tabular} &
\begin{tabular}{l}
first line of text in cell 6 \\
second line of text in cell 6 \\
\end{tabular} \\
\hline
\end{tabular}
gives:
so you can define a macro centered
\newcommand{\centered}[1]{\begin{tabular}{l} #1 \end{tabular}}
and use it like this:
%\begin{tabular}{|l|c|c|} <- Old version of this answer
\begin{tabular}{|@{}l@{}|@{}c@{}|@{}c@{}|}
% This update now avoids double indentations and allows hlines
\hline
\centered{ text in cell 1 } &
\centered{
first line of text in cell 2 \\
second line of text in cell 2} &
\centered{
first line of text in cell 3 \\
second line of text in cell 3 \\
third line of text in cell 3 \\ } \\
\hline
\centered{
first line of text in cell 4 \\
second line of text in cell 4 } &
\centered{
first line of text in cell 5 \\
second line of text in cell 5 \\
third line of text in cell 5 \\ } &
\centered{
first line of text in cell 6 \\
second line of text in cell 6 \\ } \\
\hline
\end{tabular}