# Automatically create two PDF output files from one tex file [duplicate]

Possible Duplicate:
Can one TeX file output to multiple PDF files?

Let's say I have a tex file foo.tex. Now, I want to achieve that pdflatex does not only create a file foo.pdf but simultaneously a file fooxy.pdf which is identical (!) to foo.pdf. Is there a simple solution to this problem?

PS:

• Yes, I could just do this by copying and renaming the file in the file manager. But I want to have an automatic solution.
• No, I am not crazy. Please don't ask me why I want to do this.
-

## marked as duplicate by Peter Grill, N.N., Marco, Harish Kumar, Paul GaboritSep 27 '12 at 5:57

this would be very easy with a perl or bash script; but as detailed in how-to-influence-the-name-of-the-pdf-file-created-with-pdflatex-from-within-the‌​ it is not possible to influence the name of \jobname.pdf from within the .tex file –  cmhughes Sep 26 '12 at 20:00
pdflatex foo.tex && cp foo.pdf fooxy.pdf –  N.N. Sep 26 '12 at 20:00
@N.N. That's pratcically what I meant with doing it in the file manager ... –  lpdbw Sep 26 '12 at 20:06
As per meta.tex.stackexchange.com/questions/2770/…, I think we should wait a bit before closing as a dupe. (I'm not certain it is, anyway, but that's a separate point.) –  Joseph Wright Sep 26 '12 at 20:49
I'm afraid you can't generate identical bitwise level output files from pdflatex, unless a modification in the program source code to flush data to two output streams at the same time. Even by running pdflatex on the same .tex file will produce different checksums every time. The only way to garantee a 1:1 copy is, well, by copying the target .pdf file. –  Paulo Cereda Sep 26 '12 at 22:35

I can think of three ways to approach this:

## &&

You could just add a copy command after pdflatex as in the following:

pdflatex foo.tex && cp foo.pdf fooxy.pdf


The does everything you want in one line and it is a simple solution.

## Make

You could make it slightly more sophisticated by making a Makefile such as the following:

namebase = foo
tex = pdflatex          # Might wanna set this to latexmk

.PHONY : all
all : $(namebase)$(nameaddon).pdf

$(namebase)$(nameaddon).pdf : $(namebase).pdf cp$< $@$(namebase).pdf : $(namebase).tex$(tex) \$<


This gives you more flexibility when it comes to naming file and it does not compile or copy unless it is needed. You only have to issue

make all


and make decides what needs to be done.

## tee

This does not work but was the first approach I came to think about. The command tee reads from standard input and writes to standard output and the files given as arguments. If pdflatex would write the pdf to standard output you could have used

pdflatex foo.tex | tee foo.pdf fooxy.pdf > /dev/null


but as pdflatex does not this approach fails.

-

## Only for Microsoft Windows users.

It has been properly tested in my daily jobs and works.

rem this file name is pdflatexdup.bat

rem first arg specifies the master input filename without extension
rem remaining args specifies a list of duplicate filenames

rem remove the previously created master PDF
if exist "%~1.pdf" del "%~1.pdf"

rem create master PDF
if exist "%~1.tex" pdflatex -draftmode -interaction=batchmode "%~1.tex"
if exist "%~1.tex" pdflatex -draftmode -interaction=batchmode "%~1.tex"
if exist "%~1.tex" pdflatex -draftmode -interaction=batchmode "%~1.tex"
if exist "%~1.tex" pdflatex "%~1.tex"

rem remove unnecessary files
for %%x in (aux log out toc nav snm) do (if exist "%~1.%%x" del "%~1.%%x")

rem save the master file name
set master=%~1
shift

:loop
if "%~1"=="" goto :eof
copy "%master%.pdf" "%~1.pdf"
shift
goto :loop

rem remove the master PDF if you want
rem if exist "%master%.pdf" del "%master%.pdf"


## How to use:

Compile your foo.tex with the following command typed in the CMD window.

pdflatexdup foo a b c d e f g


where a, b, c, ... g are the duplicate file names. After compilation you will get 8 99.99% identical PDF files (including the master PDF).

If you duplicate names contain spaces then you need to enclose it with quotes as follows:

pdflatexdup foo a b c d e f g "garbage collector"


In this case you get 9 PDF files as follows.

-

The questioner stated in a comment that they use Windows.

My solution contains of two files (see below).
Inside the TeX file the batch file is run with the start command. This issues a second process so that pdfLaTeX "thinks" that the \write18 macro already ended and so that it finishes the PDF creating process without waiting for the copy process (which would copy an outdated or non-existent PDF).

## Files

### foo.tex

\documentclass{article}
\AtEndDocument{\immediate\write18{start "write18 call" /MIN kopie.bat \jobname\space xy}}%
\begin{document}
Hello real copy!
\end{document}


### kopie.bat

@ECHO OFF
ping 127.0.0.1 /n 2
copy %1.pdf %1%2.pdf
exit


## Explanation

The ping command is simply to pass some time. The amount of pings that is needed for LaTeX to finish the pdf is empiric for this document. I expect for a big document that you need more time. (Windows 7 offers timeout /T 2 for a two-second wait.)
There is probably also a way to wait for the pdflatex process to exit.

