# How can I use subfiles and standalone for several generations of LaTeX files?

I am writing my thesis in LaTeX, and my literature review now includes some TikZ figures. I can compile the individual figures, and I can compile the whole thesis, but I can't compile the literature review itself.

I was able to build a minimum working example which demonstrates my issue. This is my first time writing a MWE and also my first time using filecontents so please feel free to suggest how I might improve what I've done here.

% "pdflatex thesis.tex" works correctly
% "pdflatex varieties.tex" works correctly
% "pdflatex litreview.tex" does not work correctly, and generates the following output:
% ! LaTeX Error: Can be used only in preamble.
%
% See the LaTeX manual or LaTeX Companion for explanation.
% Type  H <return>  for immediate help.
%  ...
%
% l.5 \documentclass
%                   {standalone}
\begin{filecontents}{thesis.tex}
\documentclass{book}
\usepackage[english]{babel} % just for the MWE
\usepackage{blindtext} % just for the MWE
\usepackage{standalone}
\usepackage{subfiles}
\input{preamble}
\begin{document}
\chapter{Lit Review}
\subfile{litreview}
\end{document}
\end{filecontents}
\begin{filecontents}{preamble.tex}
\usepackage{tikz}
\usetikzlibrary{mindmap}
\end{filecontents}
\begin{filecontents}{litreview.tex}
\documentclass[thesis.tex]{subfiles}
\begin{document}
\blindtext
\begin{figure}
\input{varieties}%
\caption{Caption}
\end{figure}
\blindtext
\end{document}
\end{filecontents}
\begin{filecontents}{varieties.tex}
\documentclass{standalone}
\ifstandalone
\input{preamble}%
\fi
\begin{document}
\tikz[mindmap,concept color=red,text=black,grow cyclic]
\end{document}
\end{filecontents}


EDIT: Please note that I have no hard-and-fast reason to use both standalone and subfiles. I started with subfiles because the pattern worked for me, and standalone worked well for the figure. I would love to see a MWE which does what my MWE does but with either standalone or subfiles but not both, if using both together isn't considered a best practice.

• Welcome to TeX.SX! Any pointer for the particular thesis class you're using? – egreg Sep 27 '16 at 22:48
• I haven't looked into these issues in a while, but the solution I have been using has worked for years and continues to work: Just use the standalone package (don't see why you also need subfiles) and all files should use the same preamble. So, if there is a reason you need both, then please add that to the question. One possible issue I see is that varieties.tex invokes \input{preamble} and preamble.tex invokes \input{varieties}! – Peter Grill Sep 28 '16 at 0:09
• @egreg the specific thesis class that I'm using is beavtex which I've got at github.com/mathuin/beavtex and which is based on the book class. Oh, and thank you for the welcome! – Jack Twilley Sep 28 '16 at 18:30
• @PeterGrill The preamble.tex file above only contains two lines, one to use TikZ and another to select a particular library. The varieties.tex file imports the preamble only when compiled standalone, as the preamble would otherwise be imported already. As for why I need both, I don't necessarily need both. However, I'm new to multi-file LaTeX, and using both has gotten me close to my solution. If you know a way to rewrite what I have posted above with only standalone, please post it and I'll mark it as the solution if it works for me! – Jack Twilley Sep 28 '16 at 18:34

After quite a bit of tinkering, I was able to rewrite my example using only standalone as per @PeterGrill's suggestion.

\documentclass{minimal}
\usepackage{filecontents}
\begin{filecontents}{thesis.tex}
\documentclass{book}
\input{preamble}
\begin{document}

\chapter{Lit Review}
\input{litreview}%

\end{document}
\end{filecontents}
\begin{filecontents}{preamble.tex}
\usepackage[english]{babel} % just for the MWE
\usepackage{blindtext} % just for the MWE
\usepackage{standalone}
\usepackage{tikz}
\usetikzlibrary{mindmap}
\end{filecontents}
\begin{filecontents}{litreview.tex}
\documentclass[preview]{standalone}
\input{preamble}
\begin{document}

\blindtext

\begin{figure}
\input{varieties}%
\caption{Caption}
\end{figure}

\blindtext

\end{document}
\end{filecontents}
\begin{filecontents}{varieties.tex}
\documentclass[tikz]{standalone}
\input{preamble}
\begin{document}

\tikz[mindmap,concept color=red,text=black,grow cyclic]

The key to this solution was the addition of preview and tikz as options to the standalone document class declaration. These additions make the individual files compile correctly. It's possible that the tikz option may not be necessary, but it also acts as a reminder to me that that's a picture file, so I'm keeping it.