[OS X TeX] conditionally included text

Ross Moore ross at ics.mq.edu.au
Thu Apr 7 18:52:17 EDT 2005


Hello Larry,

On 08/04/2005, at 8:51 AM, Larry Smith wrote:

> How do I conditionally include text in LaTeX?  Say I'm typing a math
> assignment and I want to put the worked-out solutions in the same file
> (each solution after its respective question), but then toggle a 
> switch to
> print either the assignment alone or with the solutions.

I happen to be working on just such a thing at present,
preparing tests for my class.

Here are the (La)TeX definitions that I use:

%%%%%%%%%%%%%%%%%%%%%%%%
%   with solutions
%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifshowAnswers
\newif\ifforMarkers
\long\def\Answer[#1]#2\\#3{\ifshowAnswers
   \noindent\ifforMarkers\textbf{#1 mark\ifnum#1=1\else s\fi.}\fi
   \\#2\par
  \else#3\fi}
\newcommand{\Solution}[1]{\removelastskip\par\textbf{Solution:}}

\showAnswerstrue  % comment this to hide answers
\forMarkerstrue   % comment this to hide comments for markers

\newcommand{\markkey}[1]{}
\ifshowAnswers\ifforMarkers
  \renewcommand{\markkey}[1]{\smallskip{\itshape #1\par}}
\fi\fi




There are 2 conditionals:
   \ifshowAnswers    shows answers if  \showAnswerstrue
   \ifforMarkers     shows comments intended for the markers

Since the defaults are  \showAnswersfalse  and  \forMarkersfalse
all you need to do is comment-out the \....true line in order
to flip the switch.

Now you just define some macros, such as \Answer ,
that use these conditionals in their expansions.
Also, use the conditionals to give an alternative
expansion in the true or false case.



Here's a typical usage of the definitions given
  above:
  (with pattern   \Answer[#1]#2\\#3  )

\item
Find the derivative of $y=2x^3 + 4x^{-6} + 2$.
%
\hfill\Answer[2]
\Solution: $\dfrac{\dd y}{\dd x}=6x^2 - 24 x^{-7}$.
\\
{\vspace{1.5cm}}


In detail:
    #1  =  2   ---  number of marks allocated
    #2  =  .... body of solution ...
    #3  =  \vspace{1.5cm}   --- creates space for student answers


(BTW:   \providecommand{\dd}{\mathrm{d}}  for upright d )


>
> Thanks in advance,


Hope this helps,

	Ross


> Larry
>
> --------------------- Info ---------------------
> Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
>            & FAQ: http://latex.yauh.de/faq/
> TeX FAQ: http://www.tex.ac.uk/faq
> List Post: <mailto:MacOSX-TeX at email.esm.psu.edu>
>
>
>
------------------------------------------------------------------------
Ross Moore                                         ross at maths.mq.edu.au
Mathematics Department                             office: E7A-419
Macquarie University                               tel: +61 +2 9850 8955
Sydney, Australia                                  fax: +61 +2 9850 8114
------------------------------------------------------------------------

--------------------- Info ---------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
           & FAQ: http://latex.yauh.de/faq/
TeX FAQ: http://www.tex.ac.uk/faq
List Post: <mailto:MacOSX-TeX at email.esm.psu.edu>





More information about the MacOSX-TeX mailing list