handouts/Problems/problem.tex

92 lines
2.6 KiB
TeX
Raw Normal View History

2023-05-17 13:00:54 -07:00
% Defines a new problem.
% See problem definition files for usage examples.
\long\def\problemdef#1#2#3{
\ifcsname#1:problem:#2\endcsname
\PackageError{Problem Sets}{Problem #1:#2 is already defined}{}
\fi
% Use a group to limit scope of special commands.
\begingroup
% \gdef = \global\def
% This makes problem definitions global, and not restricted to this group.
%
% ##1: used to define parametrized macros in a parametrized macro.
% When \problemdef is expanded, ##1 becomes #1.
%
% \long\def: like \def, but allows multi-paragraph arguments
% \def\name#1{def: #1}: define a macro \name with one arg. TeX primitive.
%
% We don't use \newcommand or \renewcommand here because they will fail if
% a macro is defined/not defined. \def will always work, defining a new name
% in this group, temporarily redefining existing commands.
%
% \expandafter <macro> <tokens>
% \expandafter command delays expanding a macro until its arguments have been expanded.
\long\def\difficulty##1{\expandafter\gdef\csname#1:difficulty:#2\endcsname{##1}}
\long\def\statement##1{\expandafter\gdef\csname#1:problem:#2\endcsname{##1}}
\long\def\solution##1{\expandafter\gdef\csname#1:solution:#2\endcsname{##1}}
\long\def\answer##1{\expandafter\gdef\csname#1:answer:#2\endcsname{##1}}
#3
\endgroup
\ifcsname#1:all\endcsname
\expandafter\edef\csname#1:all\endcsname{\csname#1:all\endcsname \p{#1}{#2}}
\else
\expandafter\gdef\csname#1:all\endcsname{
\p{#1}{#2}
}
\fi
}
% Gets problem data.
% Arguments:
% #1: Category
% #2: Problem id
% #3: Field to get
\NewDocumentCommand{\get}{ m m m }{%
\ifcsname#1:#3:#2\endcsname%
\csname#1:#3:#2\endcsname%
\else%
\PackageError{Problem Sets}{Problem #1:#2 is not defined}{}
\fi%
}
\NewDocumentCommand{\getdifficulty}{ m m }{\get{#1}{#2}{difficulty}}
\NewDocumentCommand{\getproblem}{ m m }{\get{#1}{#2}{problem}}
% #1: Category
% #2: Problem id
% #3: Show if this problem has an answer
% #4: Show if this problem doesn't have an answer
\NewDocumentCommand{\ifanswer}{ m m m d[] }{%
\ifcsname#1:answer:#2\endcsname%
#3%
\else%
\IfNoValueF{#4}{#4}%
\fi%
}
\NewDocumentCommand{\ifsolution}{ m m m d[] }{
\ifcsname#1:solution:#2\endcsname%
#3%
\else%
\IfNoValueF{#4}{#4}%
\fi%
}
\NewDocumentCommand{\getanswer}{ m m }{%
\ifanswer{#1}{#2}{\get{#1}{#2}{answer}}%
[\PackageError{Problem Sets}{Problem #1:#2 has no answer}{}]%
}
\NewDocumentCommand{\getsolution}{ m m }{
\ifsolution{#1}{#2}{\get{#1}{#2}{solution}}%
[\PackageError{Problem Sets}{Problem #1:#2 has no solution}{}]%
}
\input{problems/numbertheory}
\input{problems/algebra}
\input{problems/combinatorics}
\input{problems/pidgeonhole}