Graphics, Figures & TablesSimultaneous alignment in cell of table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Al.x
Posts: 10
Joined: Tue Oct 27, 2009 3:53 pm

Simultaneous alignment in cell of table

Post by Al.x »

Hi guys,

I've been making quite a few tables these past few days. Been using the array-package to allow vertical alignment in the tabular-environment, typically;

\begin{tabular}{b{1.5cm}b{1.5cm}b{1.5cm}b{1.5cm}b{1.5cm}}
... bunch of stuff
\end{tabular}

But how on earth does one handle horizontal alignment simultaneously?

Alex

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Simultaneous alignment in cell of table

Post by frabjous »

I typically use the > and < to put declarations at the beginning or end of every entry in a given column. (See p. 2 of the array package's documentation.)

Perhaps this will give you the basic idea.

E.g.:

Code: Select all

\begin{tabular}{|>{\raggedright}b{1.5cm}|>{\centering}b{1.5cm}|>{\flushright}b{1.5cm}|b{1.5cm}|}
\hline
left & center & right & here is a bunch of text that I want full justification for.\\
\hline
\end{tabular}
Al.x
Posts: 10
Joined: Tue Oct 27, 2009 3:53 pm

Simultaneous alignment in cell of table

Post by Al.x »

Awesome :-)

So, I've tried some of that, and received an error.

Code: Select all

	
\begin{tabular}{>{\centering}b{2cm}>{\centering}b{2cm}>{\centering}b{\cellWidth}>{\centering}b{\cellWidth}>{\centering}b{\cellWidth}>{\centering}b{\cellWidth}>{\centering}b{\cellWidth}>{\centering}b{\cellWidth}>{\centering}b{\cellWidth}>{\centering}b{\cellWidth}}
Series & Number of observations & Mean & Median & Minimum & Maximum & Standard deviation & Skewness & Kurtosis \\ \hline
	\end{tabular}
This gave me:
! Misplaced \noalign.
\hline ->\noalign
{\ifnum 0=`}\fi \hrule \@height \arrayrulewidth \futurelet...
l.271 ...deviation & Skewness & Kurtosis \\ \hline

Also, I get a lot of overfull \hbox

PS. \cellWidth is just a variable with a width (and yeah, I tested entering the width directly).

Anyone have a clue?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Simultaneous alignment in cell of table

Post by gmedina »

Hi,

add \arraybackslash immediately after every \centering (or \raggedright or \raggedleft) command:

Code: Select all

\documentclass{book}
\usepackage{array}

\newcommand\cellWidth{2cm}

\begin{document}

\begin{tabular}%
  {*{2}{>{\centering\arraybackslash}b{2cm}}*{8}{>{\centering\arraybackslash}b{\cellWidth}}}
  Series & Number of observations & Mean & Median & Minimum & Maximum & Standard deviation & Skewness & Kurtosis \\ \hline
\end{tabular}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Simultaneous alignment in cell of table

Post by localghost »

Al.x wrote:Awesome. So, I've tried some of that, and received an error.[…]
The \arraybackslash command is only necessary for the last column to restore the \\ command for a new tabular line.


Best regards and welcome to the board
Thorsten
Al.x
Posts: 10
Joined: Tue Oct 27, 2009 3:53 pm

Simultaneous alignment in cell of table

Post by Al.x »

localghost wrote:
Al.x wrote:Awesome. So, I've tried some of that, and received an error.[…]
The \arraybackslash command is only necessary for the last column to restore the \\ command for a new tabular line.


Best regards and welcome to the board
Thorsten
gmedina wrote:Hi,

add \arraybackslash immediately after every \centering (or \raggedright or \raggedleft) command:

Code: Select all

\documentclass{book}
\usepackage{array}

\newcommand\cellWidth{2cm}

\begin{document}

\begin{tabular}%
  {*{2}{>{\centering\arraybackslash}b{2cm}}*{8}{>{\centering\arraybackslash}b{\cellWidth}}}
  Series & Number of observations & Mean & Median & Minimum & Maximum & Standard deviation & Skewness & Kurtosis \\ \hline
\end{tabular}

\end{document}
Thank you both! That worked perfectly Do any of you know what's going on in the background? Why does the \\ need to be restored? What is it exactly that \arraybackslash does?

Regards,
Alex
Last edited by Al.x on Sat Nov 13, 2010 1:21 am, edited 2 times in total.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Simultaneous alignment in cell of table

Post by localghost »

See the array manual (p. 22 bottom) for a brief explanation. You can access it via command line on your local machine.

Code: Select all

texdoc array
Post Reply