Math & Science ⇒ Simple calculations package
Simple calculations package
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
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
-
- Posts: 162
- Joined: Wed Jun 17, 2009 10:18 pm
Re: Simple calculations package
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
-
- Posts: 162
- Joined: Wed Jun 17, 2009 10:18 pm
Simple calculations package
Code: Select all
\documentclass{article}
\usepackage{pgf}
\begin{document}
87 divided by 17 is approximately \pgfmathparse{int(round(87/17))}\pgfmathresult.
\end{document}
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.
Re: Simple calculations package
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
-
- Posts: 162
- Joined: Wed Jun 17, 2009 10:18 pm
Simple calculations package
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}
Re: Simple calculations package
Your code works perfect, thanks very much for this.
VG
Re: Simple calculations package
\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
Simple calculations 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}
-
- Posts: 162
- Joined: Wed Jun 17, 2009 10:18 pm
Simple calculations package
Code: Select all
\documentclass{article}
\usepackage{pgf,xstring}
\begin{document}
\StrSubstitute{12,000.00}{,}{}[\number]
\pgfmathparse{int(round(\number/17))}\pgfmathresult
\end{document}