1
\usepackage{comment}
\newif\ifshowsolution
\showsolutionfalse

\ifshowsolution%
\newenvironment{solution}{\paragraph{Solution.}}{} %
\newcommand{\insol}[1]{#1}%
\newcommand{\notsol}[1]{}%
\else
\excludecomment{solution}%
\newcommand{\insol}[1]{}%{} ò
\newcommand{\notsol}[1]{#1}%
\fi
  • Welcome to the site. If you highlight the actual code portion of your question and click the {} icon above the edit box, it indents it 4 spaces in the edit window, which has the effect of displaying the result as code for the rest of the readers. I already did this for you just now. – Steven B. Segletes Aug 24 '17 at 10:36
  • In brief: It shows some code if you ask it for solutions and show something else if not. – TeXnician Aug 24 '17 at 10:38
  • Welcome to TeX.SX! Please give a minimal working example (MWE), i.e. a full LaTeX document starting with \documentclass{...} and ending with \end{document}. – diabonas Aug 24 '17 at 10:39
4

The following might be described in non-correct terms:

\newif initializes a new Boolean switch. The following \ifshowsolution is the name of the conditional.

\newif doesn't need its argument to start with if but everything else is obfuscating. The first two letters get removed and true and false appended for the switching commands. The complete name including the first two letters are the conditional.

From now on every time you write \ifshowsolution the following code block up until a matching \else or \fi (whatever comes first) is executed if the switch is true. If there is a matching \else everything from that point on to a matching \fi is executed if the switch is set to false.

Conditionals can be used nested. Therefore the "matching" in the above is important. For each \if statement there has to be a matching \fi else the code results in an error. The \else is optional.

\showsolutionfalse sets the switch to false, \showsolutiontrue sets it to true.

For the code between \ifshowsolution and \else:

\newenvironment defines a new environment. It requires 3 arguments:

  1. the name
  2. the code which should be executed on \begin
  3. the code which should be executed on \end

In this case \begin{solution} should start a new paragraph called "Solution." and \end{solution} should do nothing. The stuff inbetween \begin and \end are a group, so commands which don't have a global effect have no effect on the rest of the code outside that group.

\newcommand{\insol}[1]{#1} defines a new command which requires an argument and just returns its argument without further addo.

\newcommand{\notsol}[1]{} defines a new command which requires an argument but returns nothing.

In the \else-branch the effects of \insol and \notsol get exchanged and the environment called solution gets defined as a comment based on the comment package, so everything inbetween \begin{solution} and \end{solution} has no effect at all.

Does this describe everything you want to know?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.