# How do I choose which sections to compile from a LaTeX document?

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!

-
For case 2, I guess some judicious uses of the \input command should do what you want. –  Willie Wong Dec 13 '10 at 16:48
–  Martin Scharrer Mar 19 '11 at 23:57

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.

-
I've just checked it does work for the second scenario, and it does, so I've edited my answer accordingly. –  Edd Dec 14 '10 at 15:58

For case 1:

Make a file methods.tex and put your methods in it:

\section{Methods}

The methods employed here...


Put the rest of the document into a file rest.tex.

In place of the text in the main document put

\include{methods}
\include{rest}


\includeonly{methods}


A disadvantage to this solution is that the methods section will start a new page.

You could also just delete the non-Methods pages from the PDF with a PDF utility. Or you could send them the whole paper and tell them just to read the Methods section. :-)

-

To q.1: If you don't want to change the structure of your document putting each section into a separate file, you may use

\usepackage{comment}
\excludecomment{toexclude} % you can name the comment as you wish


in the preamble, and mark up any temporary unnecessary part of the document with

\begin{toexclude}
...
\end{toexclude}


Once you want again to compile everything together, just replace

\excludecomment{toexclude}


by

\includecomment{toexclude}

-
Welcome to TeX.sx! –  Andrew Swann Mar 4 '13 at 10:01