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.
Asked
Active
Viewed 136 times
0
Evan Aad
- 11,066
2 Answers
2
You can use \fpeval for numeric expressions
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
-
-
1@EvanAad
\inteval{1+2}although that is more or less\the\numexpr– David Carlisle Nov 27 '22 at 22:01 -
-
1@RaffaeleSantoro no. If you have an old latex you may need
xfppackage to define\fpevalbut it has been defined in the format for a while now. – David Carlisle Nov 28 '22 at 12:17 -
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}
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


numericapackage allows evaluating expressions inLaTeXformat. See https://tex.stackexchange.com/a/648386/218142. – LaTeXereXeTaL Nov 27 '22 at 22:32tikzpicture. 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\tikzcubecommand from an answer they gave to another post of mine as to get rid of its dependence on the global variables\lvcland\cllv, and to replace\edef,\numexpr, and\ifnumby higher-level alternatives. – Evan Aad Nov 27 '22 at 23:08\pgfmathtruncatemacro{\cllv-1}Though, you could just put\cllv-1into 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:\pgfintevalfor a cube with 2•10⁹ levels.) For high precision, you're going to need thefpulibrary but that is not needed to draw stuff – Qrrbrbirlbel Nov 28 '22 at 00:56ifthenelsefunction but it doesn't provide an interface to do to something depending on the evaluation. For usage of styles I always use/utils/ifkey (Code 2) or just something like the.tryhandler as in my answer to your linked Q. – Qrrbrbirlbel Nov 28 '22 at 01:05\ifto 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 toetoolbox's\numdef? – Evan Aad Nov 28 '22 at 05:00\numexpralso sometimes requires\relaxright? I don't want to deal with these low-level commands. – Evan Aad Nov 28 '22 at 05:04\relaxjust 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