[Tex/LaTex] Compiling multiple LaTeX files

compilingtexmaker

I have been trying to compile several LaTeX files and have been using \input to do this. However this causes some annoyances that I'm unaware of to fix (I'm using TexMaker as the editor)

  1. I get a lot of errors, if I include the preamble in any other file than the the main tex file that has \input-commands in it. As a result whenever I compile, I have to compile the whole document all the time.
  2. When you are using one file, TexMaker has this nice property where if you compile and then view your pdf using the GUI, then the pdf jumps to the point you have just been working on. This goes away when you use the \include command where the pdf just opens to the very last page.
  3. I sometimes accidentally hit compile with the subfiles in focus which throws all sorts of errors as the subfile doesn't have any preamble at all (not even a \documentclass).
  4. The helpful popups for \ref to add references are no longer present when you are working on parts of the file which complicates matters.

Short of putting everything back in to one unwieldy tex file, are there fixes for any or all of these issues?

For example, is there a package I could use that solves most of the issues? It would be great if 2), 3) and 4) have at least partial solutions. With 1) I can live with.

Best Answer

1) I get a lot of errors if I include the preamble in any other file than the the main tex file that has \input commands in it. As a result whenever I compile, I have to compile the whole document all the time.

The way to go around this is the {subfiles} package:

Put\usepackage{subfiles} in the preamble of the main.tex.

other *.tex files are included by:

\subfile{sectionOrCapter} % no extention needed

Alls other *.tex files look like this:

\documentclass[main.tex]{subfiles}

 %% special definitions or command overrides here

\begin{document}
% any LaTeX content

\subfile{someOtherSubfile} 
% any LaTeX content

\subfile{someMoreSubfile}
% any LaTeX content

\end{document}

There are some points to keep in mind:

  • All subfiles refer to main.tex in their \documentclass command, even it they as "transitive".

  • the reference to main.tex is a *relative path, this means if your subfile is buried in a deep folder structure you have to add enough ../

  • if your subfiles are in file system folders you should use the \graphicspath command to point to the folder containing your images:


main.tex

\graphicspath{{.}}
\begin{document}
    \includegraphics{imageInRootFolder}
    \includegraphics{subfolder/imageInSubFolder}
    \subfile{subfolder/someSubfile}
\end{document}

subfolder/someSubfile.tex

\graphicspath{{../}}
\begin{document}
    \includegraphics{imageInRootFolder}
    \includegraphics{subfolder/imageInSubFolder}
\end{document}

Alternatively you could swich to TexStudio as the successor of TexMaker wich can handle separately compile nested files on its own...