[texhax] Repeating blocks of LaTeX code -- can it be done?

Uwe Lueck uwe.lueck at web.de
Thu Jul 10 17:05:57 CEST 2008


Don Gingrich <gingrich at rmit.edu.au> schrieb am 10.07.2008 15:10:20:
> What I'm trying to do is simple conceptually, I've got a LaTeX
> template for final examinations and I want to put space on the
> exam paper for the students to answer. I may not be doing this 
> the best way presently, and I'm sure I'll hear if there's a better
> way to do it. But what I'd also like to do (and I had a look at
> _The_LaTeX_Companion_ and didn't find any suggestions) is
> to be able to have the code for a vertical space and a horizontal
> line and put it in a block with a multiplier to get n lines for some 
> value of n. Given that TeX and LaTeX behave like a programming
> language, I would have thought this was possible.
> 
> The relevant lines from the template follow:
> 
> \newcommand{\IncludeWritingSpace}{true}
> \newcommand{\ansSpace}[1]{\ifthenelse{\equal{\IncludeWritingSpace}{true}} {\\{\bf Write answer here:} #1}{}}
> \begin{enumerate}
> \item This is an exam question
> 
> \answer{ there is a similar block of code to the ansSpace command that allows the answer to be printed 
>                optionally -- for marking guides and for students to review their answers versus the required answers}
> % what I would like is to be able to put a multiplier on the combined vspace
> % and rule lines to get 2 or more lines for students to write on.
> \ansSpace{
> \vspace{14pt}\\
> \rule[0pt]{14cm}{0.75pt}\\
> \vspace{14pt}\\
> \rule[0pt]{14cm}{0.75pt}\\
> \vspace{14pt}\\
> \rule[0pt]{14cm}{0.75pt}\\
> \vspace{14pt}\\
> \rule[0pt]{14cm}{0.75pt}\\
> \vspace{14pt}\\
> \rule[0pt]{14cm}{0.75pt}\\
> \vspace{14pt}\\
> \rule[0pt]{14cm}{0.75pt}\\
> }
> 
> \item Another question
> 
> \end{enumerate}

\usepackage{forloop}

And here is a "plain TeX" way 
(no, it's LaTeX, and LaTeX's \loop improves the plain TeX \loop) 
to explain the concept:

\newcounter{ahead}
\newcommand*{\answerlines}[1]{%
  \loop 
    \ifnum\value{ahead}>0\relax % sorry, I don't know ifthenelse
    \addtocounter{ahead}{-1}%
    \vspace{14pt}\rule[0pt]{14cm}{.75}\\%
  \repeat
  \ignorespaces} %% needed?

So 
    \answerlines{15}

should produce 15 lines for answering. 

Isn't \rule[0pt] the same as \rule!? 

Uwe. 



More information about the texhax mailing list