1

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?

4
  • Welcome to TeX.SE!
    – Mensch
    Apr 22, 2023 at 14:42
  • Just want to add that I encountered this issue before and worked around it by putting stuff after \begin{document}. standalone when imported into a main.tex strips the preamble out so only between \begin{document} and \end{document} is actually compiled. When you compile it individually for tikz or whatever purpose then it includes the preamble so you may have to just have to copy and paste the same if blocks into your preamble and after \begin{document}
    – JamesT
    Apr 22, 2023 at 14:50
  • Won't you get a duplicate \ifsolution when you \input{sub.tex}? Apr 22, 2023 at 14:51
  • @JohnKormylo No, because the sub preamble is discarded by the main file.
    – Aftermath
    Apr 23, 2023 at 18:38

1 Answer 1

1

Low-level \if commands are rather special in TeX. Even if TeX skips over text and throws it away it sees them. So in your case when compiling main.tex TeX sees the \ifsolution in the sub.tex and is rather unhappy that it has no \fi.

Use a boolean from etoolbox instead:

\usepackage{etoolbox}
\newbool{solution}

%\booltrue{solution}

\ifbool{solution}
{\newenvironment{solution}{\medskip\textbf{Solution: }\ignorespaces}{}}
{\excludecomment{solution}}
1
  • Thank you. In addition I improved my preamble based on this answer, using the environ package, so the boolean is evaluated within the environment.
    – Aftermath
    Apr 23, 2023 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .