How to add spoiler tags, edit posts, add images etc. How to - a user's guide to the new version of Boards
Mods please check the Moderators Group for an important update on Mod tools. If you do not have access to the group, please PM Niamh. Thanks!

# Latex Table Problem

• Registered Users Posts: 5,695 ✭✭✭

Hi folks. I could do with a bit of help. I have the following code for a table. What I want is for the text in the cells to be centred vertically as well as horizontally. Any help would be appreciated!

\begin{document}
\begin{center}
{\footnotesize
\begin{tabular}{ | p{2cm} | c | c | c | c | c | c |}
\hline
Time in days from previous earthquake & $0\leq x< 100$ & $100\leq x< 200$ & $200\leq x< 300$ & $300\leq x< 400$ & $400\leq x< 500$ \\ \hline
Number of earthquakes & 31 & 24 & 12 & 14 & 8 \\ \hline
\end{tabular}
}
\end{center}
\end{document}

• The default c or 'center' command should do that already for you. Is there any particular reason to use the p(2cm)? I'd just use |c| all the way let LaTeX decide the spacing distances it requires.

hth

• I need the P{2cm} to make latex wrap the text in the first column. It will push the table off the page if I don't.

The c centres the text horizontally for me but I don't know how to centre it vertically too. That's my problem.

• Ack! My bad. Should have seen that. Apologies.

Well such is the excitement of my saturday night I spent the last hour tweaking and messing with the tabular environment and I (sorta) stumbled upon a solution.

\begin{tabular}{ | p{2.5cm} | c | c | c | c | c | c |}
\hline
Time in days &&&&&\\
from previous
& $0\leq x< 100$ & $100\leq x< 200$ & $200\leq x< 300$ & $300\leq x< 400$ & $400\leq x< 500$ \\
earthquake &&&&&\\
\hline

In so far as I can tell without changing package there's no other solution. Changing package and the best option seems tabu, but reading up on the documentation implies it's no longer supported so you may end up writing a document only to find a few months down the road it doesn't render correctly.
Failing that there was tabulary which kind of did what you want but the inequalities looked really ugly. Fugly actually.

Most aesthetic solution I got is above but I may return to this in a wee while.

• Thanks for your efforts. In the end I scoured the internet for some existing code to rob!

This seems to work perfectly although there is a lot of stuff in the code that I am not familiar with.

\begin{table}[h]
\begin{center}
{\footnotesize
\begin{tabular}{| >{\centering\arraybackslash}m{0.8in} | >{\centering\arraybackslash}m{0.8in} | >{\centering\arraybackslash}m{0.9in} | >{\centering\arraybackslash}m{0.9in} |>{\centering\arraybackslash}m{0.9in} |>{\centering\arraybackslash}m{0.9in} |}\hline
Time in days from previous earthquake & $0\leq x< 100$ & $100\leq x< 200$ & $200\leq x< 300$ & $300\leq x< 400$ & $400\leq x< 500$\\[0ex] \hline
Number of earthquakes & 31 & 24 & 12 & 14 & 8 \\[0ex] \hline
\end{tabular}
}
\end{center}
\end{table}