0

The xparse package makes writing commands easier and more flexible. The ifthen package does the same to conditionals. Is there a similar package for numeric expressions? Even better if it interacts well with TiKZ, that is to say inside a tikzpicture.

Evan Aad
  • 11,066
  • 1
    The numerica package allows evaluating expressions in LaTeX format. See https://tex.stackexchange.com/a/648386/218142. – LaTeXereXeTaL Nov 27 '22 at 22:32
  • What do you need to do with those numerical expressions? Where do you need them in your TikZ picture? – Qrrbrbirlbel Nov 27 '22 at 22:46
  • @Qrrbrbirlbel Basically what I do in my answer below: accept integer arguments to a command, and perform some simple arithmetic and branching with them. The command's body is a tikzpicture. I want to avoid \numberexpr, \edef, ifx, etc. as much as possible. I find their usage non-intuitive and hard to understand and remember. – Evan Aad Nov 27 '22 at 22:52
  • @Qrrbrbirlbel More concretely, I'm rewriting Marijn's \tikzcube command from an answer they gave to another post of mine as to get rid of its dependence on the global variables \lvcl and \cllv, and to replace \edef, \numexpr, and \ifnum by higher-level alternatives. – Evan Aad Nov 27 '22 at 23:08
  • PGF/TikZ brings its own mathematical engine which it uses basically everywhere: PGFMath. \pgfmathtruncatemacro{\cllv-1} Though, you could just put \cllv-1 into the coordinate specifications because TikZ will forward it to PGFMath anyway and you don't necessary have to pre-evaluate it. Unless your cube has more than 16000 levels you will be fine with PGFMath. (Undocumented, PGF/TikZ has a clone of \inteval: \pgfinteval for a cube with 2•10⁹ levels.) For high precision, you're going to need the fpu library but that is not needed to draw stuff – Qrrbrbirlbel Nov 28 '22 at 00:56
  • PGFMath also has an ifthenelse function but it doesn't provide an interface to do to something depending on the evaluation. For usage of styles I always use /utils/if key (Code 2) or just something like the .try handler as in my answer to your linked Q. – Qrrbrbirlbel Nov 28 '22 at 01:05
  • 1
    I thought \numexpr was intuitive. Try doing it using \advance, \multiply and \divide. – John Kormylo Nov 28 '22 at 02:52
  • @Qrrbrbirlbel Too bad TikZ doesn't have an \if to complement its \foreach. Is it possible to assign the result of a calculation to a variable in TikZ, e.g. \x = 1 + 2\y, similar to etoolbox's \numdef? – Evan Aad Nov 28 '22 at 05:00
  • @JohnKormylo \numexpr also sometimes requires \relax right? I don't want to deal with these low-level commands. – Evan Aad Nov 28 '22 at 05:04
  • \relax just tells the parser to stop adding stuff. You can generally use {\numexpr ...} instead (although {} is also a low level command). – John Kormylo Nov 28 '22 at 16:10

2 Answers2

2

You can use \fpeval for numeric expressions

enter image description here

showing cos 30 degrees is (sqrt 3)/2

\documentclass{article}

\begin{document}

\fpeval{10/3}

\fpeval{cosd 30}

\fpeval{4*(cosd 30)^2}

\end{document}

Similarly you can use \inteval for integer expressions

David Carlisle
  • 757,742
0

Using etoolbox.

\documentclass{article}
\usepackage{etoolbox}
\newcommand{\isSuccessor}[2]% successor, number
{%
   \numdef{\prev}{#1-1}%
   #1 is
   \ifnumequal{\prev}{#2}{indeed}{not}
   the successor of #2.%
}
\begin{document}
\isSuccessor{42}{41}\par
\isSuccessor{43}{41}\par
\isSuccessor{41}{42}
\end{document}

Results of arithmetic operations and tests with etoolbox


Using pgfmath for the mathematical calculations, \edef for the variable assignment, and ifthen for the conditionals.

\documentclass{article}
\usepackage{ifthen}
\usepackage{pgfmath}
\newcommand{\isSuccessor}[2]
{
   \pgfmathparse{int(#1-1)}%
   \edef\prev{\pgfmathresult}%
   #1 is
   \ifthenelse{\prev=#2}{indeed}{not}
   the successor of #2.%
}
\begin{document}
\isSuccessor{42}{41}\par
\isSuccessor{43}{41}\par
\isSuccessor{41}{42}
\end{document}

The output is the save as above.

Evan Aad
  • 11,066