Is there a calculator with LaTeX-syntax?

2019-01-21 09:00发布

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.

17条回答
相关推荐>>
2楼-- · 2019-01-21 09:36

Emacs calc-mode accepts latex-input. I use it daily. Press "d", followed by "L" to enter latex input mode. Press "'" to open a prompt where you can paste your tex.

Anyone saing it is not possible is wrong.

查看更多
乱世女痞
3楼-- · 2019-01-21 09:37

As Andy says, the answer is yes there is a calculator that can understand most latex formulas: Emacs.

Try the following steps (assuming vanilla emacs):

  1. Open emacs
  2. Open your .tex file (or activate latex-mode)
  3. position the point somewhere between the two $$ or e.g. inside the begin/end environment of the formula (or even matrix).
  4. use calc embedded mode for maximum awesomeness

So with point in the formula you gave above:

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

press C-x * d to duplicate the formula in the line below and enter calc-embedded mode which should already have activated a latex variant of calc for you. Your buffer now looks like this:

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

$\frac{-37651}{150}$`

Note that the fraction as already been transformed as far as possible. Doing the same again (C-x * d) and pressing c f to convert the fractional into a floating point number yields the following buffer:

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

$\frac{-37651}{150}$

$-251.006666667$

I used C-x * d to duplicate the formula and then enter embedded mode in order to have the intermediate values, however there is also C-x * e which avoids the duplication and simply enters embedded mode for the current formula.

If you are interested you should really have a look at the info page for Emacs Calc - Embedded Mode. And in general the help for the Gnu Emaca Calculator together with the awesome interactive tutorial.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-21 09:38

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)}
查看更多
Emotional °昔
5楼-- · 2019-01-21 09:39

My calculator can do that. To get the formatted output, double-click the result formula and press ctrl+c to copy it.

It can do fairly advanced stuff too (differentiation, easy integrals (and not that easy ones)...).

https://calculator-algebra.org/cgi-bin/calculator?request=calculator&mainInput=%5cfrac%7b1%2b2%7d%7b3%7d%3b%0ad%2fdx%28arctan%20%282x%2b3%29%29

查看更多
Ridiculous、
6楼-- · 2019-01-21 09:40

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/

查看更多
冷血范
7楼-- · 2019-01-21 09:43

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.

You are right. LaTeX as it is does not provide enough info to make any calculations.Moreover, it does not represent any information to do it. But nobody prevents to wright in LaTeX format a text that contains such an information. It is a difficult path, because you need to build a system of rules superimposed on what content ofthe text in Latex format needs to contain that it would be recognizable by your interpreter. And then convince the user that it is necessary to learn, etc. etc... The easiest way to create a logical and intuitive calculator of mathematical expressions. And the expression is already possible to convert latex. It's almost like what you said. This is implemented in the program which I have pointed to. AnEasyCalc allows to type an expression as you type the plane text in any text editor. It checks, calculates and generate LateX string by its own then. Its very easy and rapid work. Just try and you will see that.

查看更多
登录 后发表回答