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

Tobi
  • 56,353
  • Related/duplicate: http://tex.stackexchange.com/questions/42463/what-is-the-meaning-of-double-pound-symbol-1-in-an-argument See also http://tex.stackexchange.com/questions/56957/latex3-double-number-sign – egreg Jul 21 '13 at 22:46

1 Answers1

3

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}
Tobi
  • 56,353
  • 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
  • Ups. That was stupid: I used \MakeShortVerb{\+} in the testing file to write the documentation inside of it … damn! Thank you! – Tobi Jul 21 '13 at 23:16
  • Can someone tell me why I need the empty braces {} behind ####1 to get the space as desired? In LaTeX2ε an argument doesn’t gobble the following space … – Tobi Jul 22 '13 at 08:08
  • Spaces at the beginning of a line are ignored by rule (and trailing spaces are removed); either type number~####1~% or use \c_space_tl in these situations. – egreg Jul 22 '13 at 08:11
  • Grazie! That makes sense :-) – Tobi Jul 22 '13 at 09:25