我模仿书上的一个例子,这是我的测试代码,
\documentclass{article}
\newcounter{numexm}[section]
\setcounter{numexm}{0}
\renewcommand\thenumexm{\roman{numexm}}
\newenvironment{ntexmC}[5]{
\newcommand\makechoice{%
\ifcase\value{#1}%
\relax \or %
A. #2 \hfill B. #3 \hfill
C. #4 \hfill D. #5 \or %
A. #2\hspace{\stretch{2}} B. #3\hspace{\stretch{3}}\par
C. #4\hspace{\stretch{2}} D. #5\hspace{\stretch{3}} \or %
A. #2\par B. #3\par C. #4\par D. #5 \or %
\relax \fi
}%
\addvspace{.2em}\stepcounter{numexm}
\setlength\parindent{0em} Example \thenumexm.
}{\par\makechoice\addvspace{.2em}}
\begin{document}
\section{One}
\begin{ntexmC}{3}{AAAAAAA}{BBBBBBB}{CCCCCCC}{DDDDDDD}%
The title (\hspace{1em})
\end{ntexmC}
\end{document}
使用 pdflatex 编译后,报错
Missing number, treated as zero.
<to be read again>
但是我检查代码,没有发现哪里缺少参数。请问是哪里出错了?
已解决,使用 \ifcase#1
即可。
下面是正确代码
\documentclass{article}
\newcounter{numexm}[section]
\setcounter{numexm}{0}
\renewcommand\thenumexm{\roman{numexm}}
\newcounter{first}
\setcounter{first}{2}
\newenvironment{ntexmC}[5][0]{
\newcommand\makechoice{%
\ifcase#1%
\relax\or %
A. #2 \hfill B. #3 \hfill
C. #4 \hfill D. #5\or %
A. #2\hfill B. #3\hspace{\stretch{3}} \par
C. #4\hfill D. #5\hspace{\stretch{3}}\or %
A. #2\par B. #3\par C. #4\par D. #5\else %
\relax \fi
}%
\par\addvspace{.2em}\stepcounter{numexm}
\setlength\parindent{0em} Example \thenumexm.
}{\par\makechoice\par\addvspace{.2em}}
\begin{document}
\section{One}
\begin{ntexmC}[1]%
{AAAAAAA}%
{BBBBBBB}%
{CCCCCCC}%
{DDDDDDD}%
The title (\hspace{1em})
\end{ntexmC}
\end{document}
另外,使用 \addspace
时,前面需要使用一个 \par