以下示例产生了一个难以理解的错误:
\documentclass[letterpaper,12pt]{exam}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}
\begin{document}
We are interested in the following matrix:
\begin{gather*}
\ifcase 0 \relax
C = \begin{pmatrix} 1 & 2 \\ 1 & -2 \end{pmatrix}
\or
C = \begin{pmatrix} 3 & 4 \\ 1 & -5 \end{pmatrix}
\fi
\end{gather*}
\end{document}
这是怎么回事?如果我用其他任何东西替换矩阵,它都可以正常工作,所以我推测这是 pmatrix 环境与 ifcase 的交互。
如果用更基础的阵列替换 pmatrix,问题仍然存在。
\documentclass[letterpaper,12pt]{exam}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}
\begin{document}
We are interested in the following matrix:
\begin{gather*}
\ifcase 0 \relax
C = \begin{array}{cc} 1 & 2 \\ 1 & -2 \end{array}
\or
C = \begin{array}{cc} 3 & 4 \\ 1 & -5 \end{array}
\fi
\end{gather*}
\end{document}
答案1
如果你加上括号,它就可以起作用。
\documentclass[letterpaper,12pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}
\begin{document}
We are interested in the following matrix:
\begin{gather*}
\ifcase 0 \relax
{ C = \begin{pmatrix} 1 & 2 \\ 1 & -2 \end{pmatrix} }
\or
{ C = \begin{pmatrix} 3 & 4 \\ 1 & -5 \end{pmatrix} }
\fi
\end{gather*}
\end{document}
答案2
问题在于&
;您可以通过支撑条目来解决它,但可能还有更好的方法:
\documentclass[letterpaper,12pt]{exam}
\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\docases}{mm}
{
\int_case:nn { #1 } { #2 }
}
\ExplSyntaxOff
\begin{document}
\newcommand{\caseexam}{1}
We are interested in the following matrix:
\begin{gather*}
\docases{\caseexam}
{
{1}{C = \begin{pmatrix} 1 & 2 \\ 1 & -2 \end{pmatrix}}
{2}{C = \begin{pmatrix} 3 & 4 \\ 1 & -5 \end{pmatrix}}
}
\end{gather*}
\renewcommand{\caseexam}{2}
We are interested in the following matrix:
\begin{gather*}
\docases{\caseexam}
{
{1}{C = \begin{pmatrix} 1 & 2 \\ 1 & -2 \end{pmatrix}}
{2}{C = \begin{pmatrix} 3 & 4 \\ 1 & -5 \end{pmatrix}}
}
\end{gather*}
\end{document}
当然,您只需设置\caseexam
一次宏。主题可以变化。