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

I want to create a new temporary file (named main-temp.tex), append data to it multiple times, and import the contents later. I haven't learnt how to use TeX file operation, both reading and writing.

In this question, I think I need writing operation only because reading data from a file line by line is not needed in my scenario below. Thus using \input{} is enough to read all data.

% main.tex


    % code to append #1 to a shared temporary file named main-temp.tex

\newcommand\AddQuestion[2]% #1 and #2 stand for question and answer, respectively
  % code to markup  question #1
  % for example
  % code to append answer #2

% Add some questions:
\AddQuestion{Are you kidding?}{No. I am not a joker.}

% imports answers from the shared temporary file named main-temp.tex


How to append data to a temporary file? I know using --enable-write18 switch is mandatory when invoking latex. I don't want to use the existing package such as exercise, etc.

share|improve this question
You can't append to an existing file using TeX, except by reading it in first and writing it out again with the new material attached. I think we had a question for this already. Searching ... – Martin Scharrer Jul 17 '11 at 9:03
Duplicate of How can I open a file in “append” mode?. Please confirm. – Martin Scharrer Jul 17 '11 at 9:04
On second thought: Do you need to share this file across different documents? Or do you just want to accumulate material in an external file and read it in again like e.g. ToC? Then however it would look like a duplicate of What is the basic mechanism for writing something to an aux file?. [I had to undelete this question to add this comment.] – Martin Scharrer Jul 17 '11 at 9:23
Sure, no problem. If you need more help, then just update this question instead of deleting it. – Martin Scharrer Jul 17 '11 at 9:25
TeX can't append to an already existing file. It can open a new file (or overwrite an existing file) and write multiple times to it. Your title and text suggested to me that this file is shared between multiple documents or one document and an external application. – Martin Scharrer Jul 17 '11 at 10:32
up vote 11 down vote accepted

The basic code is as follows.




\AddQuestion{Are you kidding?}{No. I'm not a joker.}


You are responsible for adding formatting directives to the answers file. You might profit from the \unexpanded feature, to avoid expanding commands during the \write operation:


is a good way, but probably you'll need a mix of expanded and unexpanded writes: the reference number, for example, would need to be expanded. So, say you have a counter that keeps track of the questions



after having defined \AddQuestion to also increment the question counter.

The final ^^J means that each answer will be followed by an empty line.

share|improve this answer
Indeed this answer is cleaner than the existing answers. Thanks for posting it! – Martin Scharrer Jul 17 '11 at 10:33

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.