4

I feel very silly. Basically I want to divide the result of an addition with \numexpr, such as:

(10 + 5) / 3

The problem is that if I put parentheses there, like \numexpr (10+5)/3 \relax my online editor gives me back a syntax error. The only way I found so far is doing:

\numexpr \numexpr 10+5 \relax / 3 \relax

But honestly it's a pain to read. Is there a better way? Thanks!

Gaspa79
  • 143
  • 3
    \the\numexpr(10 + 5) / 3\relax prints 5 an no error... – Phelype Oleinik Jun 18 '20 at 19:21
  • 1
    Please, show more context of the error (minimal example). The \numexpr (10+5)/3\relax cannot raise arror. – wipet Jun 18 '20 at 19:30
  • It raises an error for some reason in my online latex editor. Anyway the package xfp has what I need. I also found \fpeval after that and it's awesome. Thanks so much! – Gaspa79 Jun 18 '20 at 21:29

3 Answers3

5
\documentclass{article}
\usepackage{xfp}
\begin{document}

\inteval{(10+5)/3}

\end{document}
user187802
  • 16,850
3

Here's a LuaLaTeX-based solution. It assumes you're doing integer arithmetic, mainly because your code example uses \numexpr. If the result needn't be integer-valued, just omit the math.floor part.

\documentclass{article}
\newcommand\inteval[1]{\directlua{tex.sprint(math.floor(#1))}}
\begin{document}
\inteval{( 10 + 5 ) / 3 }
\end{document}
Mico
  • 506,678
3

A solution with PSTricks only for fun purposes.

\documentclass[border=\dimexpr355pt/113\relax]{standalone}
\usepackage{pst-calculate}
\begin{document}

\pscalculate{(10+5)/2}

\end{document}

enter image description here

Or if you want to get the output in a fractional form, you can use Computer Algebra System named SageMath that can be invoked from LaTeX via sagetex package as follows. An extra setup is needed, let me know if you need it.

\documentclass{standalone}
\usepackage{sagetex}
\begin{document}

$\sage{(10+5)/2}$

\end{document}

enter image description here

Display Name
  • 46,933