如何在表格内创建多列?

如何在表格内创建多列?

我的老师给我们布置了一项作业,我已经完成了大部分,但它仍然与示例不同。示例如下:在此处输入图片描述

但我做的是这样的:在此处输入图片描述

这是我的代码:

\documentclass{article}
\usepackage[svgnames,table]{xcolor}

% allow multi-row entries in table cells
\usepackage{multirow}

% use rules from booktabs
\usepackage{booktabs}

% The header row should have White, large bold text
\newcommand\heading[1]{\large{#1}}

% adapt the code from the "Colouring more table rows"
% slide to highlight Janice's scores DodgerBlue!20
\usepackage{etoolbox}
\newcommand\colouredRows{}% initialise empty list
\forcsvlist{\listadd\colouredRows}{1,2,5,6,9,10,13,14,17,18}% build the list
\newcommand\SetRowColour{%
  \xifinlist{\number\rownum}\colouredRows{\cellcolor{DodgerBlue!20}}{}%
}

% for use below, define new column types C, L and R that are centred,
% left-justified and right-justified, respectively
\usepackage{array}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\begin{document}

  \begin{center}
    \rowcolors{4}{}{}% initialise the \rownum counter from xcolor
    \begin{tabular}{LLCCCCR}
      % you can only add \multirow commands, the names and totals below this line
      \heading{Student} & \heading{Marker} & \multicolumn{4}{C}{\heading{Essay}}
                              & \heading{Total} \\\toprule
                & & Originality & Synthesis & Effort & Overall & \\\midrule
                              & Alice  & 9 & 8 & 7 & 8.0 & \\
                              & Amelia & 8 & 9 & 7 & 8.0 & \\
                              & Anton  & 9 & 7 & 7 & 7.7 & \\
        \multirow{4}{*}{James}& Arthur & 7 & 8 & 8 & 7.7 & \multirow{4}{*}{7.9}\\
                              & Alice  & 9 & 9 & 7 & 8.3 & \\
                              & Amelia & 9 & 8 & 8 & 8.3 & \\
                              & Anton  & 8 & 8 & 8 & 8.0 & \\
       \multirow{4}{*}{Janice}& Arthur & 7 & 9 & 7 & 7.7 & \multirow{4}{*}{8.1}\\
                              & Alice  &10 & 9 &10 & 9.7 & \\
                              & Amelia & 9 & 7 & 8 & 8.0 & \\
                              & Anton  & 9 & 8 & 8 & 8.3 & \\
        \multirow{4}{*}{Julie}& Arthur & 9 &10 & 9 & 9.3 & \multirow{4}{*}{8.8}\\\bottomrule
    \end{tabular}
  \end{center}

\end{document}

答案1

我建议使用tabularray包进行格式化。它为表格格式提供了一个非常灵活的 keyval 接口。在这个例子中,我使用了包booktabsfromtabularray来保留正确的间距。

表输出

\documentclass{article}
\usepackage[svgnames]{xcolor}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
    
\begin{center}
    \begin{booktabs}{
        colspec={llccccr},
        row{1}={fg=white, bg=blue!50!black, font=\large\bfseries},
        row{7-10}={bg=DodgerBlue!20},
    }
    \toprule
    Student & Marker & \SetCell[c=4]{c} Essay & & & & Total \\
    \midrule
    & & Originality & Synthesis & Effort & Overall & \\
    \cmidrule[lr]{3-6}
    \SetCell[r=4]{} James & Alice & 9 & 8 & 7 & 8.0 & \SetCell[r=4]{m, font=\bfseries} 7.9 \\
    & Amelia & 8 & 9 & 7 & 8.0 & \\
    & Anton & 9 & 7 & 7 & 7.7 & \\
    & Arthur & 7 & 8 & 8 & 7.7 & \\
    \SetCell[r=4]{} Janice & Alice & 9 & 9 & 7 & 8.3 & \SetCell[r=4]{m, font=\bfseries} 8.1 \\
    & Amelia & 9 & 8 & 8 & 8.3 & \\
    & Anton & 8 & 8 & 8 & 8.0 & \\
    & Arthur & 7 & 9 & 7 & 7.7 & \\
    \SetCell[r=4]{} Julie & Alice & 10 & 9 &10 & 9.7 & \SetCell[r=4]{m, font=\bfseries} 8.8 \\
    & Amelia & 9 & 7 & 8 & 8.0 & \\
    & Anton & 9 & 8 & 8 & 8.3 & \\
    & Arthur & 9 &10 & 9 & 9.3 & \\
    \bottomrule
    \end{booktabs}
\end{center}
    
\end{document}

也可以交替着色 4 行。如果您对此感兴趣,请发表评论。

答案2

这里尝试尽可能少地修改您的代码,以获得所需的结果。主要变化是 (a) 几个\rowcolor指令和 (b)\multirow{-4}{*}{...}而不是\multirow{4}{*}{...}

在此处输入图片描述

\documentclass{article}
\usepackage[svgnames,table]{xcolor}
\usepackage{multirow,booktabs,array}
\newcommand\heading[1]{\textcolor{white}{\bfseries #1}}

\begin{document}
    \begin{center}
    \begin{tabular}{llccccr}
    \rowcolor{DarkBlue} \heading{Student} & \heading{Marker} & 
                    \multicolumn{4}{c}{\heading{Essay}} & \heading{Total} \\
    & & Originality & Synthesis & Effort & Overall & \\
    \cmidrule(lr){3-6}
                               & Alice  & 9 & 8 & 7 & 8.0 & \\
                               & Amelia & 8 & 9 & 7 & 8.0 & \\
                               & Anton  & 9 & 7 & 7 & 7.7 & \\
       \multirow{-4}{*}{James} & Arthur & 7 & 8 & 8 & 7.7 & 
       \multirow{-4}{*}{\bfseries 7.9} \\
     \rowcolor{DodgerBlue!20}  & Alice  & 9 & 9 & 7 & 8.3 & \\
     \rowcolor{DodgerBlue!20}  & Amelia & 9 & 8 & 8 & 8.3 & \\
     \rowcolor{DodgerBlue!20}  & Anton  & 8 & 8 & 8 & 8.0 & \\
     \rowcolor{DodgerBlue!20}
      \multirow{-4}{*}{Janice} & Arthur & 7 & 9 & 7 & 7.7 & 
      \multirow{-4}{*}{\bfseries 8.1} \\
                               & Alice  &10 & 9 &10 & 9.7 & \\
                               & Amelia & 9 & 7 & 8 & 8.0 & \\
                               & Anton  & 9 & 8 & 8 & 8.3 & \\
      \multirow{-4}{*}{Julie}  & Arthur & 9 &10 & 9 & 9.3 & 
      \multirow{-4}{*}{\bfseries 8.8} \\
    \bottomrule
    \end{tabular}
    \end{center}
\end{document}

相关内容