[Tex/LaTex] How to choose which sections to compile from a LaTeX document

compilingsectioning

Is it possible to compile a LaTeX document but only include one section and its associated tables and figures?

I have two cases for which this would be useful:

  1. I would like to provide the Methods section of a paper to colleagues.

  2. I would like to have multiple versions of a document that contain different combinations of the sections of a master document.

I know that I could place each section in a separate file and then have separate master files – likely what I will do for case 2 since it is integral to the final document structure, but at least for case 1, I am hoping that there is a way to only compile one section.

Thanks!

Best Answer

I'd have a look at the Subfiles LaTeX package for Part 1. It provides a lot of flexibility, and saves having to alter files prior to building them.

Essentially, you could have your main document Paper.tex:

% Preamble
\usepackage{subfiles}
% More preamble
\begin{document}
  \subfile{Methods}
  \subfile{AnotherSection}
  \subfile{OtherStuff}
\end{document}

And then each other section would look a bit like this:

\documentclass[Paper.tex]{subfiles}
\begin{document}
  \section{Methods}
\end{document}

You could then either run LaTeX on the individual sections (for example Methods.tex) and it will be compiled with the preamble from the main root document.

To solve Part 2, I'd do something like the following which I've just tested with the examples above.

\documentclass[Paper.tex]{subfiles}
\begin{document}
  \subfile{Methods}
  \subfile{AnotherSection}
\end{document}

This means you only have to maintain one preamble, and stops you having lots and lots of root files, even though you will have a minimised root file for each version of your document involving more than one subfile.

Related Question