# Tag Info

## Hot answers tagged pgfmath

26

I ran your code but it appeared to be very slow, I suspect from all the \pgfmathtruncatemacro. But here we can do all calculations with \numexpr easily. This code is based on the TeX primitives \ifnum, \ifcase and \csname..\endcsname. I have used \foreach loops in the first two code samples as I wanted to stay close to your original framework. In the third ...

24

Short answer The problem illustrated by your example is due to round-off error. See section 56 in the TikZ/PGF documentation (v2.10, p.505): [...] for fractional steps that are not multiples of 2n for some small n, rounding errors can occur pretty easily. Thus, in \foreach \x in {0,0.1,...,0.5} {\x, }, 0.5 should probably be replaced by 0.501 for ...

19

Just for fun (but perhaps it can be useful to anyone), there is my Lua solution: Main TeX file \documentclass{article} \usepackage{pgffor} \usepackage{xcolor} \usepackage{courier} % Courier has bold series, while cm doesnt \usepackage[active,tightpage]{preview}\PreviewEnvironment{tabular} % Load lua program, and define macros for accessing its functions ...

16

After getting these two answers I'd like to publish my solution also. After seeing jfbu's answer I was a bit intimidated and I went the luatex way. The code is probably not efficient, but it can produce an animated PDF – unfortunately this feature only works in Adobe Reader – or pages with the different evolution phases. Also this code only works with n×n ...

16

