1

Im trying to nest two sequence mappings, which works so far but I want to use them inside a command definition so the <items> should be accessible as ##1 and ###1 but this won’t work. Is it possible to use nested mappings at this level?

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\makeatletter

% some variants
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnf , NnV , Nnx }

% sequence to store the higlight key value pair
\seq_new:N \l_@@_highlight_seq
% sequence to store the numbers inside the key
\seq_new:N \l_@@_highlight_pos_seq


\cs_new_protected:Npn \@@_evaluate_higlights:n #1
 {
  % map the whole argument as clist
  \clist_map_inline:nn { #1 }
   {
    % split a clist item at the equal sign
    \seq_set_split:Nnn \l_@@_highlight_seq { = } { ##1 }
    % split the first part of the higlight sequence at plus signs
    \seq_set_split:Nnf \l_@@_highlight_pos_seq { + }
     {
      \seq_item:Nn \l_@@_highlight_seq { 1 }
     }
    \par\bigskip
    % map the key numbers to their values
    \seq_map_inline:Nn \l_@@_highlight_pos_seq
     {
      % for this example just some text output
      number~###1
      {}~has~value~
      \seq_item:Nn \l_@@_highlight_seq { 2 }\par
     }
   }
 }

\NewDocumentCommand { \highlights } { m }
 {
  \@@_evaluate_higlights:n { #1 }
 }

\ExplSyntaxOff

\begin{document}
\textbf{Highlights}
\highlights{1=a, 2+3=b}

\bigskip
\textbf{Desired output}
\par\bigskip
number 1 has value a\par

\par\bigskip
number 2 has value b\par
number 3 has value b\par
\end{document}

highlights

2

Got it my self. The nesting sequence for arguments is #1, ##1, ####1 etc.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\makeatletter

% some variants
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnf , NnV , Nnx }

% sequence to store the higlight key value pair
\seq_new:N \l_@@_highlight_seq
% sequence to store the numbers inside the key
\seq_new:N \l_@@_highlight_pos_seq


\cs_new_protected:Npn \@@_evaluate_higlights:n #1
 {
  % map the whole argument as clist
  \clist_map_inline:nn { #1 }
   {
    % split a clist item at the equal sign
    \seq_set_split:Nnn \l_@@_highlight_seq { = } { ##1 }
    % split the first part of the higlight sequence at plus signs
    \seq_set_split:Nnf \l_@@_highlight_pos_seq { + }
     {
      \seq_item:Nn \l_@@_highlight_seq { 1 }
     }
    \par\bigskip
    % map the key numbers to their values
    \seq_map_inline:Nn \l_@@_highlight_pos_seq
     {
      % for this example just some text output
      number~####1
      {}~has~value~
      \seq_item:Nn \l_@@_highlight_seq { 2 }\par
     }
   }
 }

\NewDocumentCommand { \highlights } { m }
 {
  \@@_evaluate_higlights:n { #1 }
 }

\ExplSyntaxOff

\begin{document}
\textbf{Highlights}
\highlights{1=a, 2+3=b}

\bigskip
\textbf{Desired output}
\par\bigskip
number 1 has value a\par

\par\bigskip
number 2 has value b\par
number 3 has value b\par
\end{document}
|improve this answer|||||
  • When ## appears in a macro's definition, it becomes # when the macro is expanded. So basically you double #'s at each level. Your ###1 is really # followed by argument #1, which isn't what you want. – egreg Jul 21 '13 at 22:38
  • Yep, thanks. The penny's dropped a little late! – Tobi Jul 21 '13 at 22:52
  • @egreg: But something strange happens. The code works in my MWE exactly as I want, but when I copy the code (as is) to my package it stops working: the sequence isn’t spilt at the + no more. Do you have an idea why, without seeing the code? – Tobi Jul 21 '13 at 22:53
  • 1
    Are you using + or perhaps _ in the real code? – egreg Jul 21 '13 at 23:10
  • Hm … I made the + short verb in the documentation (i.e. in the dtx file, but neither in the testing code nor the sty file itself). Im using _ in the sty file as usual with \ExplSytaxOn and as a string to get the metric symbols. But the MWE even works when I include my package in it … I added a link to the real code to my question … – Tobi Jul 21 '13 at 23:14

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.