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.

Can someone explain the following preamble to me? This is copied and pasted from a problem set template my T.A. made to help us typeset our homework... I'd really like to understand how it works. The thing that is stumping me the most is lines 5-6; why was it necessary to define a new TeX macro. Is it usually necessary to do so in LaTeX documents?

\setlength{\parindent}{0pt}
\setlength{\parskip}{5pt plus 1pt} 
\pagestyle{empty}

\def\indented#1{\list{}{}\item[]}
\let\indented=\endlist

\newcounter{questionCounter}
\newcounter{partCounter}[questionCounter]
\newenvironment{question}[2][\arabic{questionCounter}]{%
  \setcounter{partCounter}{0}%
  \vspace{.25in} \hrule \vspace{0.5em}%
  \noindent{\bf #2}%
  \vspace{0.8em} \hrule \vspace{.10in}%
  \addtocounter{questionCounter}{1}%
}{}
\renewenvironment{part}[1][\alph{partCounter}]{%
  \addtocounter{partCounter}{1}%
  \vspace{.10in}%
  \begin{indented}%
    {\bf (#1)} %
}{\end{indented}}
share|improve this question

3 Answers 3

up vote 5 down vote accepted

@egreg's answer provides the means to correct the code, as well as introducing some improvements. Consider this answer merely an explanation as to way the original (faulty) code still compiled. The sequence of events that TeX followed are

  1. \def\indented#1{\list{}{}\item[]}

    This defined the macro (not environment) \indented to take one argument. Since it is not used in the expansion, it will be discarded. For example, using only the above (and removing line 6 (given below in [2]), would gobble something when using \indented{something}.

  2. \let\indented=\endlist

    This rewrites the definition of \indented as defined in [1] above, making [1] useless.

  3. \begin{indented} ... \end{indented}

    Since the macro \indented is not undefined (or, defined) at this point - however meaningless its definition is, \begin{indented} is expanded to \indented, which is \endlist, which has no problem completing. I'm stumped by why TeX does not produce an error when trying to expand \endindented which is undefined. However, this could be a case where defining a start \<myenv> as the end \end<myenv> (like in [2]) may settle out the unmatched start-and-end of an environment <myenv>.

  4. Finally, since the indented environment was never actually formally defined as anything, it didn't amount to anything in terms of the visible output.

share|improve this answer

Line 6 should read

\let\endindented\endlist

These two lines are just a slightly more efficient way to say

\newenvironment{indented}
  {\begin{list}{}{}\item[]}
  {\end{list}}

When you do \newenvironment{xyz} you are really defining two commands: \xyz and \endxyz, which are executed (along with other commands) when LaTeX finds \begin{xyz} and \end{xyz}. Your TA is just showing off. :)

It's quite mysterious why the definition of question doesn't say

\refstepcounter{questionCounter}

instead of \addtocounter{questionCounter}{1}, thus inhibiting the possibility to say \label{a-question} in order to refer later to a question. It's quite mysterious also why not defining

\renewenvironment{part}[1][\alph{partCounter}]
  {\refstepcounter{partCounter}%
   \vspace{.10in}%
   \begin{list}{}{}\item[]\textbf{(#1)} }
  {\end{list}}

that would not need an auxiliary environment indented.

Also redefining \part doesn't seem a good idea.

share|improve this answer
4  
In short, the OP's TA wanted to show off, but failed to do so. ;-) –  lockstep Sep 10 '11 at 16:07
2  
I suspect his name is Ben Lee User. ;-) –  egreg Sep 10 '11 at 16:30
    
so is it not usually necessary to define TeX macros like that within a LaTeX document? even if it is more efficient, the main reason LaTeX exists is to provide an an abstracted TeX that makes page layout easier, right? –  Alex Lockwood Sep 12 '11 at 5:02

As a complement to egreg's answer: The code lines

\newenvironment{indented}
  {\begin{list}{}{}\item[]}
  {\end{list}}

(as amended by egreg) define the indented environment as a one-item list (and issue the necessary \item command). This way, the spacing parameters of the new environment will conform to that of lists like itemize and enumerate. (The LaTeX standard classes use the same trick to define e.g. the quote environment.)

share|improve this answer

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.