6

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.

David
  • 607

3 Answers3

13

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.

dgoodmaniii
  • 4,270
  • 1
  • 19
  • 36
  • 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 02 '15 at 17:33
  • Yes; I'll add that to my answer. – dgoodmaniii Jun 02 '15 at 17:34
10

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}
Manuel
  • 27,118
  • Can you provide a full example? I mean with packages included. – David Jun 02 '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 02 '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 02 '15 at 16:37
  • 1
    -1. The proposed method is incorrect if the result isn't integer-valued. – Mico Jun 02 '15 at 17:31
  • @Mico The question isn't clear in this regard: the example is all integers. – Joseph Wright Jun 02 '15 at 17:48
  • @JosephWright - The OP wrote, "Also I may need to divide floats by integer values." Doesn't this suggest that one shouldn't assume that the result of the division will always be integer-valued? – Mico Jun 02 '15 at 17:53
  • @Mico He explicitly said no packages or “long winded commands”, I think this is the furthest one can go (may be using LuaTeX is an option, but I tend to consider that kind of solutions as “another package”). +1 To your solution in any case :P – Manuel Jun 02 '15 at 18:29
  • If the OP's condition -- no packages or "long winded commands" -- is so restrictive as to make a general answer impossible, it's entirely OK to alert him/her to this fact, and to recommend the use of a floating point package (such as fltpoint, and the macro \fpDiv). – Mico Jun 02 '15 at 18:55
  • This method works for me, I used '\the\numexpr\x/\y\relax' as a shortened version for use inline with the content. – David Jun 03 '15 at 06:11
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?

Mico
  • 506,678
  • 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 02 '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 02 '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 02 '15 at 19:22
  • @barbarabeeton - Is it unambiguous enough now, with the parentheses added in? :-) – Mico Jun 02 '15 at 19:28
  • even a \cdot would be unambiguous with the parentheses. thank you. – barbara beeton Jun 02 '15 at 19:36