9

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.
lpdbw
  • 8,330
  • 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
  • 7
    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
  • @ Peter Grill: Well, I did take notice of that post but I am not sure whether it really is a duplicate ... My case is much simpler since I just want to produce two identical copies of the same file. I thought there might possibly be a simple solution for that. The accepted answer in the other post would of course work here but it means having two main files. I wwas wondering whether it is possible with just one main file. – lpdbw Sep 26 '12 at 20:08
  • @PeterGrill I don't agree that this is a duplicate. I'm sure that some answers for this question need not to work for the other one, and there is no answer in the other one that would satisfy this one. – yo' Sep 26 '12 at 20:13
  • @tohecz: Wouldn't Ulrike Fischer's answer work here? But even if there is not an existing answer that solves this problem, I think that the answers should be added to the other question. – Peter Grill Sep 26 '12 at 20:17
  • See my answer. It has been tested and works very well! – kiss my armpit Sep 26 '12 at 20:19
  • @Ipdbw: I know you said not to ask why you need this, but it is kind of intriguing... – Jake Sep 26 '12 at 20:20
  • 2
    As per http://meta.tex.stackexchange.com/questions/2770/closing-questions-as-exact-duplications-best-practice, 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
  • What does the ! mean in your "identical (!)"? Should they be 100% identical including the creation date and time? – kiss my armpit Sep 26 '12 at 22:02
  • 2
    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
  • With identical I only mean identical in content and appearance. Creation date and time need to be the same! – lpdbw Sep 27 '12 at 10:13

6 Answers6

7

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
nameaddon = xy
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.

N.N.
  • 36,163
5

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.

enter image description here

3

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.

Qrrbrbirlbel
  • 119,821
  • 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. – yo' 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
  • I'm not sure about the possiblity to change the default setting. The experience says: the more carefulness the better! – yo' Sep 26 '12 at 20:23
  • The request was identical files. PDF files, generated by pdfTeX contains some randomness (ID values, font name prefixes). Also the status of the auxiliary files is quite difficult to handle. And if the document contains \today and the second document is generated a few seconds later right after midnight, … – Heiko Oberdiek Sep 26 '12 at 20:42
  • @tohecz The "inner" run will not execute the \write18 operation, unless the restrictions on shell-escape have been removed from texmf.cnf – egreg Sep 26 '12 at 20:52
  • @HeikoOberdiek My first idea was \write18{copy …} but that failed for obvious reason (PDF not finished). I did not think of \AtEndDocument which kind of works (it copies the pdf from the first run). – Qrrbrbirlbel Sep 26 '12 at 20:56
  • @HeikoOberdiek The only way to get two identical files (apart from the file system timestamps) is copying: do you agree? – egreg Sep 26 '12 at 20:57
  • 1
    @egreg Copying and hard links. – Heiko Oberdiek Sep 26 '12 at 21:11
  • 1
    @HeikoOberdiek The hard link is cheating. :) Besides you can't have them on a well known OS. – egreg Sep 26 '12 at 21:13
  • 1
    @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
3

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.

  • 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 ;)
  • – yo' 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. – kiss my armpit Sep 26 '12 at 20:26
  • @ガベージコレクタ: Ah! Okay, I understand. Nice idea! – Jake Sep 26 '12 at 20:28