TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
Welcome to TeX.SE. It eliminates having to use a separate .sty style file. See the comments at the end of Create multiple choice command or environment within my own latex class?. – Peter Grill Oct 23 '12 at 1:08
Are you the same person as the one who posted the question I linked to? The rep is different but the userid is similar? If so, perhaps your accounts should be merged. – Peter Grill Oct 23 '12 at 1:10
up vote 19 down vote accepted

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

share|improve this answer

Your Answer


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.