I'm looking for a command that converts simple strings to a number
that can be used in a \numexpr. It would be fine if in case the
string starts with a number this one is used but not really
necessary. On the whole any number larger than 0 would be okay.
Has someone an idea?
\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
\newcommand\converttonumber[1]{0#1}
\def\test{1}
\the\numexpr \converttonumber{\test} +1\relax = 2
\def\test{10}
\the\numexpr \converttonumber{\test} +1\relax =11
\def\test{1a}
\the\numexpr \converttonumber{\test} +1\relax =2 (if possible)
\def\test{10a}
\the\numexpr \converttonumber{\test} +1\relax =11 (if possible)
\def\test{abc}
\the\numexpr \converttonumber{\test} +1\relax = a number > 0
\end{document}
--
Ulrike Fischer
With the experimental feature \pdfmatch of pdfTeX 1.30:
\documentclass{article}
\usepackage[T1]{fontenc}
\makeatletter
\newcommand*{\converttonumber}[1]{%
\ifnum\pdfmatch{^([0-9]*)}{#1}=1 %
\expandafter\strip@prefix\pdflastmatch1 %
\else
0%
\fi
}
\makeatother
\begin{document}
\def\test{1}
\the\numexpr \converttonumber{\test} +1\relax = 2
\def\test{10}
\the\numexpr \converttonumber{\test} +1\relax =11
\def\test{1a}
\the\numexpr \converttonumber{\test} +1\relax =2 (if possible)
\def\test{10a}
\the\numexpr \converttonumber{\test} +1\relax =11 (if possible)
\def\test{abc}
\the\numexpr \converttonumber{\test} +1\relax = a number > 0
\end{document}
Yours sincerely
Heiko <ober...@uni-freiburg.de>
greetings, el
on 11/29/07 10:54 AM Heiko Oberdiek said the following:
--
If you want to email me, replace nospam with el
> Ulrike Fischer <ne...@nililand.de> wrote:
>
>> I'm looking for a command that converts simple strings to a number
>> that can be used in a \numexpr. It would be fine if in case the
>> string starts with a number this one is used but not really
>> necessary. On the whole any number larger than 0 would be okay.
>>
>> Has someone an idea?
> With the experimental feature \pdfmatch of pdfTeX 1.30:
>
> \documentclass{article}
> \usepackage[T1]{fontenc}
> \makeatletter
> \newcommand*{\converttonumber}[1]{%
> \ifnum\pdfmatch{^([0-9]*)}{#1}=1 %
> \expandafter\strip@prefix\pdflastmatch1 %
> \else
> 0%
> \fi
> }
How neat. I wasn't aware that you can do regex-searches. I think I
will refine the things a bit more as planed.
Thank you.
--
Ulrike Fischer