5

I'm trying to create a timetable using tabularx environment, but i can't find a way to center all columns.

Using this code:

\documentclass[11pt,oneside, landscape]{article} 

\usepackage[utf8]{inputenc} 

\usepackage{geometry}
\geometry{a4paper} 
\usepackage[osf]{mathpazo}
\linespread{1.05}\selectfont 

\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}  


\usepackage{booktabs}
\usepackage{array} 

\usepackage{tabularx}

\usepackage{fancyhdr}
\pagestyle{empty}

\newcommand{\tit}[1]{\fontsize{15}{17}\color{darkgray}\usefont{T1}{pplj}{b}{it}\selectfont #1}

\begin{document}

\begin{center}
\renewcommand{\arraystretch}{1.5}

\begin{tabularx}{19cm}{r >{\centering}X >{\centering}X >{\centering}X >{\centering}X X}
                   & \tit{Lun} &  \tit{Mar} &  \tit{Mer} & \tit{Gio} & \tit{Ven} \\
\toprule
\color{gray}9-11  $||$ &  & Laboratorio III &  &  Metodi mat. per la fisica & Laboratorio III \\ \midrule
\color{gray}11-13  $||$& Meccanica Quantistica & Metodi mat. per la fisica &  & Meccanica Quantistica & Fisica dell'informazione \\ \midrule
\midrule
\color{gray}15-17  $||$ &  & Fisica dell'informazione & Laboratorio III &  & \\
\bottomrule

\end{tabularx}

\end{center}

\end{document}

i get this

enter image description here

where last column is not centered. If i try to change

\begin{tabularx}{19cm}{r >{\centering}X >{\centering}X >{\centering}X >{\centering}X X}

with

\begin{tabularx}{19cm}{r >{\centering}X >{\centering}X >{\centering}X >{\centering}X {\centering}X}

i get

! Misplaced \noalign.
\toprule ->\noalign 
                    {\ifnum 0=`}\fi \@aboverulesep =\abovetopsep \global        \@b...
l.39 \end{tabularx}

2 Answers 2

7

Both the package arrayand the the package tabularxredefine \\ in a slightly different way. I refer to tabularx-can’t-center-some-column for a more in depth explanation.

When defining the last column, you have to put in an \arraybackslash after the \centering-command, i.e.:

>{\centering\arraybackslash}X

Your table preamble will be:

\begin{tabularx}{19cm}{r >{\centering}X >{\centering}X%
>{\centering}X >{\centering}X >{\centering\arraybackslash}X}

Or you can use tabu:

\documentclass[11pt,oneside, landscape]{article} 
\usepackage[utf8]{inputenc} 
\usepackage{geometry}
\geometry{a4paper} 
\usepackage[osf]{mathpazo}
\linespread{1.05}\selectfont 
\usepackage{graphicx,tabu}
\usepackage[dvipsnames]{xcolor}  
\usepackage{booktabs}
\usepackage{array} 
\usepackage{tabularx}
\usepackage{fancyhdr}
\pagestyle{empty}

\newcommand{\tit}[1]{\fontsize{15}{17}\color{darkgray}\usefont{T1}{pplj}{b}{it}\selectfont #1}

\begin{document}
\tabulinesep=4pt

\begin{tabu}to 19cm{r X[c] X[c] X[c] X[c] X[c]}
\rowfont{\tit} & Lun &  Mar &  Mer & Gio & Ven \strut\\
\toprule
\color{gray}9-11  $||$ &  & Laboratorio III &  &  Metodi mat. per la fisica & Laboratorio III \\ \midrule
\color{gray}11-13  $||$& Meccanica Quantistica & Metodi mat. per la fisica &  & Meccanica Quantistica & Fisica dell'informazione \\\midrule
\midrule
\color{gray}15-17  $||$ &  & Fisica dell'informazione & Laboratorio III &  & \\
\bottomrule
\end{tabu}
\end{document}

MWE with tabu:

enter image description here

1
  • An explanation of why \arraybackslash is needed would be interesting. The array documentation has \arraybackslash Restore \\ for use in array and tabular environment (after \raggedright etc.. Which is less than illuminating. Also, the \arraybakslash should be fixed. Dec 3, 2012 at 19:46
6

in preamble define new column type, for example:

\newcolumntype{C}{>{\centering\arraybackslash}X}

and then instead >{\centering}X and >{\centering\arraybackslash}X just use for column type C.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .