0

As can be seen in my MWE below, I'm using tabularx to create a table with alternating colors. For different reasons, I defined three new column types L, C and R that shall automatically fill out the whole \textwidth and are left, center and right-aligned.

However, this MWE constantly fails and I can't figure out a way to get it to work and also don't know what to make of the error messages:

  • Misplaced \noalign. \end{tabularx}
  • Extra alignment tab has been changed to \cr. \end{tabularx}
\documentclass[11pt, oneside, a4paper]{article}
\usepackage{fontspec}
    \setmainfont[BoldFont=Calibri, ItalicFont=CalibriLightItalic]{Calibri Light}
% captions
\usepackage[font={color=red, small}, justification=justified, singlelinecheck=false, tableposition=top]{caption}
% colors
\usepackage[table]{xcolor}
\usepackage{colortbl}
% tables
\usepackage{float}
    \floatstyle{plaintop}
    \restylefloat{table}
\usepackage{xltabular}
\usepackage{tabularx}
    \renewcommand{\tabularxcolumn}[1]{m{#1}}
\usepackage{array}
    \newcolumntype{L}{>{\raggedright}X}
    \newcolumntype{R}{>{\raggedleft}X}
    \newcolumntype{C}{>{\centering}X}
    \renewcommand{\arraystretch}{0.75}
    \setlength\arrayrulewidth{1pt}
    \setlength\tabcolsep{0pt}

\begin{document}
\begin{table}[H]
    \rowcolors{1}{white}{gray!50}
    \color{red}{
        \begin{tabularx}{\textwidth}{C C C}
            \arrayrulecolor{gray} 
            \hline
            \textbf{Band No.} & \textbf{Spectral Range} & \textbf{Resolution} \\
            \hline
            1 & 0.435 & 30 \\
            2 & 0.452 & 30 \\
            \hline
        \end{tabularx}
    }
    \caption{Sensor specifications Landsat 8}
    \label{tab:specs_landsat8}
\end{table}
\end{document}

I can get it to work using column definitions like this:

\begin{tabularx}{\textwidth}{
    >{\arraybackslash}p{0.35\textwidth}
    >{\raggedright\arraybackslash}p{0.3\textwidth} 
    >{\arraybackslash}p{0.35\textwidth}
    }
...
3
  • 2
    Not quite understood. Besides, you should add \arraybackslash to your definitions of L, C and R. Plus you really shouldn't be using the [H] specifier.
    – daleif
    Commented Feb 18, 2021 at 13:52
  • Using \arraybackslash in the definition solves the whole issue! Whatever this is used for (I have no clue), it works. Great, thanks!
    – s6hebern
    Commented Feb 18, 2021 at 13:58
  • See also: tex.stackexchange.com/a/12712/134144
    – leandriis
    Commented Feb 18, 2021 at 14:11

1 Answer 1

1

\raggedright and friends redefines \\ so it no longer means new row, \arraybackslash restores the tabular definition of \\. Thus it is better t use

\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}

Technically, you only need the redefinition for the right most column, but it doesn't hurt adding it to all.

1
  • This solves so many issues I always had with tabularx at once, fantastic!
    – s6hebern
    Commented Feb 18, 2021 at 14:35

You must log in to answer this question.

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