2

Adapting Joseph's answer in Extract the numerical and non-numerical portion from text to count a non-numerical portion from a text. The idea behind my method is to use regex to seperate the numerical and symbolic part from a text (like in the answer), and store the symbolic and numerical parts as key names and values respectively in a property list. This works perfectly fine. The problem I ram into is when an input is a control sequence like \alpha and sub/superscipt _ and ^. The code shows this issue e.g. \test{\alpha} does not produce \alpha (the symbol).

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\test}{ m }
{
    \simon_test:n {#1}
}

\cs_new:Npn \simon_test:n #1
{
    \prop_clear:N \l_tmpa_prop
    \exp_args:NNn \prop_put:Nnn \l_tmpa_prop {#1} {} % the value is of no importance for the mwe

    \prop_map_inline:Nn \l_tmpa_prop
    {
        ##1
    }
}
\ExplSyntaxOff

\begin{document}
$\test{a_n}$ should be $a_n$

$\test{b^2}$ should be $b^n$

$\test{\alpha}$ should be $\alpha$
\end{document}

enter image description here

I believe this is due to l3prop stores the key names as strings.

The use of a property list in this way is an core part of my code (I could rewrite it to use two sequences, but I found property list easier to use). Is there a way to overcome this, or do I have to rewrite my code?

  • 1
    I'm not sure why you're using a property list for this, as you assign no value to any key. – egreg Sep 15 '18 at 19:49
  • @egreg In my actual code I do (It's just way too long for an MWE). – Simon Sep 15 '18 at 20:01
  • Yes, key names are strings: broadly, you should be using them to look stuff up, not expecting to typeset them. Use an appropriate structure: I'd likely go for a sequence. – Joseph Wright Sep 15 '18 at 20:07
2

The idea behind property list is that it stores key-value pairs and the key is treated as a string (in the expl3 technical sense). Using keys as values is wrong to begin with.

Besides, using \prop_map_inline:Nn doesn't guarantee a precise order, so it should be used to set other things, not generally for typesetting (unless you don't care about the order or never modify the values once the prop is set).

Use \tl_rescan:nn, but keep in mind there can be some secondary issues.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\test}{ m }
{
    \simon_test:n {#1}
}

\cs_new:Npn \simon_test:n #1
{
    \prop_clear:N \l_tmpa_prop
    \prop_put:Nnn \l_tmpa_prop {#1} {} % the value is of no importance for the mwe

    \prop_map_inline:Nn \l_tmpa_prop
    {
      \tl_rescan:nn { } { ##1 }
    }
}
\ExplSyntaxOff

\begin{document}
$\test{a_n}$ should be $a_n$

$\test{b^2}$ should be $b^n$

$\test{\alpha}$ should be $\alpha$
\end{document}

I removed \exp_args:Nnn that does nothing at all. It exists for technical reasons, but it's not even documented.

enter image description here

Alternatively, you can set an auxiliary property list:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\test}{ m }
 {
  \simon_test:n {#1}
 }

\cs_new:Npn \simon_test:n #1
 {
  \prop_clear:N \l_tmpa_prop
  \prop_clear:N \l_tmpb_prop
  \prop_put:Nnn \l_tmpa_prop {#1} {} % the value is of no importance for the mwe
  \prop_put:Nnn \l_tmpb_prop {#1} {#1}
    \prop_map_inline:Nn \l_tmpa_prop
    {
      \prop_item:Nn \l_tmpb_prop { ##1 }
    }
}
\ExplSyntaxOff

\begin{document}
$\test{a_n}$ should be $a_n$

$\test{b^2}$ should be $b^n$

$\test{\alpha}$ should be $\alpha$
\end{document}
  • \tl_rescan:nn worked in my code, but if it can cause problems I'm not aware of, it might not be the ideal way to go. I might aswell go with one sequence for symbols and one sequence for values. – Simon Sep 16 '18 at 5:38
1

I would use a sequence for storing the 'symbols' here

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\test}{ m }
{ \simon_test:n {#1} }

\seq_clear:N \l_tmpa_seq

\cs_new_protected:Npn \simon_test:n #1
  {
    \seq_put_right:Nn \l_tmpa_seq {#1}
    \seq_map_inline:Nn \l_tmpa_seq {##1}
}
\ExplSyntaxOff

\begin{document}
$\test{a_n}$ should be $a_n$

$\test{b^2}$ should be $b^n$

$\test{\alpha}$ should be $\alpha$
\end{document}

though one could also put the data itself into the property list (more complex, and probably needing a full use case to be sure if it's the best plan).

  • It was a sheer coincidence that I discovered property list stored the key names as strings. I think you are right, using sequence are the way to go here. Maybe I will post a new question with a full use case in the future. – Simon Sep 16 '18 at 5:49

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.