2

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.

\documentclass[a4paper]{article}
\usepackage{tkz-euclide}
\usetkzobj{all}
%\usepackage{calc}
% Found these out from https://tex.stackexchange.com/questions/284977/use-floating-point-with-calc
%\newcounter{dac}
%\newcounter{bad}
\newcommand\trian[4]{
  \tkzDefPoint(0,0){A}
  \tkzDefPoint(1,0){B}
  \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}
  \tkzLabelAngle[pos=6/(#1+#2)](B,A,D){#1+#2}
  \tkzLabelAngle[pos=6/#3](D,B,A){#3}
  \tkzLabelAngle[pos=6/#4](C,B,D){#4}
}
\begin{document}
\begin{figure}[h]
 \centering
 \begin{tikzpicture}[scale=5.0]
  \trian{-40}{70}{50}{20}
 \end{tikzpicture}
\end{figure}
\end{document}
1
  • Have a look at the apnum macros from Petr Olzak.
    – User
    Commented Jun 19, 2018 at 11:38

1 Answer 1

2

'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

\documentclass{article}
\usepackage{xfp}
\begin{document}
Primitive syntax: \number\numexpr -40 + 70\relax

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

This can be used in your example as

\tkzLabelAngle[pos=6/(#1+#2)](B,A,D){\inteval{#1+#2}}

(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:

\documentclass{article}
\usepackage{xintexpr}
\begin{document}
A large value: \thexintexpr -400000000000000000 + 70\relax
\end{document}
1

You must log in to answer this question.

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