[Tugindia] RE: Counters

Radhakrishnan CV cvr at river-valley.org
Sat Dec 27 05:34:42 CET 2003


>>>>> "Manoj" == Manoj Kummini <kummini at math.ukans.edu> writes:

    Manoj> On Fri, Dec 26, 2003 at 18:42:25hrs +0530, Suresh A.L
    Manoj> wrote:

    Suresh> I am having the pages numbers indicated with alphanumeric
    Suresh> values.

    Suresh> Say Page number: A241

    Suresh> I need to take only the numeric value "241" alone and
    Suresh> asign it to a counter, (without including 'A') to
    Suresh> manipulate the text.  I have to split the text as 'A' and
    Suresh> '241'.

    Manoj> How have you defined \thepage? Its definition should give
    Manoj> us a clue how to remove the letter from the string.

At times, it becomes a necessity to split the numbers which might be
defined to print with alphabets originally for some obvious reasons.
Surely, LaTeX provides many hooks to do the job. Here are two of them:

One way:
-------
\makeatletter
\newcounter{mycount}  % we define counter to grab the number
\newtoks\mytoks       % a token register for the alphabet

\def\grabNo#1{\@grabNo#1\@nil}
\def\@grabNo#1#2\@nil{\mytoks{#1}\setcounter{mycount}{#2}
		\the\mytoks : \themycount
    }
\makeatother

You might suitably use the token and counter registers for your
specific purpose or assign more meaningful names to it.

Another way:
-----------

\makeatletter
\newcounter{mycounter}
\def\grabNo#1{\edef\x{\@car#1\@nil}\relax
     \expandafter\c at mycount\@cdr#1\@nil
		 \x : \themycount
    }
\makeatother

LaTeX provides two commands namely, \@car and \@cdr which would grab
the first character and the rest of the characters respectively.  This
is how LaTeX programmers prefer to do.

Please try the following examples with any one of the above macros
defined:

\grabNo{A234}

\grabNo{X 234}

\grabNo{B 4}

\grabNo{C234789}

Hope this would help to solve your problem.  

Best regards

Radhakrishnan


More information about the tugindia mailing list