I'd to be able to set a value at the start of my document that sets in which chapters examples will have solutions. My attempt at this is as follows.
\newcommand{\solutionsupto}{2}
\newenvironment{solution}{
\ifnum \thechapter < \solutionsupto
\underline{\bf Solution}
}{\fi}
The intention here is that solutions should be displayed in chapter 1 but not chapter 2 (or later chapters).
However, I get a compilation error if I have a solution in chapter 2. For example,
\documentclass[a4paper,12pt]{book}
\usepackage[british]{babel}
\newcommand{\solutionsupto}{2}
\newenvironment{solution}{
\ifnum\value{chapter} < \solutionsupto
\underline{\bf Solution}
}{\fi}
\begin{document}
\chapter{One}
First question.
\begin{solution}
First answer
\end{solution}
\chapter{Two}
Second question
\end{document}
works as expected, but
\documentclass[a4paper,12pt]{book}
\usepackage[british]{babel}
\newcommand{\solutionsupto}{2}
\newenvironment{solution}{
\ifnum\value{chapter} < \solutionsupto
\underline{\bf Solution}
}{\fi}
\begin{document}
\chapter{One}
First question.
\begin{solution}
First answer
\end{solution}
\chapter{Two}
Second question
\begin{solution}
Second answer
\end{solution}
\end{document}
results in an Incomplete \ifnum
error.
On the other hand, if I replace the use of the solution environment with what I think it should evaluate to, that is
\documentclass[a4paper,12pt]{book}
\usepackage[british]{babel}
\newcommand{\solutionsupto}{2}
\newenvironment{solution}{
\ifnum\value{chapter} < \solutionsupto
\underline{\bf Solution}
}{\fi}
\begin{document}
\chapter{One}
First question.
\ifnum \thechapter < \solutionsupto
\underline{\bf Solution}
First answer
\fi
\chapter{Two}
Second question
\ifnum \thechapter < \solutionsupto
\underline{\bf Solution}
Second answer
\fi
\end{document}
it works exactly as expected with the first answer displayed but the second answer omitted.
Any help on this would be very much appreciated.
\ifnum \thechapter < \solutionsupto
with\ifnum\value{chapter} < \solutionsupto
?\documentclass{...}
, the required\usepackage
's,\begin{document}
, and\end{document}
. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.