I want to compile a bunch of (independent) documents from a main.tex file. The documents are in subfolders and as they can load input files and graphics from other subfolders I need to change the current directory. I think this can't be done inside a \write18 call so I'm writing first a .bat-script and call this later. I would like to adapt the code so that is works on linux too, but as I don't have a linux system available I can't test it. Could someone tell me how this should look on linux? (You need folders a1 and a2 with a1.tex and a2.tex in them.)



\immediate\write\writebatch{cd a1}
\immediate\write\writebatch{pdflatex a1}
\immediate\write\writebatch{cd ..}
\immediate\write\writebatch{cd a2}
\immediate\write\writebatch{pdflatex a2}
\immediate\write18{copy compile-files.txt compile-files.bat}
%compile-files.bat will probably be called later  on the command line, is here for 
%the test. Needs --shell-escape. 

  • If an external tool is acceptable, I could provide an answer with arara. :) May 4, 2014 at 9:38
  • @PauloCereda: The requirements are 1. it should work without fuss with miktex and texlive. 2. The list of documents is created by the main document (and can change). Also the main document will pass informations to the other documents, e.g. pdflatex \def\startnumber{50}\input{a1}. 3. The main document at the end should include the pdfs. May 4, 2014 at 9:53

1 Answer 1


I'd use subshells:



  (cd a1; pdflatex a1)^^J%
  (cd a2; pdflatex a2)^^J%
\immediate\write18{sh compile-files.sh}

  • Does the subshells do more than saving the need to use cd ..? And is the exit needed? (If not I could use the same file content on both system and only change the extension.) May 4, 2014 at 9:02
  • @UlrikeFischer It's not necessary, but cleaner; exit is a best practice in shell scripts, but not mandatory. You still have the problem of copy versus cp, in any case.
    – egreg
    May 4, 2014 at 10:18
  • Well the copy line is different anyway due to the different extensions (and it is not part of the batch script). From your example it looks as if it is not necessary on linux, on windows I needed it as the system wouldn't allow me to write a bat directly. May 4, 2014 at 10:49
  • maybe you could use the "ifplatform" package May 14, 2014 at 18:41

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.