我的教授给我的模板中有这个命令
\newcommand{\homeworkProblemName}{}%
\newcounter{homeworkProblemCounter}%
\newenvironment{homeworkProblem}[1][Problem \arabic{homeworkProblemCounter} - Homework]%
{\stepcounter{homeworkProblemCounter}%
\renewcommand{\homeworkProblemName}{#1}%
\section{\homeworkProblemName}%
\enterProblemHeader{\homeworkProblemName}}%
{\exitProblemHeader{\homeworkProblemName}}%
我想在第三行添加第二个参数,以便能够有类似的内容,Problem 1 - Homework 1
但家庭作业的数量可以改变,例如,我可以有Problem 1 - HW 1
然后Problem 4 - HW 2
等等Problem 2 - HW 3
。
我试过这个
\newcommand{\homeworkProblemName}{}%
\newcounter{homeworkProblemCounter}%
\newenvironment{homeworkProblem}[2][Problem \arabic{homeworkProblemCounter} - Homework #2]%
{\stepcounter{homeworkProblemCounter}%
\renewcommand{\homeworkProblemName}{#1}%
\section{\homeworkProblemName}%
\enterProblemHeader{\homeworkProblemName}}%
{\exitProblemHeader{\homeworkProblemName}}%
但我收到编译错误
`Illegal parameter number in definition of \homeworkProblem. <to be read again> 2`
`{\exitProblemHeader{\homeworkProblemName}}`
我没有创建命令和所有这些东西的经验,我只是一个基本的 LaTeX 用户。
有人能告诉我怎样做这个吗?
更新
这里有用的是 exitProblemHeader 和 enterProblemHeader 的定义
\newcommand{\enterProblemHeader}[1]{\nobreak\extramarks{#1}{#1 continued on next page\ldots}\nobreak%
\nobreak\extramarks{#1 (continued)}{#1 continued on next page\ldots}\nobreak}%
\newcommand{\exitProblemHeader}[1]{\nobreak\extramarks{#1 (continued)}{#1 continued on next page\ldots}\nobreak%
\nobreak\extramarks{#1}{}\nobreak}%
答案1
环境定义中的第二个方括号指定第一个参数的默认值(从而使其成为可选的)。不可能在其中引用其他参数(您试图引用第二个参数)。
我建议定义命令\homeworkNumber
(无论如何它对于所有问题都是相同的)并在定义中使用它:
\newcommand{\homeworkName}{42}%
\newenvironment{homeworkProblem}[1][Problem \arabic{homeworkProblemCounter} - Homework \homeworkName]
{...}{...}
答案2
我的印象是,最初的定义是有缺陷的,homeworkProblem
环境绝不使用可选参数调用。
\newcommand{\homeworkProblemName}{}
\newcounter{homeworkProblemCounter}
\newenvironment{homeworkProblem}[1][Problem \arabic{homeworkProblemCounter} - Homework]
{\stepcounter{homeworkProblemCounter}
\renewcommand{\homeworkProblemName}{#1}%
\section{\homeworkProblemName}%
\enterProblemHeader{\homeworkProblemName}}
{\exitProblemHeader{\homeworkProblemName}}
呼唤
\begin{homeworkProblem}[Problem \arabic{homeworkProblemCounter} - Foo]
仅仅改变最后一个词似乎很笨拙;调用
\begin{homeworkProblem}[Problem]
不会产生任何数字,但计数器仍会增加。
如果文档中从未使用环境和可选参数,则只需将其更改为接受对您有意义的参数:
\newcommand{\homeworkProblemName}{}
\newcounter{homeworkProblemCounter}
\newenvironment{homeworkProblem}[1][]
{\stepcounter{homeworkProblemCounter}
\renewcommand{\homeworkProblemName}{%
Problem \arabic{homeworkProblemCounter} - Homework\addNumber{#1}}%
\section{\homeworkProblemName}%
\enterProblemHeader{\homeworkProblemName}}
{\exitProblemHeader{\homeworkProblemName}}
\newcommand{\addNumber}[1]{\if\relax\detokenize{#1}\relax\else\space#1\fi}
因此
\begin{homeworkProblem}
将给出原始行为,而
\begin{homeworkProblem}[4]
将执行“问题 3 - 家庭作业 4”(如果 3 是当前问题编号)。