代码

代码

我正在尝试创建下表

在此处输入图片描述

但是,我不断收到以下错误消息

! 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.

我尝试在参数周围添加 {} 以确保siunitx列可以正常工作,但即使这样也不起作用。总结

  • 我该如何修复多列之间的空白?我添加了\\[-0.02cm],但这感觉像是世界上最肮脏的伎俩。
  • 我该如何修复\noalign错误?
  • 有没有更好的方法将其n置于表格底部的中心?

代码

\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}

答案1

你让自己的生活变得复杂multirow

括号位置错误:括号前面不能有括号\rowcolor,这没有成为下一个单元格的一部分。单元格的内容应该用括号括起来,因为它位于一S列中。

务必总是说明一列中的数字数目S

\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}

在此处输入图片描述

答案2

如果使用\cellcolor而不是\rowcolor您将获得预期的输出:

在此处输入图片描述

\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}

答案3

before使 TeX 插入对齐项 〈pre〉-text 并停止搜索包含以下内容{的材料。原因是\Rowcolor\noalign\rowcolor{是一个非空格、不可扩展的标记(这里这里有关 TeX 在表格行开头或结尾的行为的更多详细说明)。删除此额外的分组级别可以解决问题,因为这样就可以正确找到\noalignused by \rowcolor,而 TeX 正在扩展标记以查找\noalign\omit它开始读取对齐条目〈pre〉-text。

注意:我所说的〈pre〉文本对应于使用该包时在或序言>{...}中声明的材料。tabulararrayarray

\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}

截屏

答案4

如果你使用卡路里, 这很容易:

\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}

在此处输入图片描述

相关内容