8

Since \ExplSyntaxOn changes catcode of : it makes command with weird argument break when using the macro outside.

\documentclass{article}

\begin{document}

\ExplSyntaxOn
\cs_set:Npn \my_foo:w #1:#2\q_stop {
  (#1)
}
\newcommand{\foo}[1]{
  \my_foo:w #1:\q_stop
}

\foo{abc:def} % works
\ExplSyntaxOff

\foo{abc:def} % doesn't work

\end{document}

enter image description here

Is there constant like \c_math_subscript_token but for colon?

5 Answers 5

6

The problem is that in the scope of \ExplSyntaxOn the colon has category code 11, but in the document it has category code 12.

You can store the desired parameter text in a token list and then use its value:

\documentclass{article}

\ExplSyntaxOn

\tl_set:Nx \l_tmpa_tl { ##1 \c_colon_str ##2 }
\exp_last_unbraced:NNV \cs_set:Npn \my_foo:w \l_tmpa_tl \q_stop
 {
  (#1)
 }

\NewExpandableDocumentCommand{\foo}{m}
 {
  \my_foo:w #1:\q_stop
 }

\ExplSyntaxOff

\begin{document}

\foo{abc:def}

\end{document}
4
  • I'm trying to skip an intermediate \l_tmpa_tl variable, but why doesn't \exp_last_unbraced:NNx \cs_set:Npn \my_foo:w { ##1 \c_colon_str ##2 } \q_stop { (#1) } work?
    – antshar
    Commented Aug 9, 2022 at 17:05
  • @antshar Because there is no \exp_last_unbraced:NNx function?
    – egreg
    Commented Aug 9, 2022 at 17:31
  • Oh right.. I forgot that it doesn't have all the possibilities of arguments. That's unfortunate.
    – antshar
    Commented Aug 9, 2022 at 22:47
  • Just figured out a workaround: \exp_args:NNNx \exp_last_unbraced:NNo \cs_set:Npn \my_foo:w { ##1 \c_colon_str ##2 } \q_stop { (#1) }
    – antshar
    Commented Aug 10, 2022 at 11:28
6

Instead of changing the colon in the definition you could also change the catcode before reading the argument, that has the advantage that your command will work inside and outside \ExplSyntax:

\documentclass{article}

\begin{document}

\ExplSyntaxOn
\cs_set:Npn \__antshar_foo:w #1:#2\q_stop {
  (#1)
}

\cs_new:Npn \foo 
 {
  \group_begin: \char_set_catcode_letter:N :
  \__anthshar_foo_aux:n  
 }
\cs_new:Npn \__anthshar_foo_aux:n  #1
 {
  \group_end: \__antshar_foo:w #1:\q_stop
 } 


\foo{abc:def} % works

\ExplSyntaxOff

\foo{abc:def} % works

\end{document}
5

Here is a workaround (but the definition is global).

\documentclass{article}

\begin{document}

\ExplSyntaxOn

{
\catcode`\: = 12
\expandafter \gdef \csname my_foo:w \endcsname #1 : #2 \q_stop  { (#1) }
}

\newcommand{\foo}[1]{ \my_foo:w #1:\q_stop }

\ExplSyntaxOff

\foo{abc:def} % does work

\end{document}
5

You will need to produce a string version of the colon. There are a few ways: one example:

\use:x
  {
    \cs_set:Npn \exp_not:N \my_foo:w
      ##1 \token_to_str:N : ##2 \exp_not:N \q_stop
  }
  { ( #1 ) }
4

You can use TeX primitive \def, no Expl3:

\def\foo#1{\fooA#1\end}
\def\fooA#1:#2\end{(#1)}

\foo{abc:def} % works
2
  • 2
    Yeah with plain tex it's straight forward, I have been asking about latex3 syntax because there : is a special character so it has to be treated specifically.
    – antshar
    Commented Aug 9, 2022 at 22:45
  • 1
    latex3 syntax is only additional complication. A have shown that if we leave it then some things are more simple.
    – wipet
    Commented Aug 10, 2022 at 6:34

You must log in to answer this question.

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