3

I'm trying to split macro arguments into two separate strings on 'underscore' character using expl3 package but none of the following attempts (commented line) work. I also tried 'verbatim' argument specifier for xparse NewDocumentCommands

\documentclass{article}
\usepackage{xstring}
\usepackage{xparse}
\usepackage{expl3}

\ExplSyntaxOn
  \NewDocumentCommand{\smartVec}{m}{
    \seq_clear_new:N \vseq_input
    \seq_set_split:Nnn \vseq_input {\_}{#1}

    % \seq_set_split:Nnn \vseq_input {\textunderscore}{#1}        
    % \seq_set_split:Nnn \vseq_input {_}{#1}
    % \seq_set_split:Nnn \vseq_input {\mathunderscore}{#1}
    % \seq_set_split:Nnn \vseq_input {\char`_}{#1}

   \seq_item:Nn \vseq_input {1}
  }
\ExplSyntaxOff

I also tried

\ExplSyntaxOff
  \NewDocumentCommand{\smartVec}{m}{
    \str_clear_new:N \vstr_input
    \str_set:Nn \vstr_input {#1}

    \seq_clear_new:N \vseq_input
    \seq_set_split:Nnn \vseq_input {\_}{\vstr_input}
    \str_clear_new:N \vstr_test
    \seq_pop_left:NN \vseq_input \vstr_test
    \str_use:N \vstr_test
  }
\ExplSyntaxOff

\begin{document}
$\smartVec{a_b}$
\end{document}

Expected results for running this macro should be (at least I expect it to be) $a$ but it shows $a_b$

NOTE: If it is possible I'd prefer to get an answer on how to make my macro work instead of suggesting different solutions (using another package, plain tex etc.) because I'm planning to add more things to this macro based on expl3 package's tools.

NOTE: I've updated texlive-full to 2017 version just before writing this question and nothing changed.

NOTE: I also tried to find whether a string contains underscore character and this also failed. (Looks like expl3 cannot interpret any of five underscore characters that I tried to use)

EDIT: I also tried to split using \seq_set_split:Nnn \sequence {\c_math_subscript_token}{#1} with no results either

4

The underscore has a special status after \ExplSyntaxOn (it is considered a letter, in order to be part of command names), so it doesn't match an underscore in “user space”.

\documentclass{article}
\usepackage{expl3}
\usepackage{xstring}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\smartVec}{ m }
 {
  \kamanji_svec_split:Nn \l_kamanji_svec_input_seq { #1 }
  \seq_item:Nn \l_kamanji_svec_input_seq { 1 }
 }

\seq_new:N \l_kamanji_svec_input_seq

\cs_new:Nn \kamanji_svec_split:Nn
 {
  \seq_set_split:Nxn #1 { \char_generate:nn { `_ } { 8 } } { #2 }
 }
\cs_generate_variant:Nn \seq_set_split:Nnn { Nx }
\ExplSyntaxOff

\begin{document}

$\smartVec{a_b}$

\end{document}

I defined a helper function just to avoid complicated input in the main code.

This prints just “a”.

Please, conform to the guidelines for naming functions and variables. Note that str is the wrong data type when dealing with things such as \seq_pop_left:NN. In this case popping is not necessary, as you can access the item by number; however, depending on your aim this can become necessary.

  • 1
    Your example (copy-pasted with no modifications) does not compile and results in errors about \l_kamanji_svec_input being undefined and \__seq_item:wNn does not match its definition. – Kamanji May 7 '18 at 14:25
  • @Kamanji Sorry about the typo – egreg May 7 '18 at 16:32

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.