[OS X TeX] LaTeX question

Ross Moore ross at ics.mq.edu.au
Tue Jun 17 22:16:18 CEST 2008


Hi Roberto,

On 18/06/2008, at 1:39 AM, Roberto Avanzi wrote:
> Hi all.
>
> I have following question which is more or less OT because it is  
> not TeX-on-OS-X-related.  I would like to "dump" a _copy_ of some  
> text - in particular numbered environments - to an external file,  
> which I can later include in my LaTeX source, but it must be an  
> expanded version of the text.
>
> The idea is that at the end of some course notes I have a list of  
> all theorems in their original form, with the corresponding  
> original numberings. I.e., if there is a theorem in the main body  
> given by
>
> \begin{theorem}
>   The sum of $1$ and $2$ is given by
>   \begin{equation}
>     \label{eq:three}
>      3
>      \enspace.
>   \end{equation}
> \end{theorem}
>
> and typeset as
>
> Theorem 2.1. The sum of 1 and 2 is given by
>                     3.                   (5)
>
>
> I would like the same text to be dumped to an auxiliary file so  
> that I can include it at the end of the course notes. So I would  
> again the same text at the end of the book.

This is a pretty standard kind of thing to do.
Best advice is to read the relevant section of The TeXbook,
concerning the macros and primitives
    \newwrite  \openout  \write \closeout

Doubtless there is a LaTeX package that tries to use these
in a supposedly more friendly manner, but in TeX this is how
these are used:

% In the preamble, declare a write-channel:
%    (call it whatever you like)
\newwrite\backcontent


% when you want to start writing into the file:
%   choose the filename for this
\immediate\openout\backcontent=exercises.tex


% write stuff into the file:
%   (do as many of these as you need)
\immediate\write\backcontent{ ... stuff ... }


% when you have completed writing into the file:
\immediate\closeout\backcontent


Now the issue is how to make the  ... stuff ...
be the correct tokens for the information that you
really want to be in the file  exercises.tex .
This is the trickiest part, and it helps "a lot"
is you understand the TeX concepts of tokens, token-lists,
macros and macro-expansion.

Here is an example of what you might try, and the kind
of difficulties that you will face.

Suppose you have stored the tokens in a macro, as follows:

\def\thistheorem{%
  \begin{theorem}
   The sum of $1$ and $2$ is given by
   \begin{equation}
     \label{eq:three}
      3
      \enspace.
   \end{equation}
  \end{theorem}
}%  end of \def\thistheorem

Then you might try to use:

    \immediate\write\backcontent{\thistheorem}%

But this will give you a real mess, due to the
expansion of LaTeX's  \begin  and \end  macros.
Try it, to see what you do get!


Alternatively, you could use a token-list instead
of a macro:

\newtoks\theoremtoks

\theoremtoks={\begin{theorem}
   The sum of $1$ and $2$ is given by
   ...
  \end{theorem}}% end of \theoremtoks

and use it as:

  \immediate\write\backcontent{\the\theoremtoks}%


This will give the same kind of mess as above.


Better is coding such as the following.

First define a macro that controls what you
want to write:

\newcommand{\writetobackcontent}[1]{%
   \begingroup
    \def\begin{\noexpand\begin}% suppress expansion
    \def\end{\noexpand\end}% suppress expansion
    \def\enspace{\noexpand\enspace}% suppress expansion
    \def\label##1{}%  gobble \label commands
    %
    \immediate\write\backcontent{#1}%
   \endgroup
}

Then use it as:
     \writetobackcontent{\thistheorem}
or
     \writetobackcontent{\the\theoremtoks}

>
> Of course, If I have this mechanism, I can also decide to expand  
> the body of the theorem separately by redefining the environment,  
> and thus I could output

Indeed.
You need to decide which macros you want to expand
as they are written into the file. This can involve deciding
which arguments you want to keep and which to discard
  (e.g., there is no point keeping the \label{...}
   --- or maybe you *do* want it, but different from where
   the same material occurs in the text;
   e.g., use   \def\label##1{\noexpand\label{back-##1}}%
  )

This kind of decision depends very much on exactly what
kind of material is being written into the file,
and what you then want to do with it.

This is where TeX really comes into its own as a powerful
tool for text-processing.


>
>
> Theorem 2.1 on page 715.  The sum of 1 and 2
> is given by
>                     3.                   (5)

To get the "on page 715" you will need to capture
  \pageref{thm:3}
where \label{thm:3} has been used with your theorem.
Some creative environment definitions, having the
\label string as an argument, will allow you to do this.

>
> Does anybody know how to do it?

Play with the above ideas.

Best of luck!

>
>  best
>   Roberto


Cheers,

	Ross

------------------------------------------------------------------------
Ross Moore                                       ross at maths.mq.edu.au
Mathematics Department                           office: E7A-419
Macquarie University                             tel: +61 (0)2 9850 8955
Sydney, Australia  2109                          fax: +61 (0)2 9850 8114
------------------------------------------------------------------------






More information about the macostex-archives mailing list