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.

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

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
add comment

1 Answer

up vote 17 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

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

share|improve this answer
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.