我想创建一个空白练习,其中的答案可以是文本或数学表达式,并且下面列出所有答案的无序列表。
在我使用 LUA 制作的 MWE 中,文本按预期出现在下方,但我不知道如何使用像 $\pi$ 或 $\frac{2}{3}$ 这样的数学表达式。
任何帮助,将不胜感激。
\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}
编辑:感谢 Mico,我更新了我的代码,并将答案显示在方框中。当隐藏的答案是数学表达式的一部分并被分隔符包围时,就会出现问题,就像最后两个例子:分数的括号和花括号。
产生的错误都指向最后一个 \luaexec 块的结尾:“缺少插入的 $。”和“额外的 },或者忘记了 $。”
不知道有没有什么办法可以解决这个问题,代码如下:
\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}
再次,任何帮助都将不胜感激。
答案1
结合以下想法:
你可以做这样的事情
% !TeX TS-program = lualatex
\documentclass{article}
\usepackage{xcolor}
\usepackage{expl3}
\usepackage{luacode} % for '\luastringN' and '\luaexec' macros
\usepackage{unicode-math}
\setlength\parindent{0pt}
\renewcommand{\baselinestretch}{1.5}
\everymath{\displaystyle}
\directlua{list={}} % intialize a Lua table object
% select version by using \finaltrue or \finalfalse
\newif\iffinal
%\finaltrue
\finalfalse
\ExplSyntaxOn
\cs_set:Npn \hide #1 {
% check if in math mode
\relax\ifmmode
\directlua{ table.insert(list,\luastringN{$#1$})}
\else
% normal text mode
\directlua{ table.insert(list,\luastringN{#1})}
\fi
% instead of making things white, we simply create a placeholder with the same dimensions
% this prevents the learners from copying the answer from a PDF viewer
\iffinal
\phantom{#1}
\else
\textcolor{red}{#1}
\fi
}
\ExplSyntaxOff
\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}
\begin{luacode}
for i,v in ipairs(list) do
--texio.write_nl(v)
tex.sprint ( '\\fbox{'..v..'} \\ ' )
end
\end{luacode}
\end{document}
第二个链接也表明,可以用不同的方式处理数学风格,但由于上述原因这里,其工作效果不太理想。
事实上,仅使用 LaTeX 就可以实现您的目标。尝试下面的代码。
\documentclass{article}
\usepackage{xcolor}
\usepackage{expl3}
\setlength\parindent{0pt}
\renewcommand{\baselinestretch}{1.5}
\everymath{\displaystyle}
% select version by using \finaltrue or \finalfalse
\newif\iffinal
%\finaltrue
\finalfalse
\ExplSyntaxOn
\seq_new:N \g_my_keys_seq
\cs_set:Npn \hide #1 {
% check if in math mode
\relax\ifmmode
\seq_gput_right:Nn \g_my_keys_seq {$#1$}
\else
% normal text mode
\seq_gput_right:Nn \g_my_keys_seq {#1}
\fi
% instead of making things white, we simply create a placeholder with the same dimensions
% this prevents the learners from copying the answer from a PDF viewer
\iffinal
\phantom{#1}
\else
\textcolor{red}{#1}
\fi
}
\tl_set:Nx \pivalue {\fp_use:N \c_pi_fp}
\ExplSyntaxOff
\begin{document}
Try with \hide{text}.\par
Try with \hide{LUA \pivalue}.\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}
\ExplSyntaxOn
% shuffle keys
\seq_shuffle:N \g_my_keys_seq
\seq_map_inline:Nn \g_my_keys_seq {
\fbox{#1}\
}
\ExplSyntaxOff
\end{document}