4

I need to divide two values in LaTex,

\newcommand{\x}{30}
\newcommand{\y}{10}

Is there a simple way to do this? i.e

x / y = 3

Preferably not using external packages, or a long winded \newcommand function. It needs to be as simple as possible. Also I may need to divide floats by integer values.

  • May be eTeX's \the\numexpr\x/\y\relax? Apart from that, there are packages, for instance \usepackage{expl3} \ExplSyntaxOn \cs_set_eq:NN \fpeval \fp_eval:n \ExplSyntaxOff gives you the power of using in the document \fpeval{\x/\y} – Manuel Jun 2 '15 at 16:11
  • 1
    Can you be more specific about your aim? – egreg Jun 2 '15 at 16:14
  • Why don't you want to add other packages ? – percusse Jun 2 '15 at 17:56
  • The question Handling numbers and calculations is more general than this one and the answers below are, IMHO better, so I think this should stay open and not be marked as a duplicate. More than this, Handling numbers and calculations is marked as being a duplicate of Floating point calculations in LaTeX! – Andrew Jun 2 '15 at 18:43
6

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}
  • Can you provide a full example? I mean with packages included. – David Jun 2 '15 at 16:33
  • 3
    @damorton no packages are needed it is (e)tex primitives, but note that it is integer division so 33/3 is also 3. – David Carlisle Jun 2 '15 at 16:37
  • This doesn't need packages (just eTeX, which is quite common in the most used distributions), you just need to add \documentclass{article} \begin{document} .. \end{document} to compile with LaTeX – Manuel Jun 2 '15 at 16:37
  • -1. The proposed method is incorrect if the result isn't integer-valued. – Mico Jun 2 '15 at 17:31
  • @Mico The question isn't clear in this regard: the example is all integers. – Joseph Wright Jun 2 '15 at 17:48
8

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.

  • The method proposed here has the same defect as the one given by in @Manuel's answer: The printed result will always be an integer, regardless of what the true result is. – Mico Jun 2 '15 at 17:33
  • Yes; I'll add that to my answer. – dgoodmaniii Jun 2 '15 at 17:34
5

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?

  • i find the output of the last example not entirely clear, although the code is. in the output, does the "7" multiply the result of the first two numbers, or is it part of the denominator? needs clarification. – barbara beeton Jun 2 '15 at 19:08
  • @barbarabeeton - I thought it was clear that the \cdot symbol has algebraic priority the the verbal term "divided by". However, that may not be the case! I'll add a pair of parentheses to eliminate any ambiguity. :-) – Mico Jun 2 '15 at 19:15
  • my understanding was that "divided by" and "multiplied by" had the same level of precedence. and both of these have precedence over "plus" or "minus". (but my formal study of this was a long time ago, and memory isn't always reliable.) – barbara beeton Jun 2 '15 at 19:22
  • @barbarabeeton - Is it unambiguous enough now, with the parentheses added in? :-) – Mico Jun 2 '15 at 19:28
  • even a \cdot would be unambiguous with the parentheses. thank you. – barbara beeton Jun 2 '15 at 19:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.