Graphics, Figures & Tablescolortbl, tabularx | Centering Error

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
k_lau
Posts: 3
Joined: Wed Oct 12, 2011 3:17 pm

colortbl, tabularx | Centering Error

Post by k_lau »

Hi,

I'm trying to combine a tabularx table with centered text using colortbl to colour the columns.
I wish to equally space the columns so I'm using the column type X for each column header.
With this I'm trying to using \centering with the \columncolor as follows:

Code: Select all

\newcolumntype{y}{>{\columncolor{LightGray}\centering}X}
But see the error:
! Misplaced \noalign.
\hline ->\noalign
{\ifnum 0=`}\fi \let \hskip \vskip \let \vrule \hrule \let...
l.62 \end{tabularx}
When I centre the columns or color them individually it works (see example code)

Many thanks for your assistance!

Kevin (Long time reader, first time poster)

--- Example code ---

Code: Select all

\documentclass[10pt,a4paper]{report}
%
\usepackage{color}
\definecolor{LightGray}{rgb}{0.83,0.83,0.83}
%
\usepackage{colortbl} 
\usepackage{tabularx}
%
\newcolumntype{z}{>{\columncolor{LightGray}}X}
\newcolumntype{y}{>{\columncolor{LightGray}\centering}X}
%
\begin{document}
%
\begin{table}[h!]
%
\centering
\caption{ABC}
%
\begin{tabularx}{1.0\columnwidth}{X X X}
A 	& B	& C 	\\ \hline
1	& P	& I	\\
2	& Q	& J	\\
3	& R	& K		
\end{tabularx}
%
\end{table}
%
\begin{table}[h!]
%
\centering
\caption{XYZ}
%
\begin{tabularx}{1.0\columnwidth}{z z z}
A 	& B	& C 	\\ \hline
1	& P	& I	\\
2	& Q	& J	\\
3	& R	& K		
\end{tabularx}
%
\end{table}
%
\begin{table}[h!]
%
\begin{tabularx}{1.0\columnwidth}{y y y}
A 	& B	& C 	\\ \hline
1	& P	& I	\\
2	& Q	& J	\\
3	& R	& K		
\end{tabularx}
%
\end{table}
%
%
\end{document}
Last edited by k_lau on Thu Oct 13, 2011 12:59 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

colortbl, tabularx | Centering Error

Post by localghost »

You have to restore the \\ command for a new table line with the \arraybackslash command at least for the last column.

Code: Select all

\newcolumntype{y}{>{\columncolor{LightGray}\centering\arraybackslash}X}
This is necessary as soon as you have e. g. justification commands like \centering in »X« column types.

As far as I remember this is documented in the array manual. This package is loaded by the colortbl package.

By the way, it is better to let xcolor load the colortbl package by its »table« option.

Code: Select all

\usepackage[table,dvipsnames]{xcolor}
The package manual has the details, especially regarding predefined colors by package options.

PS: Good example.


Best regards and welcome to the board
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
k_lau
Posts: 3
Joined: Wed Oct 12, 2011 3:17 pm

Re: colortbl, tabularx | Centering Error

Post by k_lau »

Many thanks! It works perfectly now.

Kevin
Post Reply