# How to adapt a batch-script / write18-call to Linux systems

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.)

\documentclass{article}

\begin{document}

\newwrite\writebatch
\immediate\openout\writebatch=compile-files.txt
\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\closeout\writebatch
\immediate\write18{copy compile-files.txt compile-files.bat}
\immediate\write18{compile-files.bat}
%compile-files.bat will probably be called later  on the command line, is here for
%the test. Needs --shell-escape.

\end{document}

• If an external tool is acceptable, I could provide an answer with arara. :) – Paulo Cereda May 4 '14 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. – Ulrike Fischer May 4 '14 at 9:53

I'd use subshells:

\documentclass{article}

\begin{document}

\newwrite\writebatch
\immediate\openout\writebatch=compile-files.sh
\immediate\write\writebatch{%
(cd a1; pdflatex a1)^^J%
(cd a2; pdflatex a2)^^J%
exit
}
\immediate\closeout\writebatch
\immediate\write18{sh compile-files.sh}

\end{document}

• 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.) – Ulrike Fischer May 4 '14 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 '14 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. – Ulrike Fischer May 4 '14 at 10:49
• maybe you could use the "ifplatform" package – Pablo González L May 14 '14 at 18:41