Skip to content

Instantly share code, notes, and snippets.

@N0taN3rd
Last active April 23, 2018 20:44
Show Gist options
  • Save N0taN3rd/66329b2a357a19ba95d58e52e231c220 to your computer and use it in GitHub Desktop.
Save N0taN3rd/66329b2a357a19ba95d58e52e231c220 to your computer and use it in GitHub Desktop.
Selectively Compile Latex Documents

The subfile package is a much more efficient way to bring in the sub parts of a large latex document.

Each part of the complete document that is split up into its own text file brought is added to the document from the main tex file via

\subfile{file} 

Each subfile has the following structure

\documentclass[main.tex]{subfiles}
\begin{document}
% content
\end{document}

And each subfile has access to all packages included in the preable of the main document.

I think there is an package option for subfiles that allows each subfile to use its own preamble.

Best pratice I found is include everything used by your subfiles in the main tex files preamble

Particulars as to the setup and macros documented as comments in the main.tex file included in this gist

The wrapping of figures, tables is optional!! And is only included in this MWE as an example as to what can be done using this approach

\documentclass[main.tex]{subfiles}
\begin{document}
\chapter{Stuff}
\displayIf[true] {
\lipsum
}
\end{document}
\documentclass[main.tex]{subfiles}
\begin{document}
\displayIf[false]{
\begin{figure}
Fig 1
\caption{Fig 1 does not gets compiled}
\end{figure}
}
\displayIf{
\begin{figure}
Fig 2
\caption{Fig 2 gets compiled}
\end{figure}
}
\displayIf{
\begin{table}
Table 1
\caption{Table 1 gets compiled}
\end{table}
}
\displayIf[false]{
\begin{figure}
Fig 2
\caption{Table 2 does not gets compiled}
\end{figure}
}
\end{document}
\documentclass{wsdlthesis}
%% For dummy text
\usepackage{lipsum}
%%% Used for selective compilation
\usepackage{xparse}
\usepackage{xstring}
\usepackage{subfiles}
\usepackage{ifthen}
%%%%
% Macro setup
%% Booleans for use in selective compilation
%%% Controls showing the preface
\newboolean{displayPrface}
\setboolean{displayPrface}{false}
%%% Controls showing the abstract
\newboolean{displayAbstct}
\setboolean{displayAbstct}{false}
%%% Controls showing each chapter
\newboolean{displayCh1}
\setboolean{displayCh1}{false}
%\newboolean{displayCh<N>}
%\setboolean{displayCh<N>}{false}
%%% Controls showing the appendix
\newboolean{displayApndx}
\setboolean{displayApndx}{false}
%%% Controls showing the vita page
\newboolean{displayVta}
\setboolean{displayVta}{false}
\NewDocumentCommand{\displayPreface}{}{
\setboolean{displayPrface}{true}
}
\NewDocumentCommand{\displayAppendix}{}{
\setboolean{displayApndx}{true}
}
\NewDocumentCommand{\displayAbstract}{}{
\setboolean{displayAbstct}{true}
}
\NewDocumentCommand{\displayVita}{}{
\setboolean{displayVta}{true}
}
%%% Sets each chapters display boolean to true
\NewDocumentCommand{\displayAllChapters}{}{
\setboolean{displayCh1}{true}
%\setboolean{display<ChN>}{true}
}
%%% Takes a single agrument, a number (1,2,3,...),
%%% and matches it to which chapter you want to compile
\NewDocumentCommand{\displayChapter}{m}{
\IfEq{1}{#1}{\setboolean{displayCh1}{true}}{}
}
%%% Processes a comma separated list of chapters numbers
%%% or single chapter number you want to compile
\NewDocumentCommand{\displayOnlyChapters}{ > { \SplitList { , } } m}{
\ProcessList {#1} { \displayChapter }
}
%%% This macro takses a single optional argument True or False
%%% The body of the macro is the contents to be displayed
%%% when the optional boolean argument is true (default)
\NewDocumentCommand{\displayIf}{O{true}+m} {
\ifthenelse{\boolean{#1}}{#2}{}%
}
%%%%
%%% Control display using the macros before document begins
%% Show only a single chapter
%\displayOnlyChapters{1}
%% or show multiple chapters
%\displayOnlyChapters{1,2}
%%% Full document control
\displayAbstract
\displayPreface
\displayAllChapters
\displayAppendix
\displayVita
%%% cause errors
\title{To Selectively Compile}
\author{John Berlin}
\principaladviser{Mat Kelly}
\degrees{Baddass}
\dept{Computer Science}
\submitdate{May 2018}
\begin{document}
\displayIf[displayAbstct]{
\abstract{\lipsum[1]}
\beforepreface
}
\displayIf[displayPrface]{
\subfile{preface}
\afterpreface
}
\displayIf[displayCh1]{
\subfile{chapter1}
}
%\displayIf[displayCh<N>]{
% \subfile{<chapter.tex>}
%}
\displayIf[displayApndx]{
\appendix
\subfile{appendices}
}
\displayIf[displayVta]{
\subfile{vita}
\vitapage
}
\end{document}
\documentclass[main.tex]{subfiles}
\begin{document}
\vita{\lipsum[1]}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment