10

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

\newcounter{question}
\newcounter{partno}
\newcounter{subpart}
\newcounter{subsubpart}
\newcounter{choice}

\newenvironment{questions}{%

\newcounter{choice}
\renewcommand\thechoice{\Alph{choice}}
\newcommand\choicelabel{\thechoice.}

\newenvironment{choices}%
  {\list{\choicelabel}%
     {\usecounter{choice}\def\makelabel##1{\hss\llap{##1}}%
       \settowidth{\leftmargin}{W.\hskip\labelsep\hskip 2.5em}%
       \def\choice{%
         \item
       } % choice
       \labelwidth\leftmargin\advance\labelwidth-\labelsep
       \topsep=0pt
       \partopsep=0pt
     }%
  }%

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

lockstep
  • 250,273
Kayla
  • 1,655

1 Answers1

19

\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

\setcounter{<name>}{<value>} 

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

\addtocounter{<name>}{<value>}

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

\stepcounter{<name>}

to add one to the counter.

\the<name>

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

\renewcommand\the<name>{\arabic{<name>}} 

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:

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

\newcounter{myexa}
\newenvironment{myexample}
  {\begin{quote}\stepcounter{myexa}{\bfseries\themyexa.}~\ignorespaces}
  {\end{quote}}

\begin{document}

\lipsum[2]
\begin{myexample}
\lipsum[2]
\end{myexample}
\lipsum[2]
\begin{myexample}
\lipsum[2]
\end{myexample}

\end{document}

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.

Gonzalo Medina
  • 505,128