3

The minimal working example could not be simpler: I have a main.tex file like so:

\documentclass{article}

\usepackage{subfiles}

\begin{document}
    \subfile{Subfiles/subfile}
\end{document}

and a subfile inside a subdirectory (Subfiles/subfile):

\documentclass[../main]{subfiles}

\begin{document}
    Hello, world!
\end{document}

When I compile main.tex using the LaTeX-Workshop VSCode extension, everything goes well and I get the result I expect. However, with the subfile it is a bit more complicated: At first, it does work. But then after another compile, it suddenly fails to find ../main.tex. If I then change ../main.tex to just ./main.tex (which does not seem to make sense), it works again for one or a couple compiles and then I have to change ./main.tex back to ../main.tex again to make it work. I am not sure what to think of this semi-reliable behaviour. This is the error:

Latexmk: Missing input file '../main.tex' (or dependence on it) from following:
  ! LaTeX Error: File `../main.tex' not found.
4
  • Did you change directory to Subfiles before compiling subfile.tex? Alternatively did you use the -cd option to latexmk? Dec 25, 2022 at 15:57
  • I guess this is an VSCode issue, it getting confused about the file to compile and doing so from which directory. When you try to compile Subfiles/subfile.tex from the directory where main.tex is (e.g. by pdflatex Subfiles/subfile), then LaTeX will complain that it does not find ../main.tex, but will work if you change ../main to main in the first line of Subfiles/subfile.tex. If you first change into the Subfiles folder and then do pdflatex subfile, then ../main will work, but not main. As @JohnCollins said: You/your tool has to change to Subfiles first ...
    – gernot
    Dec 25, 2022 at 18:15
  • Adding the -cd option to the compile config solved the issue. Regardless, it was most likely a VSCode issue as gernot said. (Or at least I would suspect because of the unreliablity)
    – Jan Matula
    Dec 25, 2022 at 19:09
  • Maybe you, Jan, or @JohnCollins might want to write an answer that Jan then can accept, such that other people with the same problem find the solution here? Thanks ...
    – gernot
    Dec 26, 2022 at 20:47

1 Answer 1

2

Use the option -cd to latexmk when compiling subfile.tex. This tells latexmk to change directory to the directory containing subfile.tex before compiling it. Then the line \documentclass[../main]{subfiles} is interpreted as intended: the file main.tex is in the parent directory relative to where subfile.tex is.

If you don't use the -cd option, and you (or VSCode) don't set the working directory to that of subfile.tex, then the compilation will look for main.tex in the parent directory of the working directory rather than the correct place.

1
  • How do I set this option? I have googled for a long time, unable to find an awnser.
    – KGB91
    Feb 7 at 12:48

You must log in to answer this question.

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