% Copyright 2007 by Mark Wibrow % % This file may be distributed and/or modified % % 1. under the LaTeX Project Public License and/or % 2. under the GNU Free Documentation License. % % See the file doc/generic/pgf/licenses/LICENSE for more details. % \section{Evaluating Mathematical Expressions} The easiest way of using \pgfname's mathematical engine is to provide a mathematical expression given in the usual infix notation (such as |1cm+4*2cm/5.5| or |2*3+3*sin(30)|). This expression can be parsed by the mathematical engine and the result be placed in a dimension register, a counter, or a macro. Supported are infix mathematical operations involving integers and non-integers, with or without units. It should be noted that all calculations must not exceed $\pm16383.99999$ at \emph{any} point, because the underlying algorithms rely on \TeX{} dimensions. This means that many of the underlying algorithms are necessarily approximate. It also means that some of the algorithms are not very fast. \TeX{} is, after all, a typesetting language and not ideally suited to relatively advanced mathematical operations. However, it is possible to change the algorithms as described in Section~\ref{pgfmath-reimplement}. In the present section, the high-level macros for parsing an expression are explained first, then the syntax for expression is explained. \subsection{Commands for Parsing Expressions} \label{pgfmath-registers} \label{pgfmath-parsing} The basic command for invoking the parser of \pgfname's mathematical engine is the following: \begin{command}{\pgfmathparse\marg{expression}} This macro parses \meta{expression} and returns the result without units in the macro |\pgfmathresult|. \example |\pgfmathparse{2pt+3.5pt}| will set |\pgfmathresult| to the text |5.5|. In the following, the special properties of this command are explained. The exact syntax of mathematical expressions is explained in Section~\ref{pgfmath-syntax}. \begin{itemize} \item The result stored in the macro |\pgfmathresult| is a decimal \emph{without units}. This is true regardless of whether the \meta{expression} contains any unit specification. But, any units specified will be converted to points first. \begin{codeexample}[] \pgfmathparse{2pt+3.4pt} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{2cm+3.4cm} \pgfmathresult \end{codeexample} \item If no units are specified \emph{at any point} in the expression, the result will be multiplied by the value in |\pgfmathresultunitscale|, which can be a number or a dimension (which will be converted to points). By default it is set to 1, but can be changed with |\pgfmathsetresultunitscale|. Note that the result will still be a number \emph{without} units. \begin{codeexample}[] \pgfmathparse{2pt+3.4pt} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathsetresultunitscale{1cm} \pgfmathparse{2+3.4} \pgfmathresult \end{codeexample} \pgfmathsetresultunitscale{1pt} \item You can check whether an expression contained a unit using the \TeX-if |\||ifpgfmathunitsdeclared|. After a call of |\pgfmathparse| this if will be true exactly if some unit was encountered in the expression. \item The parser handles numbers with or without units regardless of the operation. \begin{codeexample}[] \pgfmathparse{54pt/3cm*2.1} \pgfmathresult \end{codeexample} \item the parser can cope with \TeX{} registers, including those preceded by |\the|. \makeatletter \begin{codeexample}[] \pgf@x=12.34pt \c@pgf@counta=5 \pgfmathparse{\pgf@x+\c@pgf@counta*6} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgf@x=56.78pt \pgfmathparse{\pgf@x+\the\pgf@x} \pgfmathresult \end{codeexample} \item \TeX{} dimension registers can be multiplied without the |*| operator by preceding them with a number (\emph{not} a function), or a count register. \begin{codeexample}[] \c@pgf@counta=-4 \pgf@x=10pt \pgfmathparse{.5\pgf@x-\c@pgf@counta\pgf@x} \pgfmathresult \end{codeexample} \item Parenthesis can be used to group operations. \begin{codeexample}[] \pgfmathparse{(4pt+0.5)*3} \pgfmathresult \end{codeexample} \item functions are recognized, so it is possible to parse |sin(.5*pi r)*60|, which means ``the sine of $0.5$ times $\pi$ radians, multiplied by 60''. The argument of most functions can be any expression. \begin{codeexample}[] \pgfmathparse{sin(pi/2 r)*60} \pgfmathresult \end{codeexample} \item Scientific notation in the form |1.234e+4| is recognised (but the restriction on the range of values still applies). The exponent symbol can be upper or lower case (i.e., |E| or |e|). \begin{codeexample}[] \pgfmathparse{1.234567891e-2} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{1.234567891e4} \pgfmathresult \end{codeexample} \end{itemize} \end{command} \begin{command}{\pgfmathqparse\marg{expression}} This macro is similar to |\pgfmathparse|: it parses \meta{expression} and returns the result in the macro |\pgfmathresult|. It differs in two respects. Firstly, |\pgfmathqparse| does not parse functions or scientific notation. Secondly, numbers in \meta{expression} \emph{must} specify a \TeX{} unit (except in such instances as |0.5\pgf@x|), which greatly simplifies the problem of parsing of non-integers. As a result of these restrictions |\pgfmathqparse| is about twice as fast as |\pgfmathparse|. Note that the result will still be a number \emph{without} units. \end{command} \begin{command}{\pgfmathsetresultunitscale\marg{number or dimension}} Sets the value in |\pgfmathresultunitscale|, which scales the result of an expression parsed with |\pgfmathparse|, if that expression contains no units \emph{at any point}. The argument can be an integer, non-integer or a dimension, but the result will still be a number \emph{without} units. Note, that this will affect |\pgfmathsetlength| and friends, but not if the expression starts with |+| (which switches parsing off). By default the value in |\pgfmathresultunitscale| is 1. \end{command} Instead of the |\pgfmathparse| macro you can also wrapper commands, whose usage is very similar to their cousins in the \calcname{} package. The only difference is that the expressions can be any expression that is handled by |\pgfmathparse|. For all of the following commands, if \meta{expression} starts with |+|, no parsing is done and a simple assignment or increment is done using normal \TeX\ assignments or increments. This will be orders of magnitude faster than calling the parser. \begin{command}{\pgfmathsetlength\marg{dimension register}\marg{expression}} Sets the length of the \TeX{} \meta{dimension register}, to the value (in points) specified by \meta{expression}. The \meta{expression} will be parsed using |\pgfmathparse|. \end{command} \begin{command}{\pgfmathaddtolength\marg{dimension register}\marg{expression}} Adds the value (in points) of \meta{expression} to the \TeX{} \meta{dimension register}. \end{command} \begin{command}{\pgfmathsetcount\marg{count register}\marg{expression}} Sets the value of the \TeX{} \meta{count register}, to the \emph{truncated} value specified by \meta{expression}. \end{command} \begin{command}{\pgfmathaddtocount\marg{count register}\marg{expression}} Adds the \emph{truncated} value of \meta{expression} to the \TeX{} \meta{count register}. \end{command} \begin{command}{\pgfmathsetcounter\marg{counter}\marg{expression}} Sets the value of the \LaTeX{} \meta{counter}, to the \emph{truncated} value specified by \meta{expression}. \end{command} \begin{command}{\pgfmathaddtocounter\marg{counter}\marg{expression}} Adds the \emph{truncated} value of \meta{expression} to \meta{counter}. \end{command} % \begin{command}{\pgfmathnewcounter\marg{counter}} % This is simply a version of the \LaTeX{} macro |\newcounter|, % implemented to maintain consistency (consistency is good, % inconsistency is evil). Considering |\pgfmathnewcounter{foo}|, this % creates a new count register |\c@foo|, and a macro |\thefoo|, which % returns the value in |\c@foo|. % \end{command} \begin{command}{\pgfmathsetmacro\marg{macro}\marg{expression}} Defines \meta{macro} as the value of \meta{expression}. The result is a decimal \emph{without} units. \end{command} \begin{command}{\pgfmathsetlengthmacro\marg{macro}\marg{expression}} Defines \meta{macro} as the value of \meta{expression} \LaTeX{}\emph{in points}. \end{command} \begin{command}{\pgfmathtruncatemacro\marg{macro}\marg{expression}} Defines \meta{macro} as the truncated value of \meta{expression}. \end{command} \subsection{Syntax for mathematical expressions} \label{pgfmath-syntax} The syntax for the expressions recognized by |\pgfmathparse| and friends is straightfoward, and the following operations and functions are currently recognized: \begin{math-operator}{\mvar{x}\ +\ \mvar{y}} Adds \mvar{y} to \mvar{x}. \begin{codeexample}[] \pgfmathparse{4+2pt} \pgfmathresult \end{codeexample} \end{math-operator} \begin{math-operator}{\mvar{x}\ -\ \mvar{y}} Subtracts \mvar{y} from \mvar{x}. \begin{codeexample}[] \pgfmathparse{155.35-4cm} \pgfmathresult \end{codeexample} \end{math-operator} \begin{math-operator}{\mvar{x}\ *\ \mvar{y}} Multiplies \mvar{x} by \mvar{y}. \begin{codeexample}[] \pgfmathparse{3.9pt*4.56} \pgfmathresult \end{codeexample} \end{math-operator} \begin{math-operator}{\mvar{x}\ /\ \mvar{y}} Divides \mvar{x} by \mvar{y}. \begin{codeexample}[] \pgfmathparse{-31.6pt/17} \pgfmathresult \end{codeexample} \end{math-operator} \begin{math-operator}{\mvar{x}\ {\char94}\ \mvar{y}} Raises \mvar{x} to the power \mvar{y}. For greatest accuracy \mvar{y} should be an integer. If \mvar{y} is not an integer the actual calculation will be an approximation of $e^{y\ln(x)}$. { \catcode`\^=7 \begin{codeexample}[] \pgfmathparse{2.3^4} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{2^-4} \pgfmathresult \end{codeexample} } \end{math-operator} \begin{math-operator}{\mvar{x}\ ==\ \mvar{y}} This evaluates to |1| if \mvar{x} equals \mvar{y}, or |0| if \mvar{x} does not equal \mvar{y}. Note that equalities (and inequalities) are evaluated left to right, and are only evaluated when another equality (or inequality) operator is scanned, or the end of the current group or parse is reached. So |5+4==3+2==9| results in |0| because |5+4| does not equal |3+2|, resulting in zero, and the second equality is therefore evaluating |0==9|. \begin{codeexample}[] \pgfmathparse{3*5==15} \pgfmathresult \end{codeexample} \end{math-operator} \begin{math-operator}{\mvar{x}\ >\ \mvar{y}} This evaluates to |1| if \mvar{x} is greater than \mvar{y}, or |0| if \mvar{x} is smaller or equal to \mvar{y}. \begin{codeexample}[] \pgfmathparse{17>4.2*1.97+4} \pgfmathresult \end{codeexample} \end{math-operator} \begin{math-operator}{\mvar{x}\ <\ \mvar{y}} This evaluates to |1| if \mvar{x} is smaller than \mvar{y}, or |0| if \mvar{x} is greater or equal to \mvar{y}. \begin{codeexample}[] \pgfmathparse{2<-5.2/-3.6-2} \pgfmathresult \end{codeexample} \end{math-operator} \begin{math-function}{mod(\mvar{x},\mvar{y})} This evaluates \mvar{x} modulo \mvar{y} (using truncated division). This function cannot be nested inside itself or the functions |max|, |min| or |pow|. \begin{codeexample}[] \pgfmathparse{mod(20,6)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{max(\mvar{x},\mvar{y})} This evaluates to the maximum of \mvar{x} or \mvar{y}. This function cannot be nested inside itself or the functions |min|, |mod| or |pow|. \begin{codeexample}[] \pgfmathparse{max(17,23)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{min(\mvar{x},\mvar{y})} This evaluates to the minimum of \mvar{x} or \mvar{y}. This function cannot be nested inside itself or the functions |max|, |mod| or |pow|. \begin{codeexample}[] \pgfmathparse{min(17,23)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{abs(\mvar{x})} Evaluates the absolute value of $x$. \begin{codeexample}[] \pgfmathparse{abs(-5)} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{-abs(4*-3)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{round(\mvar{x})} Rounds \mvar{x} to the nearest integer. It uses ``asymmetric half-up'' rounding. So |1.5| is rounded to |2|, but |-1.5| is rounded to |-2| (\emph{not} |0|). \begin{codeexample}[] \pgfmathparse{round(32.5/17)} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{round(398/12)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{floor(\mvar{x})} Rounds \mvar{x} down to the nearest integer. \begin{codeexample}[] \pgfmathparse{floor(32.5/17)} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{floor(398/12)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{ceil(\mvar{x})} Rounds \mvar{x} up to the nearest integer. \begin{codeexample}[] \pgfmathparse{ceil(32.5/17)} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{ceil(398/12)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{exp(\mvar{x})} { \catcode`\^=7 Maclaurin series for $e^x$. } \begin{codeexample}[] \pgfmathparse{exp(1)} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{exp(2.34)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{ln(\mvar{x})} { \catcode`\^=7 An approximation for for $\ln(\textrm{\mvar{x}})$. } \begin{codeexample}[] \pgfmathparse{ln(10)} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{ln(exp(5))} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{pow(\mvar{x},\mvar{y})} Raises \mvar{x} to the power \mvar{y}. \begin{codeexample}[] \pgfmathparse{pow(2,7)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{sqrt(\mvar{x})} Calculates $\sqrt{\textrm{\mvar{x}}}$. \begin{codeexample}[] \pgfmathparse{sqrt(10)} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{sqrt(8765.432)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{veclen(\mvar{x},\mvar{y})} Calculates $\sqrt{\left(\textrm{\mvar{x}}^2+\textrm{\mvar{y}}^2\right)}$. \begin{codeexample}[] \pgfmathparse{veclen(12,5)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-constant}{pi} The constant $\pi=3.14159$. \begin{codeexample}[] \pgfmathparse{pi} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{pi r} \pgfmathresult \end{codeexample} \end{math-constant} \begin{math-operator}{\mvar{x}\ r} This converts \mvar{x} from radians to degrees. Note that |r| will evaluate any preceding series of multiplication or division \emph{before} conversion, but not other operations. So |3*4/6r| converts 2 radians to degrees, but |3-4+6r|, converts 6 radians to degrees and adds the result to |-1|. \begin{codeexample}[] \pgfmathparse{2*pi r-pi r} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{2*pi/8 r} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{sin(3*pi/2r)*60} \pgfmathresult \end{codeexample} \end{math-operator} \begin{math-function}{rad(\mvar{x})} Convert \mvar{x} to radians. \mvar{x} is assumed to be in degrees. \begin{codeexample}[] \pgfmathparse{rad(90)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{deg(\mvar{x})} Convert \mvar{x} to degrees. \mvar{x} is assumed to be in radians. \begin{codeexample}[] \pgfmathparse{deg(3*pi/2)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{sin(\mvar{x})} Sine of \mvar{x}. By employing the |r| operator, \mvar{x} can be in radians. \begin{codeexample}[] \pgfmathparse{sin(60)} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{sin(pi/3 r)} \end{codeexample} \end{math-function} \begin{math-function}{cos(\mvar{x})} Cosine of \mvar{x}. By employing the |r| operator, \mvar{x} can be in radians. \begin{codeexample}[] \pgfmathparse{cos(60)} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{cos(pi/3 r)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{tan(\mvar{x})} Tangent of \mvar{x}. By employing the |r| operator, \mvar{x} can be in radians. \begin{codeexample}[] \pgfmathparse{tan(45)} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{tan(2*pi/8 r)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{sec(\mvar{x})} Secant of \mvar{x}. By employing the |r| operator, \mvar{x} can be in radians. \begin{codeexample}[] \pgfmathparse{sec(45)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{cosec(\mvar{x})} Cosecant of \mvar{x}. By employing the |r| operator, \mvar{x} can be in radians. \begin{codeexample}[] \pgfmathparse{cosec(30)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{cot(\mvar{x})} Cotangent of \mvar{x}. By employing the |r| operator, \mvar{x} can be in radians. \begin{codeexample}[] \pgfmathparse{cot(15)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{asin(\mvar{x})} Arcsine of \mvar{x}. The result is in degrees and in the range $\pm90^\circ$. \begin{codeexample}[] \pgfmathparse{asin(0.7071)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{acos(\mvar{x})} Arccosine of \mvar{x} in degrees. The result is in the range $\pm90^\circ$. \begin{codeexample}[] \pgfmathparse{acos(0.5)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{atan(\mvar{x})} Arctangent of $x$ in degrees. \begin{codeexample}[] \pgfmathparse{atan(1)} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{rnd} Generates a pseudo-random number between 0 and 1. \begin{codeexample}[] \pgfmathparse{rnd} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{2*rnd} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{-rnd+5} \pgfmathresult \end{codeexample} \end{math-function} \begin{math-function}{rand} Generates a pseudo-random number between -1 and 1. \begin{codeexample}[] \pgfmathparse{rand} \pgfmathresult \end{codeexample} \begin{codeexample}[] \pgfmathparse{rand*15} \pgfmathresult \end{codeexample} \end{math-function}