0

I have a question that is very similar to the one described here: How to create individual chapter PDFs from included TeXs?

I want to create individual PDF-files (chap01.pdf, chap02.pdf, ...) from multiple TeX-Files (chap01.tex, chap02.tex, ...), which are included in my maintex.tex file. However, the main difference is that my main.tex file is located in my main project directory (e.g. /project), whereas the article-files are located in a subdirectory (e.g. /project/heft1-2019).

\documentclass[a4paper]{report}
\usepackage[utf8]{inputenc} % Required for including letters with accents
\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage[ngerman]{babel}

\begin{document}
\include{heft1-2019/chap01}
\include{heft1-2019/chap02}
\include{heft1-2019/chap03}
\end{document}

I tried different solutions, but for me the most elegant seems to be the bash command from egreg: https://tex.stackexchange.com/a/31366/192099

for i in chap*.tex; do j=${i%.tex}; pdflatex -jobname=the$j "\includeonly{$j}\input{maintex}"; done

If all tex files are in the same directory this bash command works wonderfully.

Since the tex files of the articles are in a subdirectory, compiling obviously does not work.

How do I rewrite the bash command so that I can run it from the main project directory and create the PDFs in the subdirectory (heft1-2019), where the article files (chap01.tex, chap02.tex, ...) are located?

1 Answer 1

1

Here are two ways:

LaTeX run from the base directory

% maintex.tex
\documentclass{report}

\begin{document}
\tableofcontents
\include{heft1-2019/chap01}
\include{heft1-2019/chap02}
\include{heft1-2019/chap03}
\end{document}
% heft1-2019/chap01.tex
\chapter{First chapter}

Foo bar.
% heft1-2019/chap02.tex
\chapter{Second chapter}

Baz.
% heft1-2019/chap03.tex
\chapter{Third chapter}

Quux.

Bash command run from the directory containing maintex.tex:

for i in heft1-2019/chap*.tex; do j="${i%.tex}"; base="$(basename "$j")"; pdflatex -jobname="$base" "\includeonly{$j}\input{maintex}"; if [ -f "${base}.pdf" ]; then mv "${base}.pdf" heft1-2019; fi; done

When you want to compile the whole:

pdflatex maintex.tex

With this method, paths of included files are relative to the directory containing maintex.tex.

LaTeX run from the subdirectory

% maintex.tex
\documentclass{report}

\begin{document}
\tableofcontents
\include{chap01}
\include{chap02}
\include{chap03}
\end{document}

and heft1-2019/chap01.tex, heft1-2019/chap02.tex, heft1-2019/chap03.tex as above. Bash command still run from the directory containing maintex.tex (as per your question):

(cd heft1-2019 && for i in chap*.tex; do j="${i%.tex}"; pdflatex -jobname="the$j" "\includeonly{$j}\input{../maintex}"; done)

When you want to compile the whole:

(cd heft1-2019 && pdflatex ../maintex.tex)

With this method, paths of included files are relative to the heft1-2019 subdirectory, since LaTeX is run from there.

3
  • Thank you very much, frougon! This is a wonderful solution. Building on this, I have written a bash function which can be applied to a subdirectory with any name: createpdfs() { for i in $1/*.tex; do j="${i%.tex}"; base="$(basename "$j")"; pdflatex -jobname="$base" "\includeonly{$j}\input{maintex}"; if [ -f "${base}.pdf" ]; then mv "${base}.pdf" $1; fi; done } Commented Jan 17, 2020 at 9:09
  • Glad to hear that. You may want to use "$1" instead of $1 in case your subdirectory name contains spaces.
    – frougon
    Commented Jan 17, 2020 at 9:11
  • Thank you for the advice! Commented Jan 21, 2020 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .