Basic arithmetic in Latex

More and more, I use Latex as scratch paper, sometimes to develop numerical (counter-)examples. I often find it much cleaner and better organized than if I rely on actual pen and paper.

Numerical examples are usually a “back and forth” kind of business, with many moving parts. You have to find the numbers that “make it work here” without “changing things there”.

So finding the right combination of numbers often requires a lot of simple (but error-prone) calculations. In that matter too, Latex can save you time and prevent mistakes.

With pen and paper, you would usually (a) write down your arithmetic (e.g., (1/3)*57 + (1/4)*23 + (5/12)*20), (b) use a calculator or spreadsheet to perform the calculation, and (c) write down the result on your sheet of paper.

There are many ways to make mistakes and loose time in that process. As often, Latex offers a solution.

Have a look at https://tex.stackexchange.com/questions/30081/how-can-i-sum-two-values-and-store-the-result-in-other-variable. Very much like you would if you were using Markdown, Latex — with the help of a particular package — lets you,  (a) write down your arithmetic and (b) simply ask your computer to calculate the result.

The code looks something like this:

\documentclass{article}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\begin{document}

\FPeval{\result}{clip(
(1/3)*57 + (1/4)*23 + (5/12)*20
)}

$(1/3)*57 + (1/4)*23 + (5/12)*20 = \result$

\end{document}

As illustrated in https://tex.stackexchange.com/questions/30081/how-can-i-sum-two-values-and-store-the-result-in-other-variable, you can easily round decimals to your liking:

\documentclass{article}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\begin{document}

\FPeval{\result}{round(
100/3,
1
)}

$100/3 = \result$

\end{document}

 

Advertisements