views:

2277

answers:

7

When I write math in LaTeX I often need to perform simple arithmetic on numbers in my LaTeX source, like 515.1544 + 454 = ???.

I usually copy-paste the LaTeX code into Google to get the result, but I still have to manually change the syntax, e.g.

\frac{154,7}{25} - (289 - \frac{1337}{42})

must be changed to

154,7/25 - (289 - 1337/42)

It seems trivial to write a program to do this for the most commonly used operations. Is there a calculator which understand this syntax?

EDIT: I know that doing this perfectly is impossible (because of the halting problem). Doing it for the simple cases I need is trivial. \frac, \cdot, \sqrt and a few other tags would do the trick. The program could just return an error for cases it does not understand.

A: 

IIRC Mathematica can do it.

Mathematica can copy something as LaTeX, but evaluating what you paste into in LaTeX markup seemingly isn't possible.
Joey
Oh yes it is ! ToExpression["your tex here", TeXForm]
High Performance Mark
+4  A: 
Joey
I will probably just write a simple script in elisp to do what you suggest. I just wanted to know if it had already been written.
Jørgen Fogh
Not that I know of, at least. But I think with a decent scripting language one can build something that at least works in about an afternoon.
Joey
+1  A: 

There is none, because it is generally not possible.

LaTeX math mode markup is presentational markup and there are cases in which it does not provide enough information to calculate the expression.

That was one of the reasons MathML content markup was created and also why MathML is used in Mathematica. MathML actually is sort of two languages in one:

  • presentation markup
  • content markup

To accomplish what you are after you'll have to have MathML with comibned presentation and content markup (see MathML spec).

In my opinion your best bet is to use MathML (even if it is verbose) and convert to LaTeX when necessary. That said, I also like LaTeX syntax best and maybe what we need is a compact syntax for MathML (something similar in spirit to RelaxNG compact syntax).

Ludwig Weinzierl
+3  A: 

The calc package allows you to do some calculations in source, but only within commands like \setcounter and \addtolength. As far as I can tell, this is not what you want.

If you already use sage, then the sagetex package is pretty awesome (if not, it's overkill). It allows you get nicely formatted output from input like this:

The square of
$\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}$
is \sage{matrix([[1, 2], [3,4]])^2}.

The prime factorization of the current page number is \sage{factor(\thepage)}
Anton Geraschenko
+1  A: 

For calculations with LaTeX you can use a CalcTeX package. This package understand elements of LaTeX language and makes an calculations, for example your problem is avialble on http://sg.bzip.pl/CalcTeX/examples/frac.tgz or just please write

\noindent For calculation please use following enviromentals $515.1544 + 454$ or \[ \frac{154.7}{25}-(289-\frac{1337}{42.}) \] or \begin{equation} 154.7/25-(289-1337/42.) \end{equation}

For more info please visite project web site or contact author of this project.

+1  A: 

You can run an R function called Sweave on a (mostly TeX with some R) file that can replace R expressions with their results in Tex.

A tutorial can be found here: http://www.scribd.com/doc/6451985/Learning-to-Sweave-in-APA-Style

Sam
+2  A: 

The LaTeXCalc project is designed to do just that. It will read a TeX file and do the computations. For more information check out the home page at http://latexcalc.sourceforge.net/

Loserpenguin15