5

I use a clist of delimiters to define functions that enclose text within the said delimiters.

Here is a MWE:

\ExplSyntaxOn

\clist_new:N \l_delim_clist
\clist_put_right:Nn \l_delim_clist {
  {paren, (, )},
  {curly, \{, \}},
  {brac, [, ]}
}

\clist_new:N \l_delim_specs
\clist_map_inline:Nn \l_delim_clist {
  \clist_clear:N \l_delim_specs
  \clist_put_right:Nn \l_delim_specs {#1}
  \clist_pop:NN \l_delim_specs \l_delim_name
  \clist_pop:NN \l_delim_specs \l_delim_start
  \clist_pop:NN \l_delim_specs \l_delim_end

  \cs_new_nopar:cpx {delim_ \l_delim_name :n} ##1 {\l_delim_start ##1 \l_delim_end}
}

\show\delim_paren:n
\show\delim_brac:n

\ExplSyntaxOff

Now, on compilation, TeX runs out of stack space. This seems to be because of the curly part. In particular, removing line 6:

  {curly, \{, \}},

leads to correct compilation with the expected result. That is, \show\delim_paren:n does yield:

> \delim_paren:n=macro:
#1->(#1)

I would like \delim_curly:n to be the macro #1->\{#1\} which may be used in math mode. Could someone shed some light on the reason TeX runs into an infinite loop and how I may fix the above code? Thanks!

  • The problem is that \{ and \} (with their current definition) are not “fully expandable” and so they don't survive \edef (which you are using with \cs_new_nopar:cpx – egreg Mar 12 '14 at 12:47
  • 1
    if you only want math you could use \lbrace and \rbrace instead of \{ \} – David Carlisle Mar 12 '14 at 12:54
6

You can't use \{ and \} inside an x expansion argument, unless you take precautions not to expand them. The same problem doesn't apply with the other delimiters because they're character tokens.

You can exploit the fact that \clist_item:nn returns the item with \exp_not:n around it.

\RequirePackage{expl3}

\ExplSyntaxOn

\clist_new:N \l_arakonam_delim_clist
\clist_set:Nn \l_arakonam_delim_clist
 {
  {paren, (, )},
  {curly, \{, \}},
  {brac, [, ]}
 }

\clist_map_inline:Nn \l_arakonam_delim_clist
 {
  \cs_new_nopar:cpx {delim_ \clist_item:nn {#1} {1} :n} ##1
   {
    \clist_item:nn {#1} {2}
    ##1
    \clist_item:nn {#1} {3}
   }
 }

\cs_show:N \delim_paren:n
\cs_show:N \delim_brac:n
\cs_show:N \delim_curly:n

\ExplSyntaxOff

Here's the output on the terminal:

> \delim_paren:n=macro:
#1->(#1).
<recently read> }

l.24 \cs_show:N \delim_paren:n

? 
> \delim_brac:n=macro:
#1->[#1].
<recently read> }

l.25 \cs_show:N \delim_brac:n

? 
> \delim_curly:n=macro:
#1->\{#1\}.
<recently read> }

l.26 \cs_show:N \delim_curly:n

? 
  • 1
    Thanks! Your answer was to the point (and very quick too). \clist_item:nn seems like a recent addition and is not available in my expl3 package. I did get it working by saying: {curly, {\exp_not:n {\{}}, {\exp_not:n {\}}}}, – arakonam Mar 12 '14 at 13:17
  • @arakonam It's much better to have a very up-to-date expl3 bundle. You can also use your code, but with \exp_not:V \l_delim_start and \exp_not:V \l_delim_end. However, note that you're using wrong variable names and don't declare them. – egreg Mar 12 '14 at 13:19
  • I'm writing these macros towards easy semantics for writing papers in my area of research. I am aiming towards making the package as inter-operable with an eye towards submitting to journals / colloborating with who might not have but a standard texlive distribution. Thus, while I could pull an svn version, my co-authors / journals might not like to do it; this would lead to having to worry about bundling the package but that's a separate question altogether. Thanks again! – arakonam Mar 12 '14 at 13:23
  • Yes, the naming convention suggests \l_delim_start_tl I suppose and that I should define them. I will remember that. – arakonam Mar 12 '14 at 13:27
  • 1
    @arakonam The feature clist_item:nn has been in expl3 (as an 'experimental addition') for some time. Trying to use too old an expl3 version is fraught with issues: we are still developing the language and its not realistic to hope to use arbitrary older versions . – Joseph Wright Mar 12 '14 at 13:39

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.