# How to do multiple calls to \includeonly?

Doing multiple call to include only as:

\includeonly{myfirstinclude}
\includeonly{mysecondinclude}


It is only including the last file called mysecondinclude by the last \includeonly call. It is useful to do multiple calls to \includeonly when you have all your \includeonly call listed and commented out.

\documentclass{article}

% How does 'filecontents' keep LaTeX parsing while temporarily stop writing output
% https://tex.stackexchange.com/questions/104159/how-does-filecontents-keep-latex
\usepackage{filecontents}

\begin{filecontents*}{myfirstinclude.tex}
myfirstinclude
\end{filecontents*}

\begin{filecontents*}{mysecondinclude.tex}
mysecondinclude
\end{filecontents*}

\begin{filecontents*}{mythirdinclude.tex}
mythirdinclude
\end{filecontents*}

% \includeonly{myfirstinclude}
% \includeonly{mysecondinclude}
% \includeonly{mythirdinclude}

\begin{document}

\include{myfirstinclude}
\include{mysecondinclude}
\include{mythirdinclude}

\end{document}


So when you want to include only one or some files, you just uncomment the desired line(s).

\includeonly{myfirstinclude}
\includeonly{mysecondinclude}
% \includeonly{mythirdinclude}


# Related Questions

• It should be said somewhere that the usual way to do this would be \includeonly{myfirstinclude,mysecondinclude}, which doesn't need any new macros. The disadvantage is that you have to change the command manually instead of commenting out lines. (You already know this, since \addtoincludeonly is building a comma separated list. But if a TeX rookie finds this question, I would encourage them to start with the usual way before using your macro.) – Teepeemm Sep 7 '17 at 1:15

For your specific use case you could use just one includeonly by putting each chapter on its own line:

\includeonly{%
myfirstinclude,
mysecondinclude,
mythirdinclude,
}


This way it is easy to comment out any of them, say, the second one:

\includeonly{%
myfirstinclude,
%mysecondinclude,
mythirdinclude,
}


You can create a new command as \addtoincludeonly{somefile}, which holds an internal string as explained on Stringified commands to macros sequence, then after all your custom include only calls, you do the call to another command as \doincludeonly, which takes no arguments.

The new commnad \doincludeonly is required as we only should do a call to \includeonly when some call to \addtoincludeonly{somefile} is performed. Otherwise an empty call as includeonly{} will create an almost empty document not including anything inserted/added by \include{somefile}.

\documentclass{article}

% How does 'filecontents' keep LaTeX parsing while temporarily stop writing output
% https://tex.stackexchange.com/questions/104159/how-does-filecontents-keep-latex
\usepackage{filecontents}

% Logical String Length
% https://tex.stackexchange.com/questions/87638/logical-string-length
\usepackage{xifthen}
\usepackage{xstring}

\newcommand{\includeonlyfilelist}[0]{}
\makeatletter
{%
\StrLen{\includeonlyfilelist}[\includeonlyfilelistlen]

% How to concatenate strings into a single command?
% https://tex.stackexchange.com/questions/74707/how-to-concatenate-strings-into-a
\ifnum\includeonlyfilelistlen>0
\else
\fi
}
\newcommand{\doincludeonly}[0]
{%
\StrLen{\includeonlyfilelist}[\includeonlyfilelistlen]
\ifnum\includeonlyfilelistlen>0
\includeonly{\includeonlyfilelist}
\else
\fi
}
\makeatother

\begin{filecontents*}{myfirstinclude.tex}
myfirstinclude
\end{filecontents*}

\begin{filecontents*}{mysecondinclude.tex}
mysecondinclude
\end{filecontents*}

\begin{filecontents*}{mythirdinclude.tex}
mythirdinclude
\end{filecontents*}