W3cubDocs

/LaTeX

\read

Synopsis:

\read number tomacro

Make the command macro contain the next line of input from text stream number, as in \read5 to\data.

This opens the file email.tex for reading, puts the contents of the first line into the command \email, and then closes the file.

\newread\recipientfile
\openin\recipientfile=email
\read\recipientfile to\email
\typeout{Email address: \email}
\closein\recipientfile

If number is outside the range from 0 to 15 or if no file of that number is open, or if the file has ended, then \read will take input from the terminal (or exit if interaction is not allowed, e.g., \nonstopmode; see interaction modes). (However, the natural way in LaTeX to take input from the terminal is \typein (see \typein.)

To read an entire file as additional LaTeX source, use \input (see \input) or \include (see \include & \includeonly).

A common reason to want to read from a data file is to do mail merges. CTAN has a number of packages for that; one is datatool.

© 2007–2018 Karl Berry
Public Domain Software
http://latexref.xyz/_005cread.html