Save the body of an environment to a macro, without typesetting
It is never safe to place arbitrary LaTeX content in edef or xdef.
The macro you want is already defined as \BODY
so you need \let
not \xdef
to give it a global name,
\documentclass{article}
\usepackage{environ}
\newcounter{problemnumber}\setcounter{problemnumber}{0}
\NewEnviron{problem}{%
\stepcounter{problemnumber}%%%%%%
\global\expandafter\let\csname myproblem-\theproblemnumber\endcsname\BODY
}
\begin{document}
\begin{problem}
Some equation: $E=mc^2$
\end{problem}
\begin{problem}
Some text.
A second paragraph with a \textbf{bold text}. And an inline equation: $E=mc^2$.
\end{problem}
...
Typsetting the problems in a different order, later in the document.
\textbf{Problem 2:}\quad\csname myproblem-2\endcsname\par\hrulefill
\textbf{Problem 1:}\quad\csname myproblem-1\endcsname\par\hrulefill
\end{document}
You should use \global\let
as pointed out by David.
Here's a shorter implementation with also an interface to print the problems.
\documentclass{article}
\ExplSyntaxOn
\seq_new:N \g_digitalink_problem_seq
\NewDocumentEnvironment{problem}{+b}
{
\seq_gput_right:Nn \g_digitalink_problem_seq { #1 }
}
{}
\NewExpandableDocumentCommand{\getproblem}{m}
{
\seq_item:Nn \g_digitalink_problem_seq { #1 }
}
\ExplSyntaxOff
\begin{document}
\begin{problem}
Some equation: $E=mc^2$
\end{problem}
\begin{problem}
Some text.
A second paragraph with a \textbf{bold text}. And an inline equation: $E=mc^2$.
\end{problem}
Typesetting the problems in a different order, later in the document.
\bigskip
\textbf{Problem 2:}\quad\getproblem{2}\par\hrulefill
\textbf{Problem 1:}\quad\getproblem{1}\par\hrulefill
\end{document}