1

I'm trying to perform a division and sometimes it works and other times I get and unexpected value.

\documentclass[fleqn]{article}
\usepackage{amsmath}
\usepackage{enumerate}
\usepackage{cancel}
\begin{document}
\newcommand\coeff{5}
\newcommand\addend{13}
\newcommand\result{45}
\newcommand\exaddend{\the\numexpr\addend*\coeff}
\newcommand\resultrest{\the\numexpr\result-\exaddend}
\begin{align*}
        \coeff x+\exaddend&=\result \\
        -\exaddend&=-\exaddend\\
        \coeff x&=\resultrest\\
        \frac{\cancel{\coeff}x}{\cancel{\coeff}}&=\frac{\resultrest}{\coeff}\\
        x&=\boxed{\the\numexpr\resultrest/\coeff\relax}
        \end{align*}\vspace{1em} 
\end{document}

It works if I use fixed values:
It seems the last line \the\numexpr\resultrest/\coeff\relax seems to produce the issue. Please help!

E.Yu
  • 337
  • Although your code seems to compile properly, but you are including many unnecessary commands and format. As a start, you may remove $ symbol, before \begin{aligned} and after \end{aligned}. – hesham Mar 20 '20 at 01:01
  • I can also see no need for the triple successive environment \begin{enumerate} \item \begin{flushleft} $\begin{aligned}. Perhaps \begin{align} or \begin{align*} if you need no equations numbering are enough. – hesham Mar 20 '20 at 01:05
  • If you want align your equations left, you can use fleqn option in your class, e.g., \documentclass[fleqn]{article}. – hesham Mar 20 '20 at 01:15
  • Yes thank you @hesham. That works a lot better. – E.Yu Mar 20 '20 at 01:22
  • For the arithmetic calculation part, you may check out this answer https://tex.stackexchange.com/a/30083/91556 at the second part of fp package. – hesham Mar 20 '20 at 01:24

1 Answers1

3

You defined \extatend and \resultrest with unterminated \numexpr (no \relax), so when you use them in another \numexpr, the expression is effectively inserted literally, so you may evaluate 5-1/2 when you intended (5-1)/2. I suggest you put \relax at the end of both \extatend and \resultrest.

Other alternatives are to define them including parentheses, or if you want only a single fixed value, define them using \edef.

You don't say what unexpected value you get for which input, so there remains the possibility that you just didn't expect integer division.

  • Thanks so much. Can you point me to how to use \edef ? I'm am unfamiliar and learning. – E.Yu Mar 20 '20 at 17:39
  • Welcome! \edef is basic TeX; it is something like \newcommand but "expands" its definition. This is complicated, because "expansion" is a deep detail of TeX, in that some operations are "expandable" and others not, sometimes without obvious reason (\numexpr evaluation is expandable). Go ahead and dive into TeX reading The TeXbook or TeX by Topic, but it is a journey. \numexpr is even an extension of TeX, but has its own odd details, like being delimited by \relax. \edef\exaddend{\the\numexpr\addend*\coeff\relax} – Donald Arseneau Mar 21 '20 at 02:34
  • (The journey is by swimming, if you follow that mixed metaphor.) Remember, adding \relax to the end of the \newcommand is sufficient; you don't need \edef. – Donald Arseneau Mar 21 '20 at 02:39