2

I've the following project structure:

./main.tex
./ch1/ch1.tex
./ch1/tikz1/tikz1.tex

The main.tex looks like:

\documentclass{book}
\begin{document}
\subfile{./ch1/ch1.tex} 
\end{document}

The ch1.tex looks like:

\documentclass[../main.tex]{subfiles}
\begin{document}
\begin{figure}[H]
\centering
\subfile{./tikz1/tikz1.tex}
\end{figure}
\end{document}

When I run the ch1.tex everything is ok, it finds the tikz1.tex, but when I run the main.tex it can't find the tikz1.tex.

Andreas K.
  • 613
  • 1
  • 6
  • 9

2 Answers2

1

In addition to @MarcoDaniel's observation that \usepackage{subfiles} was missing from the main.tex file, there is a problem with how you compile the documents because of relative paths.

When you compile main.tex, latex looks in <maindir>/ch1/ for ch1.tex and in <maindir>/tikz1/ for tikz1.tex. When you compile ch1.tex (likely from its containing directory <maindir>/ch1/), latex looks in <maindir>/ch1/tikz/ for tikz1.tex. Clearly, both of these cannot be correct, unless you maintain two copies of tikz1.tex which sort of defeats the purpose.

There are a number of ways to solve this:

  1. Flatten the directory structure (might be OK depending on number of files)
  2. Use absolute paths (not recommended for portability reasons)
  3. Input ./ch1/ch1.tex to pdflatex run from <maindir> when compiling ch1.tex on its own. Depending on the editor you use, there may be ways to set this up for files other than the main file.
Paul Gessler
  • 29,607
0

I had a similar issue. It used to compile but not on my new system.

I had a main.tex and a Subfolder/firstChapter/subDoc.tex. The file subDoc.tex has a tikz subfile included with the path Subfolder/anotherFolder/tikz.tex.

To compile the main.tex I had to use the relative path from subDoc.tex to tikz.tex: ../anotherFolder/tikz not from main.tex to tikz.tex: Subfolder/anotherFolder/tikz.

Thinking about it, that totally makes sense. Perhaps this solution can help you.