Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have this command in a template given by my prof

\newcommand{\homeworkProblemName}{}%
\newcounter{homeworkProblemCounter}%
\newenvironment{homeworkProblem}[1][Problem \arabic{homeworkProblemCounter} - Homework]%
  {\stepcounter{homeworkProblemCounter}%
   \renewcommand{\homeworkProblemName}{#1}%
   \section{\homeworkProblemName}%
   \enterProblemHeader{\homeworkProblemName}}%
  {\exitProblemHeader{\homeworkProblemName}}%

I want to add in the third line a second parameter to be able to have something like Problem 1 - Homework 1 but the number of the homework can change, for instance, I can have Problem 1 - HW 1 then Problem 4 - HW 2 then Problem 2 - HW 3 and so on.

I tried with this

\newcommand{\homeworkProblemName}{}%
\newcounter{homeworkProblemCounter}%
\newenvironment{homeworkProblem}[2][Problem \arabic{homeworkProblemCounter} - Homework #2]%
  {\stepcounter{homeworkProblemCounter}%
   \renewcommand{\homeworkProblemName}{#1}%
   \section{\homeworkProblemName}%
   \enterProblemHeader{\homeworkProblemName}}%
  {\exitProblemHeader{\homeworkProblemName}}%

but I am getting a compilation error

`Illegal parameter number in definition of \homeworkProblem. <to be read again> 2`
`{\exitProblemHeader{\homeworkProblemName}}`

I had no experience on creating commands and all that stuff, I'm just a basic LaTeX user.

Can anyone tell me how to make this work?

UPDATE

For what it is useful here is the definition of the exitProblemHeader and 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}%
share|improve this question
1  
Welcome to TeX.SX. A tip: If you indent lines by 4 spaces, then they're marked as a code sample. You can also highlight the code and click the "code" button ({}) or select your code and hit Ctrl+K. –  Claudio Fiandrino Mar 9 '13 at 12:27
    
Can you show also how \enterProblemHeader and \exitProblemHeader are defined? –  egreg Mar 9 '13 at 13:16
    
Yes I can, check my update. –  BRabbit27 Mar 9 '13 at 15:21
add comment

2 Answers

up vote 1 down vote accepted

The second square brackets in the definiton of an environment specify the default value of the first argument (and thereby make it optional). It is impossible to refer to other arguments in there (you're trying to refer to the second one).

I suggest defining command \homeworkNumber (it will be the same for all problems anyway) and using that in the definition:

\newcommand{\homeworkName}{42}%
\newenvironment{homeworkProblem}[1][Problem \arabic{homeworkProblemCounter} - Homework \homeworkName]
{...}{...}
share|improve this answer
    
I will try it right away. But actually that homework number will be different, for instance, Problem 1 - HW 1, Problem 3 - HW 2, Problem 2 - HW 3. I'll update my question to make this point clear. –  BRabbit27 Mar 9 '13 at 12:41
    
Or probably I can renew the command every time I have to change the number of the homework, can't I? –  BRabbit27 Mar 9 '13 at 12:44
    
Yes, you can do that. –  Sašo Živanović Mar 9 '13 at 12:49
add comment

My impression is that the original definition is faulty and that the environment homeworkProblem is never called with the optional argument.

\newcommand{\homeworkProblemName}{}
\newcounter{homeworkProblemCounter}
\newenvironment{homeworkProblem}[1][Problem \arabic{homeworkProblemCounter} - Homework]
  {\stepcounter{homeworkProblemCounter}
   \renewcommand{\homeworkProblemName}{#1}%
   \section{\homeworkProblemName}%
   \enterProblemHeader{\homeworkProblemName}}
  {\exitProblemHeader{\homeworkProblemName}}

Calling

\begin{homeworkProblem}[Problem \arabic{homeworkProblemCounter} - Foo]

just to change the last word seems clumsy; calling

\begin{homeworkProblem}[Problem]

would produce no number, still increasing the counter.

If the environment is never used with an optional argument in documents, just change it to accept one that makes sense for you:

\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}

Thus

\begin{homeworkProblem}

will give the original behavior, while

\begin{homeworkProblem}[4]

will do "Problem 3 - Homework 4" (if 3 is the current problem number).

share|improve this answer
    
I agree that the original definition is faulty. I think, however, that the social context is also important here. OP was given the template by the professor who might actually use the optional argument sometimes, e.g. in \begin{homeworkProblem}[Example].... If the signature of the environment or the semantics of its arguments is changed, this might cause trouble for the professor. –  Sašo Živanović Mar 9 '13 at 16:16
    
@SašoŽivanović Then the correct way is to use a new environment name –  egreg Mar 9 '13 at 16:19
    
But imagine that the prof wants to collect the answers into a single document ... –  Sašo Živanović Mar 9 '13 at 16:24
    
I appreciate all your feedback, as I said I have no knowledge on this kind of stuff in LaTeX, still is good to start learning. As @SašoŽivanović said, the template was given by the prof so I wanted to change it as less as possible and the answer accepted works for me and the changes are small. –  BRabbit27 Mar 9 '13 at 16:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.