Math & ScienceSimple calculations package

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
User1802

Simple calculations package

Post by User1802 »

Hello,

Just trying to find out a package that can help me divide a number and potentially round it up/down to an integer, however, all my search results are showing up mathematical formatting topics.

What packages do you know to create division, or at least difference?

Many thanks,
VG
Last edited by User1802 on Mon Jun 13, 2011 3:06 pm, edited 1 time in total.

Recommended reading 2024:

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

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

torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

Simple calculations package

Post by torbjorn t. »

There's pgfmath, which is part of pgf, and fp. Another option, if you can use the luatex engine, is to let Lua do the calculation in a \directlua environment.
User1802

Re: Simple calculations package

Post by User1802 »

Thank you T,

Just wondering, which macro should I call, can you give me an example? I'm a rookie yet and the macro source doesn't help me much. Couldn't find much documentation on simpler things either.

Many thanks,
VG
torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

Simple calculations package

Post by torbjorn t. »

An example with pgfmath:

Code: Select all

\documentclass{article}
\usepackage{pgf}
\begin{document}
87 divided by 17 is approximately \pgfmathparse{int(round(87/17))}\pgfmathresult.
\end{document}
This prints "87 divided by 17 is approximately 5."

What happens here is that pgfmathparse evaluates the expression given in the braces, and pgfmathresult prints the result of pgfmathparse. The round function rounds the value in its parenthesis to the nearest whole integer, but the result is still printed with a decimal, e.g. "5.0". That's why I've also used the int function, which strips away all decimals, and leaves just the integer.
User1802

Re: Simple calculations package

Post by User1802 »

Thanks T,

Just playing with it now, and I have a latex "variable" defined, like this:

\newcommand{\MYNUMBER}{2000}

how can I introduce it inside the formula, I tried these three and I get an error:

- lorem \pgfmathparse{int(round(\MYNUMBER/12))}\pgfmathresult ipsum
- lorem \pgfmathparse{int(round(\MYNUMBER{}/12))}\pgfmathresult ipsum
- lorem \pgfmathparse{int(round({\MYNUMBER{}}/12))}\pgfmathresult ipsum

Thank you again,
VG
torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

Simple calculations package

Post by torbjorn t. »

The code below works fine here, so you need to give us more information, like what error you get.

It could be that you have an older version of PGF, in which case an update will solve the problem. To find out which version you have, add \listfiles before the \documentclass statement (as below), and compile the document. Somewhere in the log-file you'll find a list of the packages loaded and which version they have.

If you need instructions on how to upgrade packages, please tell us which TeX distribution (TeX Live, MikTeX, MacTeX) you're using.

Code: Select all

\listfiles
\documentclass{article}
\usepackage{pgf}

\newcommand{\MYNUMBER}{2000}

\begin{document}

\MYNUMBER\ divided by 17 is approximately \pgfmathparse{int(round(\MYNUMBER/17))}\pgfmathresult.

\end{document}
User1802

Re: Simple calculations package

Post by User1802 »

Thank you T, you are right, the problem is because of the way the number was written, it was written "2,000", please notice the comma "thousands" separator.

Your code works perfect, thanks very much for this.

VG
User1802

Re: Simple calculations package

Post by User1802 »

I am trying the following line of code and I get an error:

\pgfmathparse{int(round({\StrSubstitute{12,000.00}{,}{}}/17))}\pgfmathresult

I'm trying to remove the , character, however I get an error. Trying things separately, works fine:

\StrSubstitute{12,000.00}{,}{}

and

\pgfmathparse{int(round(12000.00/17))}\pgfmathresult

However, when I put things together as seen in the first example, they don't work.

Any advice on this?

VG
unbonpetit

Simple calculations package

Post by unbonpetit »

Why don't you use the fp package?

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{fp,xstring}
\usepackage[autolanguage]{numprint}
\begin{document}
\def\mynumber{120,000.00}
\def\mydiv{17}
$\mynumber/\mydiv=%
\StrDel\mynumber,[\mynumber]%
\FPeval\myresult{round(\mynumber/\mydiv,0)}%
\numprint\myresult$
\end{document}
torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

Simple calculations package

Post by torbjorn t. »

Probably has to do with some inner workings of TeX, but there I cannot help you. An option is to save the commaless number to a macro, and use that in the calculation.

Code: Select all

\documentclass{article}
\usepackage{pgf,xstring}
\begin{document}
\StrSubstitute{12,000.00}{,}{}[\number]
\pgfmathparse{int(round(\number/17))}\pgfmathresult
\end{document}
Post Reply