2

I would like to defines one element from a list with \edef. I wrote the following MWE, which shows that only \def gives a result. However, as i use the result in many complex ways, \def is not an option for me.

Why doesn't it work, and how should i do ? Thanks for any help.

\documentclass{article}
\setlength\parindent{0pt} 

\ExplSyntaxOn
\NewDocumentCommand { \defseq } { m m }
{   \seq_gset_from_clist:Nn#1{#2}  }

\NewDocumentCommand { \getelement } { m m } { \seq_item:Nn#1{#2} }
\ExplSyntaxOff

\begin{document}

\defseq{\listcomp}{\geq,\leq,>,<}
\edef\choixcomp{\fpeval{ randint(1,4) }}

%\def\comp{ \getelement{\listcomp}{ \choixcomp } } % Works
\edef\comp{ \getelement{\listcomp}{ \choixcomp } } % Bug
$\comp$

\end{document}
3
  • 1
    why do you want to mix expl3 definitions with edef? commands defined with NewDocumentCommand are not expandable so so do not expand in an edef Commented Feb 26, 2023 at 18:42
  • I wanted to keep latex3 in preamble, and a more user-oriented syntax in document. Commented Feb 26, 2023 at 20:37
  • 1
    \edef is as far from user oriented as you could imagine. Very few latex constructs will survive being used in \edef. Commented Feb 26, 2023 at 21:01

1 Answer 1

2

I would avoid mixing \edef and expl3.

Also, it's better to use names rather than commands for the sequences.

You can extract a specified item or a random one.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\defseq}{ m m }
 {
  \seq_new:c { g_mathteacher_named_#1_seq }
  \seq_gset_from_clist:cn { g_mathteacher_named_#1_seq } { #2 }
 }

% extract a specified item
\NewExpandableDocumentCommand{\getseqelement}{ m m }
 {
  \seq_item:cn { g_mathteacher_named_#1_seq } { #2 }
 }

% get the sequence length, just in case
\NewExpandableDocumentCommand{\getseqlength}{m}
 {
  \seq_count:c { g_mathteacher_named_#1_seq }
 }

% extract a random item
%
% \int_rand:n { <integer expression> } returns a random
% integer from 1 to <integer expression>
% so we can use \seq_count:c { <seq name> }
\NewExpandableDocumentCommand{\getseqrandomelement}{m}
 {
  \seq_item:cn { g_mathteacher_named_#1_seq }
   {
    \int_rand:n { \seq_count:c { g_mathteacher_named_#1_seq } }
   }
 }

\ExplSyntaxOff

\begin{document}

\defseq{listcomp}{\geq,\leq,>,<}

$\getseqrandomelement{listcomp}$

$\getseqrandomelement{listcomp}$

$\getseqrandomelement{listcomp}$

$\getseqrandomelement{listcomp}$

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .