# Tag Info

52

You can integrate python code into your LaTeX document using pythontex. Here is a simple example: \documentclass{article} \usepackage[gobble=auto]{pythontex} \usepackage{pgfplots} \begin{document} \begin{pycode} from sympy import * x = symbols('x') f = integrate(cos(x)*sin(x), x) \end{pycode} \begin{pysub} \begin{tikzpicture} \begin{axis}[...

34

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

24

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 filter/.code={\pgfmathparse{#1*...

23

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

22

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

21

For LuaLaTeX, and using Lua, but other than that: "Numerical methods with LuaLaTeX", by Juan Montijano, Mario Pérez, Luis Rández and Juan Luis Varona. TUGboat issue 35.1: https://www.tug.org/TUGboat/tb35-1/tb109montijano.pdf pweave was mentioned in the answer by jonaslb, so it would make sense to also mention sweave (which was the inspiration for pweave) ...

20

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

20

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

19

This question still doesn't have an answer, although it was answered in comments and in the question itself. I will post the answer here, because duplicate question was posted today and it can't be closed as duplicate as long as this question doesn't have accepted or upvoted answer. This issue was reported on PGF issue tracker, even with proposed patch, but ...

19

To work around the bug of the random function, you may defined a new randomfixed function: \tikzset{declare function={randomfixed(\a,\b) = int(random(0,int(\b-\a))+\a);}} Now, you may generate a random integer between two boundaries with parentheses for negative integer: \newcommand\randomint[2]{\bgroup% \pgfmathsetmacro\myval{randomfixed(#1,#2)}% \...

18

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

17

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

17

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.

17

TeX doesn't parse math as you want it to do. Here, \foreach might be flexible enough to understand it but in general it is a bad idea. You can force it via PGF math such as \pgfmathsetmacro\x{2} \pgfmathsetmacro\y{int(\x + 2)} %without int \y would be 4.0 instead of 4 \foreach \s in {\x,...,\y} { % Do things }

17

Updated Answer In response to tdgunes' comments about this answer years later, I contacted the package author about this question, and I'm happy to say that he has added this feature to the package as of v5.0, released to CTAN on 11 January 2018! The code in this section shows how to use the feature as implemented in the package. I've also left the ...

17

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

17

The design aims of the two systems have some differences which may be pros or cons, depending on your requirements. The pgfmath engine is non-expandable and requires only classical TeX primitives. On the other hand, l3fp works by expansion and requires the extended primitive set mandated by LaTeX3: e-TeX and \pdfstrcmp or an equivalent (see also below). Key ...

16

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

16

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

16

Your assumption that multiplying by 1cm is the same as multiplying by 1 is wrong. What about multiplying by 1in or 1mm? The function \pgfmathresult returns a number, not a dimension. For implementation reasons, the number 1 is stored as a length, precisely 1pt. Therefore 3*1 is 3, but 3*1cm is 85.35823, because 1cm=28.45275pt (and then TeX’s rounding ...

15

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

15

Regular text formatting doesn't apply to math mode and \pgfmathprintnumber inherently uses \ensuremath behind the scenes around the number. You can turn it off via \pgfmathprintnumber[assume math mode=true]{123.567} Then it will assume that it is already in the math mode and won't modify the number printing. That means the number will be interpreted as ...

15

First, you are not formatting the number when you say \pgfmathresult. Second, you have not specified any particular format for number printing. (Although the default format will do, in this case.) Compare: \documentclass{article} \usepackage{pgf} \begin{document} \pgfmathparse{3*4} \pgfmathresult \pgfmathprintnumber\pgfmathresult \pgfmathparse{int(3*4)} ...

15

With the following definitions, you can define the parameters of your random numbers by \setrand{<minimum>}{<maximum>}{<multiple of>}{<seed>} A random number is generated by \nextrand which then can be accessed arbitrarily often using \thisrand Here is the code to be included in the preamble: \usepackage{pgfmath} \newcommand\...

14

I'm not sure that the algorithm in the question is correct, nevertheless it is certainly implemented in a sub-optimal manner. Although Jake's (now deleted) answer is readable it also has a huge overhead in calling the parser inside a function. It is quite simple to use the lower level pgfmath macros (although I probably would be expected to say that). ...

14

The number range in the default math engine of pgf is quite restricted because of the limitations of TeX's numbers. Also the library for fixed point arithmetic does not help with these exponents, because this library only covers ten digits before and after the decimal point. The example can be processed with the floating point unit library: \documentclass{...

13

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)}% \$\frac{\numerator}{\denominator}\...

13

The luamath library is actively used in pgfplots: if you write \pgfplotsset{compat=1.12} (or newer) and translate the document with lualatex, pgfplots will shift lots of its arithmetics to lua (higher precision, much faster). The reference documentation for this part is in the pgfplots manual keyword lua backend. The standalone version of the library is ...

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