4

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.

New contributor
thecloisterbell is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • 1
    Welcome to TeX.SE. Have you tried replacing \ifnum \thechapter < \solutionsupto with \ifnum\value{chapter} < \solutionsupto?
    – Mico
    Commented 15 hours ago
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \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.
    – cabohah
    Commented 15 hours ago
  • @Mico Thanks, I have tried it now and it didn't make any diffrence. Commented 14 hours ago
  • @cabohah Thanks for the feedback. I've made the examples compilable. It wasn't a matter of my tedium, rather trying to avoid irrelevant details for the reader. Commented 14 hours ago

1 Answer 1

3

You can't have \fi in the end part, because \end{solution} isn't expanded if it's in the false branch of a conditional.

You can instead absorb the contents of the environment using the +b argument type.

\documentclass[a4paper,12pt]{book}
\usepackage[british]{babel}

\newcommand{\solutionsupto}{2}

\NewDocumentEnvironment{solution}{+b}{%
  \ifnum\value{chapter} < \solutionsupto \relax
    \par\noindent\underline{\bfseries Solution}\par
    #1
  \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}

output

Beware: commands such as \bf or \it have been deprecated for 30 years and aren't defined in the kernel. Some classes may define it for back compatibility with 30+ year old documents.

2
  • Great. Thank you. This works in overleaf. I am getting an Undefined control sequence error on the NewDocumentEnvironment when I try to compile it on my work system. Is this a newer command. Do I just need to ask IT to update TexLive, or is there a deeper issue? Good point on the \bf - that part was copy/pasted from an old document I'm updating. Commented 14 hours ago
  • @thecloisterbell Update. Depending on the version of TeX Live you have, adding \usepackage{xparse} might solve.
    – egreg
    Commented 14 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.