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.

(with apologies for the title)

I've just entered the murky waters of trying to get one LaTeX file to write out something useful as it goes along. In brief, I want my lecture LaTeX file to be able to write out a file that, upon LaTeX'ing, will give me the corresponding lesson plan. So at the start of the (main) document I want to write out the header, then as the document progresses, I want to write out bits to go in the plan, then at the end it needs to write out a footer.

What is confusing me is when writes happen. The lecture file consists of lots and lots of lectures (it's a beamer file) and only one is produced upon LaTeX'ing, but one of the pieces of information that I want to write out is the lecture title and I keep getting the last lecture title. I thought that maybe \immediate would be the answer, but that really does mean immediate. What I want is to put the \write in a macro in such a way that when the macro is called, the write is immediate (in particular, I want to hook it in to the lecture title page). So the question is: how do I do that?

I'm probably just missing something obvious, but whatever it is, I'm missing it!

(And for some reason, searching on google for 'latex write' doesn't help much.)

share|improve this question
1  
I heard the rites have something do do with waving a dead chicken above your computer... have you tried that? :P –  Sharpie Aug 14 '10 at 1:34
    
The title is so Donald Knuth-y –  hayalci Oct 3 '10 at 11:02
add comment

3 Answers

up vote 6 down vote accepted

I guess I must not understand the question. What is wrong with:

\newcommand{\mywrite}[1]{\immediate\write\myoutputfile{#1}}

Although I like to be a bit more complete and handle the fact that not all input files may be read on each compilation (due to \includeonly) but you still want the complete lesson plan even if some parts live in the non-included files, also the user maybe want to switch off all file output via \nofiles, or only wants to stop automatically updating the lesson plan so they can do some hand editing (here remove the \makemyrecords line). I would also want to avoid putting spurious spaces in the document, so I would do it this way, modelled loosely after how \makeindex works in latex:

\ProvidesPackage{myrecords}%
          [2010/08/14 v0.01 my example record package (LSB)]
\NeedsTeXFormat{LaTeX2e}

\def\myrec@outputfileextension{.mrc}
\newcommand*{\myrec@write}[1]{}

\def\myrec@verb{\expandafter\strip@prefix\meaning}

\newcommand*{\makemyrecords}{%
    \newwrite\myrec@outfile
    \immediate\openout\myrec@outfile=\jobname\myrec@outputfileextension
    \typeout{Writing my records to file \jobname\myrec@outputfileextension}%
    \let\makemyrecords\@empty
    \renewcommand*{\myrec@write}[1]{\immediate\write\myrec@outfile{##1}}%
}
\@onlypreamble\makemyrecords

\newcommand*{\myrecord}[1]{%
    \@bsphack
    \if@filesw
        \immediate\write\@auxout{\string\myrecordentry{#1}}%
    \fi
    \@esphack
}

\newcommand*{\myliteral}[1]{%
    \@bsphack
    \if@filesw
        \def\@tempa{#1}
        \immediate\write\@auxout{%
            \string\myrecordentry{%
                \myrec@verb\@tempa}}%
    \fi
    \@esphack
}

\newcommand*{\mywritecurrenttitle}{
    \if@filesw
        \immediate\write\@auxout{\string\myrecordentry{%
            \string\lessonplanitem{\currenttitle}}}%
    \fi
}

\newcommand*{\myrecordentry}[1]{}
\AtEndDocument{%
    \renewcommand*{\myrecordentry}[1]{%
        \def\@tempa{#1}%
        \myrec@write{\myrec@verb\@tempa}%
}}

\endinput

If I test this with what I imagine you have in mind (obviously you would want to wrap the \renewcommand{\currenttitle}\mywritecurrenttitle up with a command that generates the document sectioning, or put them in hooks provided by your document class).

\documentclass{article}
\usepackage{myrecords}
\makemyrecords
\begin{document}
\myliteral{
    \documentclass{lessonplan}
    \begin{document}}
\newcommand*{\currenttitle}{This is the title of lesson 1}
\mywritecurrenttitle
Lesson 1....

\renewcommand{\currenttitle}{This is the title of lesson 2}
\mywritecurrenttitle
Lesson 2.....

\myliteral{\end{document}}
\end{document}

Then, as expected, the lesson plan file looks like

\documentclass {lessonplan} \begin {document}
\lessonplanitem {This is the title of lesson 1}
\lessonplanitem {This is the title of lesson 2}
\end {document}
share|improve this answer
    
This is great! Thanks. With a little tweaking to take into account the details I didn't tell you about, it's now a working solution. (Basically, I can't use the aux file route because I want the lesson plan to be generated when \nofiles is in effect. But it was simple to edit that out.) –  Andrew Stacey Aug 16 '10 at 12:39
    
A follow-up: I'm now using this "in the wild" and it's fantastic. I now scatter my beamer presentation with the lesson plan stuff, plus a few things are added automatically, and my lesson plan gets written out into a separate file which I can TeX to get a very nice-looking plan. Thanks again! –  Andrew Stacey Aug 27 '10 at 17:14
add comment

Can you not just use \let to save the current title, and use it later to write to a file? Another alternative would be to always use \immediate so that the chronology of typesetting is respected.

share|improve this answer
    
This is, as far as I can tell, pretty much what Lev's answer does. For some reason, when I tried simply putting in \immediates in front of the \writes then it wasn't doing what I expected. –  Andrew Stacey Aug 16 '10 at 12:38
add comment

One solution is to use filecontents (CTAN).

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.