Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert a string to a number

28 views
Skip to first unread message

Ulrike Fischer

unread,
Nov 28, 2007, 8:57:39 AM11/28/07
to
Hello,

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

Heiko Oberdiek

unread,
Nov 29, 2007, 3:54:22 AM11/29/07
to
Ulrike Fischer <ne...@nililand.de> wrote:

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>

Eberhard Lisse

unread,
Nov 29, 2007, 7:28:04 AM11/29/07
to
Make a distance out of it then you should be able to compare easily.

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

unread,
Nov 29, 2007, 11:21:43 AM11/29/07
to
Am Thu, 29 Nov 2007 09:54:22 +0100 schrieb Heiko Oberdiek:

> 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

0 new messages