In the directory /user/, I have a file header.tex.

In the file /user/dir/main.tex, I use \input{../header.tex}.

When I run pdflatex main.tex from /user/dir, everything works fine. But if I runpdflatex dir/main.tex from /user, I get the File ../header.tex not found error.

Is it possible to define input with respect to the file on which pdflatex is run (here, /user/dir/main.tex), so that pdflatex would work irrespective of the folder from which it is ran?

  • 1
    no it is built in to tex that input and default output are relative to the working directory not the directory with the file but you can always write a wrapper script that takes a file path, cds to the file director and then runs pdflatex, or just use \input{header} and arrange that /user is in TEXINPUTS then it will work from anywhere just as article.cls gets input wherever you are as it is in TEXINPUTS – David Carlisle Nov 7 '17 at 23:13
  • @DavidCarlisle Thank you, I wrote an answer based on your comment. – anderstood Nov 8 '17 at 18:20

This answer is elaborated from David Carlisle's comment, in case it helps someone. Since \input cannot be changed in such a way, there are basically two strategies:

  • Let latex now about the file globally, ideally by adding header.tex to ~/texmf/tex/latex/<username>, or to the variable TEXINPUTS. More details on this answer. In my case, I wanted my files to be self-contained in a single directory (for git), so I went with the second option.

  • Write a script that goes into each directory, run pdflatex and so on. This is the option I used.

First, I generate a list of all tex files and store if in listOfTeX.txt (to be ran from the parent directory):

find . -type f \( -iname "$.tex" \) >> listOfTeX.txt


while read FILE; do 
    printf "\n ----> $FILE\n"; # prints the file path and name
    DIR=$(dirname "$FILE");    # extracts the directory
    cd $DIR;                   # goes to that directory
    texfot pdflatex $(basename "$FILE") | grep "error";  # run pdflatex
    cd ../;                 # goes back to the parent directory (in my case)
done < listOfTeX.txt

Explanations on the line texfot pdflatex $(basename "$FILE") | grep "error"; : texfot is used to reduce the output of pdflatex, see egreg's answer. Then I use grep "error" to display only the errors. That allows to directly find the problematic files and paths. To be adjusted to your needs of course (same for cd ../).

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.