I want to create a blank exercise, in which answers can be text or mathematical expression, and to have below a list of all answers in disorder.
In the MWE i made with LUA, the text appears below as expected, but I don't know how to have math expressions like $\pi$ or $\frac{2}{3}$.
Any help would be appreciated.
\documentclass{article}
\usepackage{xcolor}
\usepackage{luacode}
\setlength\parindent{0pt}
\directlua{list={}; }
\def\hide#1{
\directlua{ table.insert(list,"#1") }
\textcolor{red}{#1} % final version will have white instead of red
}
\begin{document}
Try with \hide{text}.\
Try with \hide{ LUA \directlua{ tex.sprint(math.pi); } }.\
Try with \hide{ \bf{command} }.\
Try with \hide{ "math mode $\pi$" }.\par\vspace{\baselineskip}
\rule[2ex]{\linewidth}{0.5pt}
\directlua{ for i,v in ipairs(list) do
tex.print(v);
tex.print("\noexpand\\");
end }
\end{document}
EDIT: I updated my code thanks to Mico, and i made the answers to appears in boxes. The problem comes when the hidden answer is a part of a math expression and surrounded with delimiters, like the last two exemples: perenthesis and curly braces of fraction.
The resulting errors both point to the end of last \luaexec block: "Missing $ inserted." and "Extra }, or forgotten $."
I wonder if there is any way to solve this. Here is the code:
\documentclass{article}
\usepackage{xcolor}
\usepackage{luacode} % for '\luastringN' and '\luaexec' macros
\setlength\parindent{0pt}
\renewcommand{\baselinestretch}{1.5}
\everymath{\displaystyle}
\directlua{list={}} % intialize a Lua table object
\def\hide#1{%
\directlua{ table.insert(list,\luastringN{#1})}%
\textcolor{red}{#1}% final version will have white instead of red
}
\begin{document}
Try with \hide{text}.\par
Try with \hide{LUA \directlua{ tex.sprint(math.pi)}}.\par
Try with \hide{\textbf{command}}.\par
Try with \hide{math mode $\pi$ inside}.\par
Try with inside math mode $\hide{\pi}$.\par
Try with inside math delimiters $f(x)=( \ \hide{5-x^2}\ ) e^x$.\par
Try with inside math delimiters $f(x)=\frac{ \hide{4-\sqrt{x}} }{e^x}$.\par
\vspace{\baselineskip}\hrule\vspace{\baselineskip}
\luaexec{
for i,v in ipairs(list) do
tex.sprint ( '\fbox{'..v..'} \ ' )
end
}
\end{document}
Again, any help would be appreciated.

