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}...
student's user avatar
  • 29k
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 (...
Symbol 1's user avatar
  • 36.9k
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/...
Torbjørn T.'s user avatar
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 ...
Paul Gaborit's user avatar
  • 70.9k
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 ...
Stefan Pinnow's user avatar
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 ...
Joseph Wright's user avatar
  • 260k
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 ...
gernot's user avatar
  • 49.6k
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 ...
egreg's user avatar
  • 1.1m
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, ...
Heiko Oberdiek's user avatar
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.) ...
cfr's user avatar
  • 199k
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 ...
Christian Feuersänger's user avatar
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} \...
Heiko Oberdiek's user avatar
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}...
David Carlisle's user avatar
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} \...
egreg's user avatar
  • 1.1m
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} \...
Alan Munn's user avatar
  • 218k
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). ...
Franck Pastor's user avatar
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} \...
egreg's user avatar
  • 1.1m
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: ...
Mico's user avatar
  • 508k
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 ...
Phelype Oleinik's user avatar
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} \...
Joseph Wright's user avatar
  • 260k
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 ...
Joseph Wright's user avatar
  • 260k
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 ...
Marcel Krüger's user avatar
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 ...
David Carlisle's user avatar
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 ...
Paul Gaborit's user avatar
  • 70.9k
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=\...
percusse's user avatar
  • 158k
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 ...
Ulrike Fischer's user avatar
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} \...
Skillmon's user avatar
  • 60.7k

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