\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}
其结果是
当我取消注释矩阵部分时,出现错误:
第 46 行:未定义控制序列。} :不完整 \iffalse;第 46 行之后的所有文本都被忽略。
第 46 行是第三个挑战的第三个参数的结束 } 的位置。
当我将矩阵等放入前两个参数中的任意一个或两个中时,我没有遇到任何问题。
答案1
Ifthenelse 完全展开它的第一个参数,所以脆弱的命令必须\protected
You could use
{
Let $\varepsilon > 0$
and
$$
A = \protect\begin{matrix}
1
\protect\end{matrix}
$$
be a matrix
.
}
但更好的方法是使用更好的空参数测试
\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
\if\relax\detokenize{#3}\relax\else\noindent\textbf{Notes}: #3\fi}
\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}
答案2
我使用了一个token
寄存器来预先扩展内容#3
以便使\if...
那里的嵌套内容静音。
不过,我建议使用类似xparse
带有可选参数的东西(请参阅本文末尾的另一个答案)
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amsthm,amsfonts}
\usepackage{xifthen}
\newcounter{example}[subsection]
\newtoks\daviddoesnotliketoksasname
\newcommand{\challenge}[3]{%
\refstepcounter{example}\par\medskip% \par?
\noindent%
\textbf{Challenge~\theexample.}
#1%
\vskip0.2cm
\noindent\textbf{Solution}: #2%
\vskip0.2cm
\daviddoesnotliketoksasname={#3}%
\ifthenelse{\equal{\the\daviddoesnotliketoksasname}{}}{}{\noindent\textbf{Notes}: \the\toks}%
}
\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}
xparse
使用版本更新
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amsthm,amsfonts}
\usepackage{xparse}
%\usepackage{xifthen}
\newcounter{example}[subsection]
\NewDocumentCommand{\challenge}{+m+m+o}{%
\refstepcounter{example}\par\medskip% \par?
\noindent%
\textbf{Challenge~\theexample.}
#1%
\vskip0.2cm
\noindent\textbf{Solution}: #2%
\vskip0.2cm
\IfValueT{#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 & 5 \\
3 & 4
\end{matrix}
\]
be a matrix.
]
\end{document}