Here is a solution using TeX integer arithmetic. I am reusing counters defined by PGF in order to avoid having to declare new ones. \documentclass{article} \usepackage{tikz} \makeatletter \newcommand\binomialCoefficient[2]{% % Store values \c@pgf@counta=#1% n \c@pgf@countb=#2% k % % Take advantage of symmetry if k > n - k ...

15

This happens because you're calling min with an argument it's not made for: The first element of your list is empty, which sets \pgfmathresult to the largest allowable number, due to the way the algorithm is implemented. To get the correct behaviour, you should make sure the first component is not empty (the last one may be, though). Also, \foreach ...

14

When using macros in node names, the macros have to be expandable in an \edef context. \pgfmathparse is not. So you need to do the computation beforehand and only use the result of it in the node name. One way is to use the evaluate key on the \foreach as in the following. \documentclass{article} %\url{http://tex.stackexchange.com/q/141259/86} ...

14

Well, here is a solution that uses pgf only for the random integers. Everything else is done with TeX's own conditionals and, in one instance, a loop. Code \documentclass{article} \usepackage{pgf} \pgfmathsetseed{\pdfuniformdeviate 10000000} \newcommand*\MakeFirstTerm[2]{ \loop\pgfmathrandominteger{\a}{#1}{#2} \ifnum\a<0\relax ...

13

Lots of if :) \documentclass{article} \usepackage{tikz} \pgfmathsetseed{\pdfuniformdeviate 10000000} \newcommand{\rndcoeff}[1][1]{ \pgfmathrandominteger{\a}{\ifnum#1>1 1\else0\fi}{6} \ifnum#1>1 \pgfmathparse{rand>0?:"-"}\pgfmathresult\ifnum\a=1\else\a\fi x^2 \else \ifnum#1<1\relax \ifnum\a>0\relax ...

13

Use \pgfmathsetmacro\mymacro{...} instead of \pgfmathparse{...}. From the v2.10 pgfmanual, section 62.1 Commands for Parsing Expressions, page 527: \pgfmathsetmacro{<macro>}{<expression>} Defines <macro> as the value of <expression>. The result is a decimal without units. ...

13

Both commands behave exactly the same. The rounding error is the reason that the terminal value is "missed" in the first case. This check \documentclass{report} \usepackage{tikz} \begin{document} \foreach \x in {1,1.1,...,2} {\number\x\ } \foreach \x in {1,1.2,...,2} {\number\x\ } \end{document} results in 1 1.1 1.20001 ...

13

Based on this answer. \documentclass{standalone} \usepackage{tikz} \makeatletter \pgfmathdeclarefunction{erf}{1}{% \begingroup \pgfmathparse{#1 > 0 ? 1 : -1}% \edef\sign{\pgfmathresult}% \pgfmathparse{abs(#1)}% \edef\x{\pgfmathresult}% \pgfmathparse{1/(1+0.3275911*\x)}% \edef\t{\pgfmathresult}% \pgfmathparse{% 1 - ...

12

You can use the optional arguments of \pgfmathprintnumber to either cut off the decimal part or to round the number to an integer. Alternatively, you can use the \num macro from the siunitx package to round the number: \documentclass{article} \usepackage{tikz} \usepackage{siunitx} \begin{document} \pgfmathsetmacro\testnumber{25.7} ...

12

\pgfmath is not expandable, and so has to be used with a 'known' output macro to provide the result (for more on expandable code, see for example Tricks to make macros expandable and Why isn't everything expandable?). The LaTeX3 FPU is expandable, and so you can do \documentclass{article} \usepackage{expl3} \ExplSyntaxOn ...

12

In the cvs version of pgf/tikz or in the version available for texlive at tlcontrib there is an experimental undocumented dim function in pgfmath defined as \makeatletter % dim function: return dimension of an array % dim({1,2,3}) return 3 % dim({{1,2,3},{4,5,6}}) return 2 \pgfmathdeclarefunction{dim}{1}{% \begingroup \pgfmath@count=0\relax ...

11

If the function is used inside a coordinate pair, then you get two closing parentheses: (0.2, f(0.2)) This apparently confuses the parser. These parentheses are not matched like curly braces in \TeX, when an argument is read. A set of curly braces protects the inner parentheses: (0.2, {f(0.2)}) Then the inner delimiters are hidden for the parser that ...

11

You can set the random seed each time you draw the ellipse, like so: \documentclass[presentation]{beamer} \usepackage{tikz, pgf} \usetikzlibrary{decorations.pathmorphing} \begin{document} \begin{frame} \begin{center} \begin{tikzpicture} \uncover<1-3>{ \pgfmathsetseed{1234} % Choose a four-digit number here \fill [decorate, decoration={random ...

11

The problem is the inaccurate division step after the rounding operation, which is necessary because you can't specify the number of decimal digits for the rounding operation. A solution would be to use \pgfmathprintnumber[precision=1]{\pgfmathresult} to round and output the number. If you want to use the rounded number elsewhere, you can use ...

11

\pgfmathparse always saves its result with a decimal part. You can either use \pgfmathprintnumber{\pgfmathresult} in the node text to output the number, which will remove the .0 for integers, or do the calculation using \pgfmathtruncatemacro\mymacro{round(\i*100/3)}, which will save the result of the calculation without the decimal part, and then use ...

11

When pgfmath parser was written, the main aim was to provide consistent and slightly more versatile mathematical operations than the calc package (which used to do all the calculations) without the overhead of the fp package. Also the integration with \foreach variables was important as well (as been suggested above). So, every expression given to the ...

10

In general it might be hard but here you are only using pgfmath for an inequality test which is a sledgehammer you can avoid: \documentclass{article} \usepackage{pgfplotstable} \begin{document} \pgfplotstableset{symbol column/.style={column type=c, postproc cell content/.style={ /pgfplots/table/@cell content={}{% \ifdim##1pt <0.01pt ...

10

If you use pdflatex or LuaLaTeX, then \documentclass{article} \usepackage{pgf} \pgfmathsetseed{\number\pdfrandomseed} \begin{document} \pgfmathsetmacro{\test}{rand}\test \end{document} will pick a random seed each time. With XeLaTeX there's nothing similar. The value of \pdfrandomseed doesn't change during a run, though.

10

Using the same approximation idea as cjorssen (I tried the Taylor series as Qrrbrbirlbel suggested but it's pretty hopeless to get a decent approximation this way) I rewrote the function without using low-level PGF. Because we have so many 2D plots here already, I'll just use my 3D plot that I had already. \documentclass{standalone} \usepackage{pgfplots} ...

10

No, that doesn’t work because TikZ only accepts fully expandable input. By the way, the coordinate input is thrown into PGF math anyway, so \draw[thick] (0.255in*3/2,2) … works just as well (case 1). You can also just store 0.255in*3/2 in \leftBoundary and use that macro (case 2; again, using that anything will be parsed through the mathematical ...

10

This is a problem with the fpu library that is used by pgfplots: The ifthenelse command is not implemented in fpu, so it falls back to the normal pgfmath routine, which then stumbles over the floating point format of the arguments, because numbers are handled in the form 1Y1.0e0]. To circumvent this, the fpu library can be disabled in the newly defined math ...

10

It is also relatively easy to solve using just TikZ: \documentclass{article} \usepackage{tikz} \newcounter{arraycard} \def\arrayLength#1{% \setcounter{arraycard}{0}% \foreach \x in #1{% \stepcounter{arraycard}% }% \the\value{arraycard}% } \begin{document} \noindent The length of $\{1,2,3\}$ is \arrayLength{{1,2,3}}.\\ And the length of ...

10

Here's a pair of lualatex solutions. Both provide \genrand which takes an optional integer argument that specifies the maximum (absolute) value of the coefficients, the default is 10. The first uses pattern matching and the second is a somewhat odd variation of the standard approach. Pattern Matching: \documentclass{article} \usepackage{luacode} ...

9

You can do complete expansion beforehand: \documentclass{article} \usepackage{pgf}% Easy way to get pgfmath \def\epgfmathsetmacro#1#2{\begingroup \edef\x{\endgroup\noexpand\pgfmathsetmacro\noexpand#1{#2}}\x} \def\MaxValueOfTok{8}% \newtoks\SomeTokDefinedDirectly \newtoks\SomeTokDefinedViaDef \SomeTokDefinedDirectly={7} ...

9

One way to do this is to use \addplot coordinates. Or, as Jake suggested one could let the second coordinate be the variable (3,x) Code: \documentclass{article} \usepackage[paperwidth=21.0cm]{geometry}% for image capture \usepackage{pgfplots} \begin{document}\noindent \begin{tikzpicture} \begin{axis}[xlabel=$x$, ylabel=$y$] ...

9

There seems to be an error in pgfmathparser.code.tex. Line 712 of the latest version (1.47) reads \pgfmathdeclareoperator{!=}{notequalto}{2}{infix} {250} but it should be \pgfmathdeclareoperator{!=}{notequal}{2}{infix} {250} since the function is called notequal (not notequalto). Changing this line fixes the problem. I'll file a bug report.

Only top voted, non community-wiki answers of a minimum length are eligible