感谢 David Carlisle 在此网站上提供的回答,我成功编写了如下代码:
\documentclass[a4paper,10pt]{article}
\usepackage{colortbl}
\usepackage{xcolor}
\newcommand{\acell}[1]{\gdef\acontent{#1}#1}
\newcommand{\bcell}[1]{\gdef\bcontent{#1}#1}
\def\atest{A}
\def\btest{B}
\def\ctest{C}
\def\dtest{D}
\def\etest{E}
\def\ntest{-}
\newcommand{\zcell}{%
\ifx\acontent\ntest\cellcolor{yellow}0
\else
\ifx\acontent\atest\ifx\bcontent\atest\cellcolor{green}1.5%
\else\cellcolor{red}-0.4%
\fi
\else
\ifx\acontent\btest\ifx\bcontent\btest\cellcolor{green}1.5%
\else\cellcolor{red}-0.4%
\fi
\else
\ifx\acontent\ctest\ifx\bcontent\ctest\cellcolor{green}1.5%
\else\cellcolor{red}-0.4%
\fi
\else
\ifx\acontent\dtest\ifx\bcontent\dtest\cellcolor{green}1.5%
\else\cellcolor{red}-0.4%
\fi
\else
\ifx\acontent\etest\ifx\bcontent\etest\cellcolor{green}1.5%
\else\cellcolor{red}-0.4%
\fi
\fi\fi\fi\fi\fi\fi}
\begin{document}
\section*{Student name}
\begin{tabular}{c c c c c c c c c}
Question & Answer & Correct answer & Points & & Question & Answer & Correct Answer & Points \\\hline
\textbf{1} & \acell{A} & \bcell{E} & \zcell & & \textbf{31} & \acell{} & \bcell{A} & \zcell \\
\textbf{2} & \acell{E} & \bcell{E} & \zcell & & \textbf{32} & \acell{} & \bcell{B} & \zcell \\
\textbf{3} & \acell{-} & \bcell{E} & \zcell & & \textbf{33} & \acell{} & \bcell{D} & \zcell \\
& & & & & & & \textbf{Score} & \textbf{} \\
\end{tabular}
Correct answers = ?
Wrong answers = ?
Null answers = ?
\end{document}
基本上是一个表格,其中给定的答案与正确答案进行比较,并根据答案的正确性、错误性或空值打印一个漂亮的彩色单元格。
我想问的是,是否有办法让 LaTeX 计算黄色、绿色和红色单元格并将数字打印在文档的某个位置。这可能吗?
答案1
我介绍了基于颜色索引的计数器。
\documentclass[a4paper,10pt]{article}
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{ifthen}
\newcounter{greencount}
\newcounter{redcount}
\newcounter{yellowcount}
\setcounter{greencount}{0}
\setcounter{redcount}{0}
\setcounter{yellowcount}{0}
\newcommand\sellcolor[1]{%
\cellcolor{#1}%
\ifthenelse{\equal{#1}{green}}{\stepcounter{greencount}}%
{\ifthenelse{\equal{#1}{red}}{\stepcounter{redcount}}%
{\ifthenelse{\equal{#1}{yellow}}{\stepcounter{yellowcount}}{}%
}%
}%
}
\newcommand{\acell}[1]{\gdef\acontent{#1}#1}
\newcommand{\bcell}[1]{\gdef\bcontent{#1}#1}
\def\atest{A}
\def\btest{B}
\def\ctest{C}
\def\dtest{D}
\def\etest{E}
\def\ntest{-}
\newcommand{\zcell}{%
\ifx\acontent\ntest\sellcolor{yellow}0
\else
\ifx\acontent\atest\ifx\bcontent\atest\sellcolor{green}1.5%
\else\sellcolor{red}-0.4%
\fi
\else
\ifx\acontent\btest\ifx\bcontent\btest\sellcolor{green}1.5%
\else\sellcolor{red}-0.4%
\fi
\else
\ifx\acontent\ctest\ifx\bcontent\ctest\sellcolor{green}1.5%
\else\sellcolor{red}-0.4%
\fi
\else
\ifx\acontent\dtest\ifx\bcontent\dtest\sellcolor{green}1.5%
\else\sellcolor{red}-0.4%
\fi
\else
\ifx\acontent\etest\ifx\bcontent\etest\sellcolor{green}1.5%
\else\sellcolor{red}-0.4%
\fi
\fi\fi\fi\fi\fi\fi}
\begin{document}
\section*{Student name}
\begin{tabular}{c c c c c c c c c}
Question & Answer & Correct answer & Points & & Question & Answer & Correct Answer & Points \\\hline
\textbf{1} & \acell{A} & \bcell{E} & \zcell & & \textbf{31} & \acell{} & \bcell{A} & \zcell \\
\textbf{2} & \acell{E} & \bcell{E} & \zcell & & \textbf{32} & \acell{} & \bcell{B} & \zcell \\
\textbf{3} & \acell{-} & \bcell{E} & \zcell & & \textbf{33} & \acell{} & \bcell{D} & \zcell \\
& & & & & & & \textbf{Score} & \textbf{} \\
\end{tabular}
Correct answers = \thegreencount
Wrong answers = \theredcount
Null answers = \theyellowcount
\end{document}