How, in the context of a Tex macro, do I do arithmetic on integers and also capture the string representation of the value of an expression so that it can be displayed? Addition and subtraction would do for now, though I might need multiplication and division later. The expressions used in the MWE are simple, but it would be useful to know how to do more sophisticated stuff involving declaration and assignment of variables, and using the values of variables.

Here's a MWE (well, it compiles and produces something, just not what I want). My aim is to get the expressions -#1 and #1+#2 evaluated and decimal representations of their values displayed.

I have deliberately left the code littered with various attempts at getting what I want, to give an idea of what I have tried without success. By searching tex.SE I found references to the packages calc, etex and xlop, so I looked at their documentation, but I don't see anything there that is useful to my purpose.

I'm using pdfTex.

% Found these out from https://tex.stackexchange.com/questions/284977/use-floating-point-with-calc
  \tkzDefPointBy[rotation=center A angle #2](B) \tkzGetPoint{C1}
  \tkzDefPointBy[rotation=center A angle #1](C1) \tkzGetPoint{D1}
  \tkzDefPointBy[rotation=center B angle -#3](A) \tkzGetPoint{D2}
  \tkzDefPointBy[rotation=center B angle -#4](D2) \tkzGetPoint{C2}
  \tkzInterLL(A,C1)(B,C2) \tkzGetPoint{C}
  \tkzInterLL(A,D1)(B,D2) \tkzGetPoint{D}
  \tkzDrawSegments(A,B A,C A,D B,C B,D C,D)
%  \setcounter{dac}{ -#1 }
%  \setcounter{bad}{ #1+#2 }
%  \def\dac{ -#1 }
%  \tkzLabelAngle[pos=6/\value{dac}](D,A,C){\value{dac}}
%  \tkzLabelAngle[pos=6/{dac}](D,A,C){dac}
%  \tkzLabelAngle[pos=6/{\dac}](D,A,C){\dac}
%  \tkzLabelAngle[pos=-6/#1](D,A,C){\value{-#1}}
  \tkzLabelAngle[pos=-6/#1](D,A,C){- #1}
  • Have a look at the apnum macros from Petr Olzak.
    – User
    Commented Jun 19, 2018 at 11:38

1 Answer 1


'Classical' TeX doesn't have a simply 'integer expression' primitive, but e-TeX (routinely available today) does: \numexpr. That can be used directly or perhaps slightly more conveniently using the xfp package, which gives it a LaTeX syntax

Primitive syntax: \number\numexpr -40 + 70\relax

In a LaTeX 'wrapper': \inteval{-40 + 70}

This can be used in your example as


(See References for \dimexpr \numexpr for details of formal documentation of \numexpr.)

The \numexpr/\inteval method is fast and expandable (this can be useful, depending on the context). However, it is possible to parse expressions using macros, and to do processing without e-TeX. This can be important if you need to go beyond TeX' range for integers. Probably most notable for such abilities is xint:

A large value: \thexintexpr -400000000000000000 + 70\relax

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .