\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amsthm,amsfonts}
\usepackage{xifthen}
\newcounter{example}[subsection]
\newcommand{\challenge}[3]{\refstepcounter{example}\par\medskip\noindent \textbf{Challenge~\theexample.}
#1
\vskip0.2cm
\noindent\textbf{Solution}: #2
\vskip0.2cm
\ifthenelse{\equal{#3}{}}{}{\noindent\textbf{Notes}: #3}}
\begin{document}
\challenge{
Write a note.
}{
We simply write a note.
}{
A note.
}
\challenge{
Do not write any notes.
}{
We simply leave the last argument empty.
}{}
\challenge{
Demonstrate that notes can cause problems
}{
We simply put a matrix to the notes.
}{
Let $\varepsilon > 0$
% and
% $$
% A = \begin{matrix}
% 1
% \end{matrix}
% $$
% be a matrix
.
}
\end{document}
Which results in
When I uncomment the matrix part, I get the error:
line 46: Undefined control sequence. } : Incomplete \iffalse; all text was ignored after line 46.
The line 46 is where the closing } for the third argument of the third challenge is.
I haven't faced any problems when I put the matrices etc. in any or both of the first two arguments.


\iffrom\ifthenelseand themath modeintroduced with\[...\]– Feb 27 '17 at 16:30$$A = 2$$, it compiles as expected. – Antoine Feb 27 '17 at 16:36\ifcommand internally used inmatrix– Feb 27 '17 at 16:36\protect\begin{matrix}? – Feb 27 '17 at 16:46\protect\beginand\protect\endwould work but really the answer is not to do that but to use a better test for empty, either xparse or simply\if\relax\detokenize{#3}\relax\else\noindent\textbf{Notes}: #3\fi}Or if usingifthenelsejust to protect the whole thing eg via a toks register as you did below – David Carlisle Feb 27 '17 at 16:51\if\relax\detokenizethen. The token register was my first idea. I am not really much into\ifthenelse, yet, so don't really know how it is defined – Feb 27 '17 at 16:52\[ … \]preferable to$$? for reasons why avoiding$$in LaTeX. – egreg Feb 27 '17 at 17:17