How to divide two values in LaTex

You should absolutely use the eTeX \numexpr option; it's clear and is supported pretty much everywhere.

If you're interested in the original Knuthian TeX, though, there are also arithmetical operators. For the four functions, you use the TeX primitives \advance, \multiply, and \divide, in a pretty unique and, I think, clever way:

\documentclass{article}
\begin{document}
\def\x{30}
\def\y{10}
$\x \div \y =$
\newcount\a\a=\number\x
\newcount\b\b=\number\y
\divide\a by\b
\def\x{\the\a}
$\x$
\end{document}

So first we print the problem we're doing, for demonstrative purposes:

$\x \div \y =$

The next thing we have to do is convert \x and \y (which TeX views as merely the characters 3 and 0, 1 and 0, not as the decimal numbers 30 and 10) into count values, which TeX does view as decimal numbers. So we create two counters, \a and \b, and then assign the decimal number version of our command sequences \x and \y to them with the following code:

\newcount\a\a=\number\x
\newcount\b\b=\number\y

Now that \a and \b have the correct values (30 and 10), we can do our actual arithmetical operation:

\divide\a by\b
\def\x{\the\a}

This does just what it looks like: it divides \a by \b, the quotient being stored in \a. Then we use the \the directive (another TeX primitive) to assign to \x, your control sequence from earlier, the new value of \a. Then, finally, we print our quotient:

$\x$

That prints the following:

Math equation with TeX's arithmetic.

TeX offers three primitives for the four arithmetical operations:

  • \advance\a by\b Does addition.
  • \advance\a by-\b Does subtraction.
  • \multiply\a by\b Does multiplication.
  • \divide\a by \b Does division.

These all work with counters, glue, muglue, and dimensions, and you can even mix them, though you have to be careful when adding, say, a counter to some glue because the stretchability can be lost.

It is important to note that these all deal with integer division; you won't get fractional values. On the other hand, at least with dimens, they work in scaled points, which are quite small, so the granularity is pretty impressive.


I don' know if this would be enough, but for your example it's enough (it requires eTeX)

\documentclass{scrartcl}
\def\basiceval#1{\the\numexpr#1\relax}
\begin{document}
\def\x{30}
\def\y{10}
\basiceval{\x/\y}
\end{document}

Here's a LuaLaTeX-based solution:

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\begin{document}
\newcommand\x{30}
\newcommand\y{10}
\textbackslash x divided by \textbackslash y is \directlua{tex.sprint(\x/\y)}.

2.5 multiplied by 4 is \directlua{tex.sprint(2.5*4)}.
\end{document}

If you need to do a lot of these simple divisions -- and other simple calcuations too -- it's probably a good idea to create a dedicated macro, say,

\newcommand\mycalc[1]{\directlua{tex.sprint(#1)}}

in the preamble, to economize a bit on typing in the body of the document. Note that the argument of \mycalc isn't limited to divisions -- all calculations that satisfy Lua syntax rules are allowed.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\newcommand\mycalc[1]{\directlua{tex.sprint(#1)}}
\begin{document}
\newcommand\x{30}
\newcommand\y{10}

\textbackslash x divided by \textbackslash y  is \mycalc{\x/\y}.

42 divided by $(5\times7)$ is \mycalc{42/(5*7)}.
\end{document}

Addendum I just noticed that Taco Hoekwater has already proposed the \directlua{tex.sprint(...) solution on TeX.SE -- see his answer to the posting ConTeXt / e-TeX Real Numbers?

Tags:

Programming