52
votes
Accepted
How to do mathematical programming in LaTeX?
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}...
41
votes
pgfplots: Strange Bump in `\tanh` Function
tanh is defined to be sinh/cosh.
And the unexpected spike is a result of a bug in the division routine.
More precisely, the macro \pgfmathdivide@@ defined in
pgfmathfunctions.basic.code line 10 (...
21
votes
How to do mathematical programming in LaTeX?
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/...
20
votes
Accepted
How to Generate Random Negative Integer?
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 ...
18
votes
Accepted
pgfplots: Strange Bump in `\tanh` Function
Symbol 1 shows in his answer the root cause of the problem. Maybe missing is the information, that all this (only) happens, when TeX is used as calculation engine. Since Symbol 1 has no idea how to ...
17
votes
Accepted
Pros and Cons: pgfmath's mathematical expressions vs. l3fp's floating point expressions
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 ...
16
votes
Accepted
Generating tables of random numbers
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
...
16
votes
Accepted
Bamboozling Arithmetic!
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 ...
15
votes
Accepted
Precision in numeric calculations
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, ...
15
votes
Accepted
Why is there a decimal for 3x4=12.0?
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.)
...
13
votes
Accepted
Lua reimplementations of PGF math?
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 ...
13
votes
Accepted
Accumulators in TikZ
The loop body of \foreach is called inside a group. A global assignment helps:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\MyVariable}{0}
\...
13
votes
How to remove zero decimal in numbers?
\documentclass{article}
\ExplSyntaxOn
\newcommand\isintegerTF[1]{
\fp_compare:nNnTF
{#1}={floor(#1)}
}
\ExplSyntaxOff
\begin{document}
\isintegerTF{2}{2 yes}{2 no}
\isintegerTF{1.5}{1.5 yes}...
12
votes
Precision in numeric calculations
Use a better floating point management framework, the one in expl3.
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_set_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
\begin{document}
\...
12
votes
Accepted
Pick Randomly Between -1 or 1
There is a PGF math function to choose randomly from a list of items. This can be used to solve the general problem:
\documentclass{article}
\usepackage{pgffor}
\begin{document}
\...
12
votes
How to do mathematical programming in LaTeX?
MetaPost is also integrated in LuaTeX. As a programming language it allows the implementation of some numerical methods. See this tutorial for an implementation of the Newton iterative method (p. 34).
...
12
votes
How to remove zero decimal in numbers?
A variant of David's answer, if the aim is just to not print trailing zeros:
\documentclass{article}
\usepackage{xfp}
\begin{document}
\fpeval{1}
\fpeval{1.0}
\fpeval{1.5}
\fpeval{14/5-4/5}
\...
11
votes
Pick Randomly Between -1 or 1
Here's a LuaLaTeX-based solution. It outputs the result of \x in math mode; if you don't need that, just remove the two $ symbols used in the definition of \x. (In case you're curious how this works: ...
11
votes
Accepted
Contingency table with pgfplotstable: dealing with large integers
Switching pgf's FPU on solves the dimension too large problem, as expected. The problem is that the FPU has a weird representation for floating point numbers. For example 1Y1.2e3] is the internal ...
11
votes
Accepted
Extract a list of distinct values from another list (remove dupes)
You could remove duplicates using expl3
\ExplSyntaxOn
\cs_new_eq:NN \removeclistdupes \clist_remove_duplicates:N
\ExplSyntaxOff
\Removeclistdupes\List
A full example
\documentclass[a4paper]{article}
\...
11
votes
Accepted
Use \pgfmathresult in siunitx
You can't use a non-expandable command inside the number part of siunitx.* The entire reason \pgfmathparse sets \pgfmathresult is because \pgfmathparse is not expandable. You could use a wrapper ...
11
votes
Accepted
Calculating things with TikZ/pgf versus calculating things with l3fp
Yes, \fp_step_inline:nnnn uses decimal floating point values with 16 digits precision, so unless you need more than 16 significant digits, all values only involving numbers expressable as decimal ...
10
votes
Pick Randomly Between -1 or 1
The apparent choice from -1,0, and 1 is due to a bug and random shouldn't be used with negative arguments as given in \pgfmathparse returns a phantom .0.0
Instead you can use the macro in the ...
10
votes
Accepted
pgfmathparse does not correctly parse zero left-padded integers?
Extract from pgf manual (section 89.1.1 "Commands", p.923, pgfmanual v3.0.1a): "An integer with a zero-prefix (excluding, of course zero itself), is interpreted as an octal number and is automatically ...
10
votes
How to Create Random Letter?
Because we are dealing with TeX, who cares about efficiency
\documentclass[]{article}
\usepackage{pgffor,pgfmath}
\def\getrandomletter{%
\pgfmathrandominteger{\mytemp}{1}{26}%
\foreach\x[count=\...
10
votes
Accepted
How to "add" units to results of \pgfmathsetmacro?
\pgfmathsetmacro calculates a length and gives back the result in pt but without the unit pt attached - the macro contains simply a number. So if you want to use this number in a sensible way you ...
10
votes
Accepted
expl3 cannot see declared functions
EDIT:
The wrapper first described below made its way to CTAN, so the code with a recent LaTeX distribution is
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfmath-xfp}
\...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
pgfmath × 550tikz-pgf × 307
pgfplots × 110
pgfmathparse × 37
pgfplotstable × 29
foreach × 27
macros × 26
random-numbers × 23
pgfkeys × 20
calculations × 16
conditionals × 15
errors × 13
loops × 10
tables × 9
beamer × 8
math-mode × 8
expl3 × 8
arrays × 8
tikzmath × 8
pdftex × 7
plot × 7
floating-point × 7
lists × 6
diagrams × 6
expansion × 6