I'm trying to use the standalone package. The idea is to have a large collection of standalone TeX files each containing a 'problem' (all text), and to be able to compile larger documents, each containing several problems selected from the pool. Now, I also want to toggle the visibility of the 'solution' to each problem, both in the main file and subfiles. I accomplish this by using if conditional and the comment package. But the main file fails to compile. A minimal example is as follows:
Subfile:
%!TeX root = sub.tex
\documentclass[class=article,crop=false,12pt]{standalone}
\usepackage{comment}
\newif\ifsolution
\solutiontrue
\ifsolution
\newenvironment{solution}{\medskip\textbf{Solution: }\ignorespaces}{}
\else
\excludecomment{solution}
\fi
\begin{document}
\subsection{Title}
Problem text.
\begin{solution}
Solution text.
\end{solution}
\end{document}
Main file:
\documentclass[12pt,a4paper,onecolumn]{article}
\usepackage[subpreambles=false]{standalone}
\usepackage{comment}
\newif\ifsolution
%\solutiontrue
\ifsolution
\newenvironment{solution}{\medskip\textbf{Solution: }\ignorespaces}{}
\else
\excludecomment{solution}
\fi
\title{Big Title}
\begin{document}
\maketitle
\input{sub.tex}
\end{document}
Note the the preambles in both files are identical. The statement \solutiontrue can be (un)commented to toggle the solution.
While the subfile compiles fine, the main file does not, giving the error Incomplete \iffalse; all text was ignored after line 17. (Line 17 is just after \title, and the error occurs at the line containing \input.
The error results from the \newif...\fi block in the subfile. It can be avoided by moving this block to a separate file and \inputing it in the subfile. I don't want to do that because I want to be able to easily toggle the solution.
It appears that standalone can't handle this if in the preamble, although my understanding is that it should completely discard the subfile preamble until \begin{document}. Is this correct? Is there a way to resolve this?
\begin{document}.standalonewhen imported into amain.texstrips the preamble out so only between\begin{document}and\end{document}is actually compiled. When you compile it individually fortikzor whatever purpose then it includes the preamble so you may have to just have to copy and paste the sameifblocks into your preamble and after\begin{document}– JamesT Apr 22 '23 at 14:50\ifsolutionwhen you\input{sub.tex}? – John Kormylo Apr 22 '23 at 14:51