Not really a proper answer, but perhaps it is useful as hint:
Based on what buffers do, the filecontents
environment combined with \input
(for \getbuffer
) and \verbatiminput
(for \typebuffer
) comes closest, I think.
Thanks all for the comments, and Altermundus for your alternative answer. However, I really think that for users that would like to enjoy a higher level of abstraction (as opposed to low-level macro hackery) a much better interface for defining such simple functions could be implemented. We don't really have to go down to low-level TeX coding.
For the moment I'll forget about the issue with variable namespaces, but with your help and some tinkering with macros, reading the TikZ sources and lots of trial and error, I was already able to provide something much closer to what I'm looking for.
\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox} % for \ifdefstring
\makeatletter
\newcommand{\setmath}[2]{\pgfmathparse{#2}\edef#1{\pgfmathresult}}
\newcommand{\domath}[1]{\pgfmathparse{#1}\pgfmathresult}
\newcommand{\ifmath}[1]{\pgfmathparse{equal(#1,0)}\ifdefstring\pgfmathresult{0}}
\newcommand{\whilemath}[2]{\ifmath{#1}{#2\whilemath{#1}{#2}}{}}
\newcommand{\returnmath}[1]{\pgfmathparse{#1}\pgfmath@smuggleone\pgfmathresult}
\newcommand{\newmathfunction}[3]{\pgfmathdeclarefunction{#1}{#2}{\begingroup#3\endgroup}}
\makeatother
\newmathfunction{mygcd}{2}{%
\setmath\a{#1}%
\setmath\b{#2}%
\whilemath{\b}{%
\setmath\t{\b}%
\setmath\b{mod(\a,\b)}%
\setmath\a{\t}%
}%
\returnmath{int(\a)}%
}
\begin{document}
\setmath\a{15}
\setmath\b{6}
gcd(\a,\b) = \domath{mygcd(\a,\b)}.
\end{document}
Now you can actually read the code to compute the gcd, which was pretty much translated line by line from my original pseudocode. I know that this might be grossly inefficient but, who cares, I just want to compute the coordinates for a few points, I'm not trying to do sophisticated physics simulations here.
I would greatly appreciate any comments and suggestions to the programming interface that I propose; in particular using etoolbox
just to compare against the token 0
seems like an overkill, but etoolbox
was something that I could also easily learn.
Best Answer
No. It is based on TeX's
\ifnum
, and only<
,=
,>
is supported.Use eTeX primitive
\numexpr
:Or you can use
etoolbox
package (looks awful for this simple bool expression, I agree):