6

Why when I try to use \int_from_roman:n {\l_roman_str} with \l_roman_str containing string (roman number), it does not work? As fo me, it's not obvious at all.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn

\str_new:N \l_roman_str

\NewDocumentCommand{\RomanToInt}{ m }
{
    \str_set:Nn \l_roman_str {#1}
    \int_from_roman:n {\l_roman_str} % this one does not work
    %\int_from_roman:n {#1} % this one well done
}

\ExplSyntaxOff

\begin{document}

\RomanToInt{mmc}

\end{document}
2
  • 2
    We have a good answer, but remember the general principle: unless documented otherwise, expl3 functions act on the input as given not on the value or expansion of the input. (Ultimately TeX is a macro-expansion system.)
    – Joseph Wright
    May 29, 2017 at 19:14
  • You have a series of very similar questions and in none of them setting a variable seems necessary for the result you want. I understand these are just examples, but perhaps more context will give better ideas on how to accomplish your purpose.
    – egreg
    May 29, 2017 at 23:25

1 Answer 1

5

\int_from_roman:n does not convert the input \l_roman_str to its expansion but uses l_roman_str as literal value, which is 'nonsense' as input for that macro, leading to -1 as output.

However, \int_from_roman:V does use the content of \l_roman_str, provided, the corresponding variant is given with \cs_generate_variant:Nn.

Now, there might be reasons why \int_from_roman:n{#1} is not used directly, but that's not clear at the moment, so defining the variant is one possibility to get around this issue when the string representation of the number is to be stored in a \str variable first...

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn

\ior_new:N \g_file_ior
\str_new:N \l_roman_str

\cs_generate_variant:Nn \int_from_roman:n {V}

\NewDocumentCommand{\CharCount}{ m }
{
    \str_set:Nn \l_roman_str {#1}
    \int_from_roman:V \l_roman_str
    %\int_from_roman:n {#1} % this one well done
}

\ExplSyntaxOff

\begin{document}

\CharCount{mmc}

\end{document}
4
  • +1 for your excellent work. Very nice. To my friends always upvote.
    – Sebastiano
    May 29, 2017 at 20:36
  • \int_from_roman:n internally does \tl_to_str:n {#1}. May 31, 2017 at 5:37
  • It's also documented: »The roman numeral is first converted to a string, with no expansion.« May 31, 2017 at 5:38
  • @HenriMenke: That' s basically what I wrote right from the start
    – user31729
    May 31, 2017 at 7:07

You must log in to answer this question.

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