TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Are there any LaTeX packages that can assist with reading and writing to files? I am trying to develop a solution to the following problem:

1) I am producing a long report regularly. Part of the report include items like

   Key date: 12 Sep 2010    Complete Floor 1
   Key date: 18 Sep 2010    Other activity

2) It will be nice to have all these dates saved into a sort of an aux file and then import later as a section of its own. It will also need to be sorted.

I have so far looked at the answers package for inspiration and also thought of hacking the index mechanism. Any ideas and pointers would be greatly appreciated.

share|improve this question
I wrote an answer about reading and writing files, but maybe if you describe more of what you're trying to accomplish, someone can provide a more targeted answer. – TH. Aug 26 '10 at 20:45
Are you absolutely sure that you want to do this in Latex? What about writing a simple script (in Python, Perl, or whatever scripting language you prefer) that reads your report and generates Latex code? – Jukka Suomela Aug 26 '10 at 20:54
Since the data is generated while someone is typing sections of the report, it makes sense to attempt to do this with \TeX. This way data can be entered via a macro, e.g. \kdate{12/09/2010}{Complete Floor 1}. The file operations would just summarize all these instances. – Yiannis Lazarides Aug 26 '10 at 22:40
up vote 11 down vote accepted

Sounds like the perfect task for the datatool package. It is design to create "databases" in TeX, which can then be saved to an external file and then read in later. Lots of useful data manipulation commands, as well.

share|improve this answer

You can easily write to a file using

\immediate\write\file{A line of text to write to the file}
\immediate\write\file{Another line of text to write to the file}

You can read from a file a line at a time in a similar way.

    \read\file to\fileline % Reads a line of the file into \fileline
    % Do something with \fileline

Edit: Note that \unless is an e-TeX extension. \readline is another useful extension. It reads the line and assigns each character a category code of other (12) or space (10). This is true even of the end of line character.

share|improve this answer
The second example contains a bug. \read\file to \fileline should be invoked before \unless\ifeof\file. I tested your answer several hours ago, now I have changed the order, see my answer. – xport Aug 8 '11 at 23:27
loop and repeat are also extensions / macros: tex.ac.uk/ctan/info/latex2e-help-texinfo/latex2e.html – Ciro Santilli 巴拿馬文件 六四事件 法轮功 Aug 19 '14 at 8:13

If some of your report data is table like, perhaps the pgfplotstable package is an option. It makes it really easy to read in (parts) of delimited text files and typeset the data as nice tables.

share|improve this answer

I tried all the answers given so far - except I did not spent too much time on the datatool mentioned by Will Robertson. At the end I decided to hack a simple solution which is shown below:

 %% Define a new command for activity key-dates
 %% this can be saved for shipout later
 \newcommand{\keydate}[2]{#1  #2 \\}
 \newcommand{\out}[2]{\immediate\write\tempfile{\noexpand\keydate{ #1}{#2}}}

 %% We open the file to  to write the key-dates
 %% we will use it later to import

 %% Example

\out{Activity 1}{10 Sep 2010}
\out{Activity 2}{12 Sep 2010}
\out{Activity 3}{13 Sep 2010}

 %% finally we close the file

 %% we now read the file and dostuff with it
 \section{Summary of  Key Dates}

This quick hack did the job. On the next version, I will try using the datatool in order to be able to manipulate the data more extensively.

share|improve this answer

Your Answer


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.