1

How do I access just one element of a list sub-delimited with forward slashes passed in as an argument?

\def\MacroTestOne#1{
    %How do I just pass 2 in here?
}


\def\MacroTestTwo#1{
    \MacroTestOne{#1b}
}

\documentclass{article}

\begin{document}

     \MacroTestTwo{1/2/3}

\end{document}

I'd like to be able to access just the two passed in to MacroTestTwo in MacroTestOne. Is there an easy way to do this?

  • 2
    you said forward slashes which would be 1/2/3 but you have shown backslashes, there is a big difference as 1/2/3 is 5 tokens, the digits 1,2,3 separated by /, but 1\2\3 is three tokens, the digit 1 the command named \2 and the command named \3 – David Carlisle May 14 '18 at 13:10
  • 1
    If you HAD used forward slashes, you could use the xstring package to separate them. Also see https://tex.stackexchange.com/questions/233085/basics-of-parsing?s=1|21.7371 – John Kormylo May 14 '18 at 13:11
  • Yes, I did mean forwards slashes. I updated the code above. – Kevin Gregory May 14 '18 at 13:30

2 Answers2

3

Assuming that you did mean forward slashes you can use

\def\MacroTestOne#1{\zzz#1/\relax}
\def\zzz#1/#2/#3\relax{#2}


\def\MacroTestTwo#1{%
    \MacroTestOne{#1b}%
}%

\documentclass{article}

\begin{document}

     \MacroTestTwo{1/2/3}

\end{document}

which makes 2 I did wonder if you intended to get 2b in which case the definition should be

\def\MacroTestTwo#1{%
    \MacroTestOne{#1}b%
}%
David Carlisle
  • 757,742
2

If you want arbitrary length lists it's more convenient to use commas as separators:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\extractitem}{smm}
 {
  \IfBooleanTF{#1}
   {
    \clist_item:Vn #3 { #2 }
   }
   {
    \clist_item:nn { #3 } { #2 }
   }
 }
\cs_generate_variant:Nn \clist_item:nn { V }
\ExplSyntaxOff

\begin{document}

\newcommand{\mylist}{1,2,3,4,5}

\extractitem{2}{1,2,3,4,5}

\extractitem*{4}{\mylist}

\end{document}

If you insist on slashes, you need to duplicate the code for \clist_item:nn.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\cs_new:Npn \kevin_slashlist_item:nn #1#2
  {
    \kevin__slashlist_item:ffnN
      { \kevin_slashlist_count:n {#1} }
      { \int_eval:n {#2} }
      {#1}
      \kevin__slashlist_item_n:nw
  }
\cs_new:Npn \kevin__slashlist_item:nnnN #1#2#3#4
  {
    \int_compare:nNnTF {#2} < 0
      {
        \int_compare:nNnTF {#2} < { - #1 }
          { \use_none_delimit_by_q_stop:w }
          { \exp_args:Nf #4 { \int_eval:n { #2 + 1 + #1 } } }
      }
      {
        \int_compare:nNnTF {#2} > {#1}
          { \use_none_delimit_by_q_stop:w }
          { #4 {#2} }
      }
    { } / #3 / \q_stop
  }
\cs_generate_variant:Nn \kevin__slashlist_item:nnnN { ffo, ff }
\cs_new:Npn \kevin__slashlist_item_n:nw #1
  { \kevin__slashlist_item_n_loop:nw {#1} \prg_do_nothing: }
\cs_new:Npn \kevin__slashlist_item_n_loop:nw #1 #2/
  {
    \exp_args:No \tl_if_blank:nTF {#2}
      { \kevin__slashlist_item_n_loop:nw {#1} \prg_do_nothing: }
      {
        \int_compare:nNnTF {#1} = 0
          { \exp_args:No \kevin__slashlist_item_n_end:n {#2} }
          {
            \exp_args:Nf \kevin__slashlist_item_n_loop:nw
              { \int_eval:n { #1 - 1 } }
              \prg_do_nothing:
          }
      }
  }
\cs_new:Npn \kevin__slashlist_item_n_end:n #1 #2 \q_stop
  { \tl_trim_spaces_apply:nN {#1} \kevin__slashlist_item_n_strip:n }
\cs_new:Npn \kevin__slashlist_item_n_strip:n #1 { \kevin__slashlist_item_n_strip:w #1 / }
\cs_new:Npn \kevin__slashlist_item_n_strip:w #1 / { \exp_not:n {#1} }

\cs_new:Npx \kevin_slashlist_count:n #1
  {
    \exp_not:N \int_eval:n
      {
        0
        \exp_not:N \kevin__slashlist_count:w \c_space_tl
        #1 \exp_not:n { / \q_recursion_tail / \q_recursion_stop }
      }
  }
\cs_new:Npn \kevin__slashlist_count:n #1 { + 1 }
\cs_new:Npx \kevin__slashlist_count:w #1 /
  {
    \exp_not:n { \exp_args:Nf \quark_if_recursion_tail_stop:n } {#1}
    \exp_not:N \tl_if_blank:nF {#1} { + 1 }
    \exp_not:N \kevin__slashlist_count:w \c_space_tl
  }

\NewExpandableDocumentCommand{\slashlistitem}{smm}
 {
  \IfBooleanTF{#1}
   {
    \kevin_slashlist_item:Vn #3 { #2 }
   }
   {
    \kevin_slashlist_item:nn { #3 } { #2 }
   }
 }
\cs_generate_variant:Nn \kevin_slashlist_item:nn { V }

\ExplSyntaxOff

\begin{document}

\newcommand{\mylist}{1/2/3/4/5}

\slashlistitem{2}{1/2/3/4/5}

\slashlistitem*{4}{\mylist}

\end{document}

Both example documents print the same.

enter image description here

egreg
  • 1,121,712