GeneralParsing a String using Regular Expressions in LaTeX?!

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
foobar
Posts: 4
Joined: Sat May 15, 2010 2:44 pm

Parsing a String using Regular Expressions in LaTeX?!

Post by foobar »

Hi everyone! This is the code I have:

I have two counters defined

Code: Select all

\newcounter{a}
\newcounter{b}
and a formula

Code: Select all

(a+b)/2
given to the following command (as #1).

Code: Select all

\expandafter\parseValue#1
\setcounter{c}{((\value{\a}+\value{\b})*10/\value{divisor}+5)/10}
The command "parses" the formula (#1) using \expandafter and \parseValue.

Code: Select all

\def\parseValue(#1+#2)/#3{%
  \def\a{#1}
  \def\b{#2}
  \setcounter{divisor}{#3}
}
I now want to handle other formulas (e.g. (a+b+c)/3) with the same code. So I need to get something like a Regular Expression in (La)TeX in order to distinguish between different formulas.

And these are my questions:

1) Is any package out there dealing with Regular Expressions?
2) a. Or do I have to hack my own code using any String Manipulation Package?
2) b. Do you have any experience which packets fit best?
3) Or does a package already deal with the issue I'm working on?

Thanks in advance! :-)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: Parsing a String using Regular Expressions in LaTeX?!

Post by josephwright »

Assuming you use e-TeX, why not just

Code: Select all

\setcounter{c}{\numexpr{(\value{a}+\value{b})/2}}
[\code]
and so forth?
Joseph Wright
foobar
Posts: 4
Joined: Sat May 15, 2010 2:44 pm

Parsing a String using Regular Expressions in LaTeX?!

Post by foobar »

I didn't know the \numexpr command and it is really a much cleaner way to round a number. Thanks for that! But my problem remains the same:

I want to avoid using \value and instead just use the counters name. There are just two types of formulas I want to use:

Code: Select all

1. (a+b)/2
2. (a+b+c)/2
All addends will always be counter names. I would just have to extract and wrap them into the \value command.

Oh, and I think I just found a solution for that! Redefining my parser to

Code: Select all

\def\parseValue(#1)/#2{%
  \def\sum{#1}
  \setcounter{divisor}{#2}
}
and splitting the \sum using a slightly modified version of this code to split a string on plus signs rather than commas will do the job I guess...

Thanks for your post and the \numexpr command! (And maybe there is still a better solution?)
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: Parsing a String using Regular Expressions in LaTeX?!

Post by josephwright »

While there are general solutions (making everything active), you need to remember that as far as TeX is concerned macros, counters and so on need to start with the escape character (\). LaTeX makes counters with internal names \c@<name>, so \newcounter{a} makes a TeX count \c@a. The type of thing you want to do is really not the TeX way (I think you might be best using LuaTeX if you are really determined).
Joseph Wright
Post Reply