5

To become somewhat familiar with the latex3 syntax, I've written a small macro to convert lengths (known in LaTeX).

I found the following on page 206 (229) in the last source3.pdf:

[+ - \ ] * ((? i) nan | inf | (\ d + | \ d * \. \ d +) (\ * e [+ - \ ] *
\ d +)?) \ *

But if I use that in the macro, I get an error message:

Range [+ -  ] out of order in character class.

It seems that the backslash with space is not interpreted correctly. But if I replace

[+ -\ ] with [+ -?]

then it seems to work.

What should be the correct input here?

I would be pleased about a hint.

enter image description here

\documentclass{article}
\usepackage{siunitx}
\sisetup{group-separator={\,},output-decimal-marker={,}}

\ExplSyntaxOn
\NewDocumentCommand { \convertlength } { O{} m m }
 {
   \tl_set:Nn \l_convert_length_tl { #2 }
   \regex_replace_all:nnN { [+-?]*((?i)nan|inf|(\d+|\d*\.\d+)(\ *e[+-?]*\d+)?)\ * } { \c{num}\cB\{ \0 \cE\}\c{,} } \l_convert_length_tl
   \ensuremath{ \text{ \tl_use:N \l_convert_length_tl }  =  \num[#1] { \dim_to_decimal_in_unit:nn { #2 } { 1#3 } } \,\text{ #3 } }
 }
\ExplSyntaxOff

\begin{document}

\convertlength{1.5in}{cm} % cm mm nd pt dd bp in

\end{document} 
11
  • You need an explicit space. So imho with active expl3 syntax \~. Dec 12, 2017 at 14:34
  • 1
    [+-?] is the range from + to ? (cf [a-z]) I would guess [+\-] is what's wanted ( that is a plus or minus) Dec 12, 2017 at 14:44
  • @UlrikeFischer thanks for that hint, thats right.
    – user139826
    Dec 12, 2017 at 14:49
  • 1
    @DavidCarlisle is right. Dec 12, 2017 at 14:50
  • 4
    I'm voting to close this question as off-topic because the fix was mentioned in the comments (escaping the minus character) and the OP didn't clearly specify what this regex is referring to
    – siracusa
    Jul 22, 2019 at 5:55

0