1

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}

result


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.

2
  • For your EDIT, which really is a new question and is longer than your original query, you should have posted an entirely new query rather than modify an existing query. (For sure, I became aware of your massive edit only after you unaccepted my answer.) How was I supposed to foresee the additional requirements you would come up with when I wrote my initial answer? As a result of your edit, my answer is pretty much useless/meaningless for future readers of your now-greatly-modified query. I have therefore decided to delete my answer.
    – Mico
    May 30 '21 at 16:41
  • I was not sure what to do. Your answer was good, for the original post. I should have left this question as solved and made a new post. I will do that tomorrow. How can i repair? If you mention again the use of \luastringN (even shortly), i will accept it again. My apologies. May 30 '21 at 22:02
1

Combining the ideas of:

You can do something like this

enter image description here

% !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}

The second link also indicates that it is possible to process math styles differently, but due to the reasons explained here, it is not working very ideally.

As a matter of fact, it is possible to achieve your goals with LaTeX only. Try the code below.

\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}
4
  • Thank Alan you for those solutions, both functionnal. I might need some time to understand those... I've seen that 'texio.write' deals with console and log. Does that mean you put if for debugging purpose? I couldn't see the difference in the log file. Jun 1 '21 at 19:34
  • Yes, it is for debugging. You can safely delete it.
    – Alan Xiang
    Jun 1 '21 at 20:21
  • The problem about your code is when the material gets saved in the Lua table, it does not know whether the material is in math mode or not. I added some kind of conditional branch to automatically surround the code with $$ if the command is called in math mode.
    – Alan Xiang
    Jun 1 '21 at 20:22
  • Yes, i understood that. I'ts rather the expl3 commands that i've never seen before. I need to learn latex3 from scratch (and maybe also a lot of tex and some of latex2). Jun 2 '21 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.