当我在LaTeX中编写数学时,我经常需要对我的LaTeX源中的数字执行简单的算术,例如515.1544 + 454 = ???

我通常会将LaTeX代码复制粘贴到Google中以获得结果,但我仍然需要手动更改语法,例如

  

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

必须更改为

  

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

为最常用的操作编写程序来执行此操作似乎微不足道。 是否有一个理解这种语法的计算器?

编辑: 我知道完全完成是不可能的(因为停止问题)。为我需要的简单案例做这件事是微不足道的。 \ frac,\ cdot,\ sqrt和其他一些标签就行了。该程序可能只是为它不理解的情况返回错误。

有帮助吗?

解决方案

LaTeXCalc项目旨在实现这一目标。它将读取TeX文件并进行计算。有关更多信息,请访问 http://latexcalc.sourceforge.net/

主页。

其他提示

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)}

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.

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

Considering that LaTeX itself is a Turing-complete markup language I strongly doubt you can build something like this that isn't built directly into LaTeX. Furthermore, LaTeX math matkup itself has next to no semantic meaning, it merely describes the visual appearance.

That being said, you can probably hack together something which recognizes a non-programmable subset of LaTeX math markup and spits out the result in the same way. If all you're interested in is simple arithmetics with fractions and integers (careful with decimal fractions, though, as they may appear as 3{,}141... in German texts :)) this shouldn't be too hard. But once you start with integrals, matrices, etc. I fear that LaTeX lacks expressiveness to accurately describe your intentions. It is a document preparation system, after all and thus not very suitable as input for computer algebra systems.

Side note: You can switch to Word which has—in its current version—a math markup language which is sufficiently LaTeX-like (by now it even supports LaTeX markup) and yet still Google-friendly for simpler terms:

Illustration of math markup in Microsoft Word

With the free Microsoft Math add-in you can even let Word calculate expressions in-place:

Illustration of the Microsoft Math add-in for Microsoft Word, calculating an expression

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

There is a way to do what you want just not quite how you describe.

You can use the fp package (\usepackage[options]{fp}) the floating point package will do anything you want; solving equations, adding dividing and many more. Unfortunately it will not read the LaTeX math you instead have to do something a little different, the documentation is very poor so I'll give an example here.

for instance if you want to do (2x3)/5 you would type:

\FPmul\p{2}{3}           % \p is the assignment of the operation 2x3
\FPupn\p{\p{} 7 round}   % upn evaluates the assignment \p and rounds to 7dp
\FPdiv\q{\p}{5}          % divides the assigned value p by 5 names result q
\FPupn\q{\q{} 4 round}   % rounds the result to 4 decimal places and evaluates
$\frac{2\times3}{5}=\FPprint\q$  % This will print the result of the calculations in the math.

the FP commands are always ibvisible, only FPprint prints the result associated with it so your documents will not be messy, FP commands can be placed wherever you wish (not verb) as long as they are before the associated FPprint.

You could just paste it into symbolab which as a bonus has free step by step solutions. Also since symbolab uses mathquill it instantly formats your latex.

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).

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.

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.

IIRC Mathematica can do it.

For performing the math within your LaTeX itself, you might also look into the pgfmath package, which is more powerful and convenient than the calc package. You can find out how to use it from Part VI of The TikZ and PGF Packages Manual, which you can find here (version 2.10 currently): http://mirror.unl.edu/ctan/graphics/pgf/base/doc/generic/pgf/pgfmanual.pdf

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.

<> try the AnEasyCalc program. It allows to get the latex formula very easy: http://steamandwater.od.ua/AnEasyCalc/ :)

This is not exactly what you are asking for but it is a nice package that you can include in a LaTeX document to do all kind of operations including arithmetic, calculus and even vectors and matrices:

The package name is "calculator"

http://mirror.unl.edu/ctan/macros/latex/contrib/calculator/calculator.pdf

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top