\jobname is foo (for the batch file it will be saved in %1). xy stands for the variable suffix you want to add (could be macro, too).

If you want to copy foo to moo you can edit the copy command to:

copy %1.pdf %2.pdf


## Note

If you need to use bib(la)tex, makeindex or something of the sort you are better off using make, an advanced batch script or you tweak your editor's build chain.

-
cmd /c is more convenient than start because you don't need to close the DOS window manually. –  Who is crazy first Sep 26 '12 at 22:17
@ガベージコレクタ cmd /c doesn't start a new instance (cmd /c start … does, though), so we're back two square one. I also found the switch /B for start and exit but I was still in some weird mode (blank line) or in the pdflatex process. –  Qrrbrbirlbel Sep 27 '12 at 23:57

You can run LaTeX inside LaTeX when you run it as pdflatex -shell-escape (MikTeX):

\documentclass{article}
\write18{pdflatex --jobname="\jobname xy" \jobname}
\begin{document}
Hello Copy!
\end{document}


This has the advantage that the second run can be slightly different, for example another page layout.

-
Just be careful, because the inside pdflatex will see \write18 again and call another pdflatex etc. etc. So maybe adding some if clause that will not call it again if \jobname=...xy. –  tohecz Sep 26 '12 at 20:06
@tohecz I thought of this myself. But unless the \write18 contains -shell-escape is there any risk? –  Qrrbrbirlbel Sep 26 '12 at 20:07
@egreg Copying and hard links. –  Heiko Oberdiek Sep 26 '12 at 21:11
@HeikoOberdiek The hard link is cheating. :) Besides you can't have them on a well known OS. –  egreg Sep 26 '12 at 21:13
@egreg Windows: mklink /H fooxy.pdf foo.pdf. (But also the file system should support hard links and the hard links cannot cross file systems.) –  Heiko Oberdiek Sep 26 '12 at 21:17

It has been tested many times in my daily projects. Compile it with pdflatex --shell-escape executor.tex. And you will get 5 PDF files: 4 identical PDF files (a.pdf, b.pdf, c.pdf, d.pdf) and executor.pdf that can be regarded as a log file.

% this file name is executor.tex
% compile it with pdflatex -shell-escape executor.tex

\documentclass{minimal}

\usepackage{filecontents}

\begin{filecontents*}{foo.tex}
\documentclass{article}
\usepackage[a6paper,margin=3cm]{geometry}
\begin{document}
\LaTeX\ is fun!
\end{document}
\end{filecontents*}

\makeatletter
% make a master
\immediate\write18{pdflatex foo.tex}
\@for\ext:={tex,dvi,ps,log,aux,out,toc,nav,snm}\do{\immediate\write18{cmd /c del foo.\ext}}%
% make multiple copies
\@for\filename:={a,b,c,d}\do{\immediate\write18{cmd /c copy foo.pdf \filename.pdf}}
\makeatother

\begin{document}
Done!
\end{document}


## Explanation:

The executor.tex is an auxiliary input file to automate the process of compiling the foo.tex for multiple output names (a.pdf, b.pdf, c.pdf, d.pdf).

## Disclaimer:

The multiple copies are not guaranteed to be 100% identical because the time stamp (creation date time, etc) for each file might not be identical.

-
1) You're mising 2nd dash in front of jobname=. 2) Isn't it a windows-only solution? If so, maybe add a comment about it ;) –  tohecz Sep 26 '12 at 20:22
Won't this generate one PDF that says "LaTeX is fun" and one that produces "Done"? –  Jake Sep 26 '12 at 20:23
@tohecz As it stands, yes, but you could use ifplatform to sort that. –  Joseph Wright Sep 26 '12 at 20:23
@Jake: The executor.tex is an auxiliary input file to automate the process of compiling the foo.tex for multiple output names. –  Who is crazy first Sep 26 '12 at 20:26
@ガベージコレクタ: Ah! Okay, I understand. Nice idea! –  Jake Sep 26 '12 at 20:28

Within LaTeX in a linux system could be:

% Need --enable-write18 or --shell-escape
\immediate\write18{cp foo.pdf fooxy.pdf}


Or instead of cp (copy) you can call any other command (a script running pdflatex several times, for example).

Addendum: In a Windows system could be (not tested):

\immediate\write18{foo.bat}


Where foo.bat is a simple text file with .bat extension that contain the following text:

pdflatex foo.tex
copy foo.pdf fooxy.pdf
del foo.aux
del foo.log


But this have no sense, because then the simplest way is to execute the batch file alone:

C:\SOMEDIR> foo

-
And where do you put this? Because LaTeX truncates the pdf fale at the beginning of the compilation, so foo.pdf contains just some mess when you call this! –  tohecz Sep 26 '12 at 20:09
I use Windows ... –  lpdbw Sep 26 '12 at 20:14
In the preamble (obviously need n+1 runs that with a single copy). I left to imagination that the command ("any other command") could be the pdflatex itself. –  Fran Sep 26 '12 at 20:19
@lpdbw, Windows also used to have a copy command (copy), but the important thing is that you can execute any windows command, including a custom foo.bat that do this and three or four more things). –  Fran Sep 26 '12 at 20:25