3

I am trying to create the following table

enter image description here

However, I keep getting the following error message

! Misplaced \noalign.
\rowcolor ->\noalign 
                     {\ifnum 0=`}\fi \global \let \CT@do@color \CT@@do@color...
l.24       \tableHeaders{Figur}{Antall sirkler}

I expect to see \noalign only after the \cr of
an alignment. Proceed, and I'll ignore this case.

I tried adding {} around the arguments to make sure siunitx columns worked, however even that did not work. To summarize

  • How can I fix the white space between the multicolumns? I added \\[-0.02cm], but that feels like the worlds dirtiest trick.
  • How can I fix the \noalign error?
  • Is there a better way to center the n at the bottom of the table?

Code

\documentclass[a4paper, dvipsnames*,svgnames, table, 11pt]{article}

\usepackage{tabularx,siunitx}
\usepackage{multirow}

\usepackage{xcolor}
\definecolor{maincolorLight}{HTML}{838fbc}
\definecolor{maincolorMedium}{HTML}{425b9b}
\definecolor{maincolorDark}{HTML}{003d80}

\newcommand{\maincolor}{maincolorLight}
\newcommand{\Rowcolor}{\rowcolor{\maincolor}}

\newcommand{\tableHeaders}[2]{%
   \Rowcolor                        & \\[-0.02cm]
   {\Rowcolor \multirow{-2}{*}{#1}} & {\multirow{-2}{*}{#2}} \\
}

\begin{document}

\begin{table}[htbp]
    \centering
    \begin{tabular}{| S | S |}
      \tableHeaders{Figur}{Antall sirkler} 
          1  &  6 \\
          2  & 10 \\
          3  & 14 \\
          4  &    \\
          5  &    \\
         {n} &    \\ \hline
    \end{tabular}
    \caption{}
    \label{tab:del-1-oppgave-1.8}
\end{table}

\end{document}
N3buchadnezzar
  • 11,348
  • 7
  • 55
  • 114

4 Answers4

4

You're complicating your own life with multirow.

The braces are at the wrong place: no brace can precede \rowcolor, which doesn't make part of the next cell. The contents of the cell should be braced, because it's in an S column.

Be sure to always state the number of digits in an S column.

\documentclass[a4paper, dvipsnames*,svgnames, table, 11pt]{article}

\usepackage[table]{xcolor}
\usepackage{siunitx}

\usepackage{xcolor}
\definecolor{maincolorLight}{HTML}{838fbc}
\definecolor{maincolorMedium}{HTML}{425b9b}
\definecolor{maincolorDark}{HTML}{003d80}

\newcommand{\maincolor}{maincolorLight}
\newcommand{\Rowcolor}{\rowcolor{\maincolor}}
\newcommand{\headerstrut}{\vphantom{$\bigg|$}}

\newcommand{\tableHeaders}[2]{\Rowcolor {#1\headerstrut} & {#2} \\}

\begin{document}

\begin{tabular}{| S[table-format=1.0] | S[table-format=2.0] |}
  \tableHeaders{Figur}{Antall sirkler} 
  1    &  6 \\
  2    & 10 \\
  3    & 14 \\
  4    &    \\
  5    &    \\
 {$n$} &    \\ \hline
\end{tabular}

\end{document}

enter image description here

egreg
  • 1,121,712
2

If you use \cellcolor instead of \rowcolor you get the expected output:

enter image description here

\documentclass[a4paper, dvipsnames*,svgnames, table, 11pt]{article}

\usepackage{tabularx,siunitx}
\usepackage{multirow}

\usepackage{xcolor}
\definecolor{maincolorLight}{HTML}{838fbc}
\definecolor{maincolorMedium}{HTML}{425b9b}
\definecolor{maincolorDark}{HTML}{003d80}

\newcommand{\maincolor}{maincolorLight}
\newcommand{\Rowcolor}{\rowcolor{\maincolor}}

\newcommand{\tableHeaders}[2]{%
   \Rowcolor                        & \\
   {\multirow{-2}{*}{\cellcolor{\maincolor}#1}} & {\multirow{-2}{*}{\cellcolor{\maincolor}#2}} \\
}

\begin{document}

\begin{table}[htbp]
    \centering
    \begin{tabular}{| c | c |}
      \tableHeaders{Figur}{Antall sirkler}
          1  &  6 \\
          2  & 10 \\
          3  & 14 \\
          4  &    \\
          5  &    \\
         {n} &    \\ \hline
    \end{tabular}
    \caption{}
    \label{tab:del-1-oppgave-1.8}
\end{table}

\end{document}
leandriis
  • 62,593
2

The { before \Rowcolor makes TeX insert the alignment entry 〈pre〉-text and stop its search for \noalign material, which \rowcolor contains. The reason is that { is a non-space, non-expandable token (see here and here for more detailed explanations on TeX's behavior at the beginning or end of a tabular row). Removing this additional grouping level solves the problem, because then the \noalign used by \rowcolor is correctly found while TeX is expanding tokens looking for \noalign or \omit, before it starts to read the alignment entry 〈pre〉-text.

Note: what I call 〈pre〉-text corresponds to >{...} material declared in the tabular or array preamble when using the array package.

\documentclass[a4paper, dvipsnames*,svgnames, table, 11pt]{article}

\usepackage{tabularx,siunitx}
\usepackage{multirow}

\usepackage{xcolor}
\definecolor{maincolorLight}{HTML}{838fbc}
\definecolor{maincolorMedium}{HTML}{425b9b}
\definecolor{maincolorDark}{HTML}{003d80}

\newcommand{\maincolor}{maincolorLight}
\newcommand{\Rowcolor}{\rowcolor{\maincolor}}

\newcommand{\tableHeaders}[2]{%
   \Rowcolor                        & \\[-0.02cm]
   \Rowcolor \multirow{-2}{*}{#1} & {\multirow{-2}{*}{#2}} \\
}

\begin{document}

\begin{table}[htbp]
    \centering
    \begin{tabular}{| c | c |}
      \tableHeaders{Figur}{Antall sirkler}
          1  &  6 \\
          2  & 10 \\
          3  & 14 \\
          4  &    \\
          5  &    \\
         {n} &    \\ \hline
    \end{tabular}
    \caption{}
    \label{tab:del-1-oppgave-1.8}
\end{table}

\end{document}

Screenshot

frougon
  • 24,283
  • 1
  • 32
  • 55
1

And if you use cals, it is easy:

\documentclass[a4paper, 11pt]{article}
\usepackage{cals, xcolor}

\definecolor{maincolorLight}{HTML}{838fbc}
\definecolor{maincolorMedium}{HTML}{425b9b}
\definecolor{maincolorDark}{HTML}{003d80}


\let\nc=\nullcell                                                  % Shortcuts
\let\sc=\spancontent

\begin{document}

\begin{calstable}

% Defining column relativ to each other and relativ to the margins
\colwidths{{\dimexpr(\columnwidth)/18*2\relax}
            {\dimexpr(\columnwidth)/18*5\relax}
            }
% The tabular fills the text area

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.4pt}
\def\cals@bodyrs@width{0.4pt}
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0.4pt}             % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.4pt}
\def\cals@bgcolor{}

\def\light{\ifx\cals@bgcolor\empty
\def\cals@bgcolor{maincolorLight}
\else \def\cals@bgcolor{} \fi}

\def\tb{\ifx\cals@borderT\relax     % Top border switch (off-on)
    \def\cals@borderT{0pt}
\else \let\cals@borderT\relax\fi}

\def\bb{\ifx\cals@borderB\relax     % Botton border switch (off-on)
    \def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}

\def\rb{\ifx\cals@borderR\relax     % Right border switch (off-on)
    \def\cals@borderR{0pt}
\else \let\cals@borderR\relax\fi}

\def\lb{\ifx\cals@borderL\relax     % Left border switch (off-on)
    \def\cals@borderL{0pt}
\else \let\cals@borderL\relax\fi}

\newcommand{\hstrut}{\vphantom{$\bigg|$}}

% R1
\thead{\bfseries
\brow
    \light\alignC\cell{\vfil Figur\hstrut}
    \cell{\vfil Antall sirkler\hstrut}\light
\erow
\mdseries}
% R2 Body
\brow
    \bb\cell{\vfil $1$}
    \cell{\vfil 6}
\erow

% R3 Body
\brow
    \cell{$2$}
    \cell{$10$}
\erow
% R4 Body
\brow
    \cell{$3$}
    \cell{$14$}
\erow
% R5 Body
\brow
    \cell{$4$}
    \cell{}
\erow
% R6 Body
\brow
    \cell{\vfil$5$}
    \cell{}
\erow
% R7 Body
\brow
    \bb\cell{{\vfil$n$}}
    \cell{}
\erow
\makeatletter
\end{calstable}\par % \par to align the tabular

\end{document}

enter image description here

Sveinung
  • 20,355