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

What is the command for file copy? I want to copy the contents of one file into another without any command expansion. In a command line driven operating system one would use

copy source target

I tried reading the contents of one file into a variable and the writing it into the target:


but it expands the TeX commands saved in the original file.

share|improve this question
While Hector knows it, for others I wanted to note that \CatchFileDef is from the ctan.org/pkg/catchfile package. – Stephen Nov 11 '11 at 18:28
up vote 16 down vote accepted

Here's a Plain TeX solution that uses the ε-TeX extension \readline. It copies its source to \jobname.copy.

\loop \unless\ifeof\in
        \readline\in to\l
share|improve this answer

You were very close in your original question; just replace




and I think you'll get what you're after.

share|improve this answer

The verbatimcopy package provides the command \VerbatimCopy which seems to do exactly what you are looking for. It doesn't seem to work for "binary" (i.e., non-text) files, though.




  % This doesn't work for binary files:

I have added a compilable example to GitHub.

share|improve this answer

This one comes fairly close. The trick is to make every character non-special while copying.

    \advance\count255 1
    \ifnum\count255<256 \repeat}


    \read\infile to\data


One remaining problem: any control character in the input will be copied to its TeX equivalent, so a backspace character becomes ^^H. I have no fix for that.

share|improve this answer
But honestly, I find the stated requirement somewhat bizarre. Why use TeX to copy files when every operating system does it better? – Harald Hanche-Olsen Jan 17 '11 at 17:45
File A was created at the beginning of the process and contains some TeX code. File A is copied to file B at the end of the TeX compilation. In the second run, I use file B to generate a "table of contents". If the compilation halted, then file B was not copied and the "table of contents" is not generated (this is what I want). If I use only one file in this work flow, then there is no way (that I know of) to prevent an incomplete file A to produce the incorrect input for the second run. – Hector Jan 17 '11 at 17:56
you might be interested in the answers to tex.stackexchange.com/questions/8729/… – Bruno Le Floch Jan 17 '11 at 18:21
@Hector: How about using a \newif\ifsuccess, and then at the end (where you would otherwise do 'copy fileA fileB') put \addtocontents{aux}{\successtrue}. You can then use \ifsuccess\input{file}\else No ToC\fi (or whatever). – Villemoes Jan 18 '11 at 14:11

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.