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.

  • 3
    While Hector knows it, for others I wanted to note that \CatchFileDef is from the ctan.org/pkg/catchfile package.
    – Stephen
    Nov 11, 2011 at 18:28

4 Answers 4


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

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.


You were very close in your original question; just replace




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


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.

  • 10
    But honestly, I find the stated requirement somewhat bizarre. Why use TeX to copy files when every operating system does it better? Jan 17, 2011 at 17:45
  • 2
    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, 2011 at 17:56
  • you might be interested in the answers to tex.stackexchange.com/questions/8729/… Jan 17, 2011 at 18:21
  • 1
    @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, 2011 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.