Tag Info

Hot answers tagged

30

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


28

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 ...


20

From texample.net. The author is Paul Gaborit. Triangle de Pascal


19

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 ...


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 ...


17

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. ...


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 ...


15

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 ...


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

\ifnum compares integers. The results stored by \pgfmathsetmacro always contain a decimal part, even for integer numbers, so you'll have to use \pgfmathtruncatemacro in this case: \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \pgfmathtruncatemacro\one{1} \pgfmathtruncatemacro\two{2} \ifnum\one<\two \draw ...


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 ...


14

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

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 ...


13

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 ...


13

You can use an x filter/.code to transform the x coordinates on a per plot basis. This also works if you use \addplot table instead of \addplot coordinates: \documentclass{article} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis} \addplot+[ x filter/.code={\pgfmathparse{#1*100}\pgfmathresult}, y ...


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

I think all you need to to do is to add an extra {} around the expression as the comma is probably confusing the parser. \foreach \j [evaluate=\j as \jn using {mod(\j,4)}] in {5,6} However, I would recommend a slightly different approach and that is to use pagemathtruncatemacro (or \pgfmathsetmacro if you need real number values) instead: Code: ...


12

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 ...


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

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 ...


12

\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 ...


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

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 ...


12

You could use the gcd() function to reduce the fractions: \documentclass[border=4pt]{standalone} \usepackage{pgfmath,pgffor} \newcommand{\reducedfractions}[1]{% \foreach \x in {1,...,#1} {% \pgfmathtruncatemacro{\numerator}{\x/gcd(\x,#1)}% \pgfmathtruncatemacro{\denominator}{#1/gcd(\x,#1)}% ...


12

Using the pgfplots package, which makes plotting with TikZ easy, something as simple as \documentclass[tikz]{standalone} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis}[domain = -1:1, samples = 500] \addplot[color = red] {asin(x)}; \addplot[color = blue] {acos(x)}; \end{axis} \end{tikzpicture} \end{document} works ...


11

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.


11

Macros in a node name are merely expanded, but not parsed by a math parser. You need to do this yourself, either by using an explicit command like Marco suggested in his answer, or by using the [evaluate = <variable> as <new macro> using <expression>] option of the \foreach statement. Note that you want to use the int(...) function to make ...


11

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 ...


11

You're right, it's a bit of a shame that the expressions aren't evaluated before the loop is started. Three approaches: Use the count=\xi expression to make the outer loop counter accessible. In this case, the outer loop only has to run from 2 to 4, while the counter will run from 1 to 3, which happens to be the starting point of the inner loop. This ...



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