In exam.cls, I saw the following codes which I don't understand how do they function in the document class (exam):




       \settowidth{\leftmargin}{W.\hskip\labelsep\hskip 2.5em}%
       } % choice

What are their purposes, especially the \newenvironment{questions} and \newcounter{question}?


\newenvironment is used to define a new environment. The syntax (without optional argument) is

\newenvironment{<name>}[<number of arguments>]
  {<begin definition>}
  {<end definition>}

where <number of arguments> is an integer number between zero and nine, inclusive. Inside <begin definition> one can access the mandatory arguments (if present) using #1, #2, up to #<number of arguments>; those arguments are not directly accessible in the <end definition> part in the same manner, but you can store them using, for example, macros in <begin definition> and then use the macros in the <end definition> part.

When an optional argument is used, the syantx is

\newenvironment{<name>}[<number of arguments>][<default>]
  {<begin definition>}
  {<end definition>}

In this case, the optional argument can be accessed using #1 and the mandatory arguments, using #2 up to #<number of arguments>; the optional argument has the default value specified by <default>.

\newcounter is the LaTeX command to define a new counter; the syntax is

\newcounter{<name>}[<old counter>]

This globally defines the counter <name> and initializes it to zero; if the name of an already existing counter (<old counter>) is specified in the optional argument, then the newly defined counter <name> is reset when <old counter> is incremented.

Associate commands are


to globally set the counter to the value given by <value>.


to globally increment the counter by the value given by <value>.


to add one to the counter.


the representation of the counter <name>; one can say, for example,


to get a representation using Hindu-Arabic numerals, and analogously one could use \Alph{<name>} (for a representation using upper-cased alphabetic characters), \alph{<name>} (for a representation using lower-cased alphabetic characters), \Roman{<name>} (for a representation using upper-cased Roman numerals), and \roman{<name>} (for a representation using lower-cased Roman numerals).

As a little example, let's define an environment that writes its contents using indentation on both sides and with numbering; for this we can use the existing quote environment; we create a counter for the numbering; the <begin part> simply starts the quote environment, steps the newly define counter and typesets its value; the <end part> just ends the quote environment:

\usepackage{lipsum}% just to generate text for the example





enter image description here

In your concrete example (which only shows part of the original definitions and has duplicate definitions (i.e., \newcounter{choice}) that will produce errors), a new environment called questions is defined; internally this environment uses a numbered \list that will produce the numbering using the newly defined choice counter.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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