23

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.

5
  • 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, 2010 at 20:45
  • 2
    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? Aug 26, 2010 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.
    – yannisl
    Aug 26, 2010 at 22:40
  • Relevant: tex.stackexchange.com/a/87014/8666
    – 0 _
    Jul 21, 2017 at 11:52
  • Thanks for all the sharing. I tried some of them and they worked. But after I added path in front of the file to write/read, Overleaf said "I can't write to the file xx". Pls help...
    – farhill
    Mar 16, 2022 at 17:06

4 Answers 4

15

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.

18

You can easily write to a file using

\newwrite\file
\immediate\openout\file=myfilename.txt
\immediate\write\file{A line of text to write to the file}
\immediate\write\file{Another line of text to write to the file}
\closeout\file

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

\newread\file
\openin\file=myfilename.txt
\loop\unless\ifeof\file
    \read\file to\fileline % Reads a line of the file into \fileline
    % Do something with \fileline
\repeat
\closein\file

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.

2
6

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.

0
6

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
\newwrite\tempfile
\immediate\openout\tempfile=keydates.tex


 %% Example
 %% 

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

 %% finally we close the file
 %%
 \immediate\closeout\tempfile

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

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .