It seems like something's wrong in the implementation of the `\pgfmathmin`

and `\pgfmathmax`

commands.

In `pgfmathfunctions.misc.code.tex`

(in the folder `texlive/2010/texmf-dist/tex/generic/pgf/math`

), you can change the two occurrences of the line

```
\pgfmathparse{getargs(#1,#2)}%
```

to

```
\pgfmathparse{#1,#2}%
```

Then the following code should work

```
\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\pgfmathmin{1,-3,5}{0,2} \pgfmathresult % This doesn't work without the fix
\pgfmathparse{min(1,-3,5,0,2)} \pgfmathresult % This does
\end{document}
```

Here is a strange suggestion based on Gonzalo's idea of using the barycentric coordinate system. Using the `decorations.markings`

library, you can mark a curve periodically. Using those computed reference points, you can do a giant barycentric computation to find the approximate centroid. To wit:

```
\documentclass{article}
\usepackage{tikz,nopageno}
\usetikzlibrary{decorations.markings,scopes}
\newcommand{\globallist}[2]{%
\global\edef#1{#1#2}%
}
\tikzset{bary markings/.style = {
decoration = {
markings,
mark = between positions 0 and 1 step .1 with
{
\edef\number{\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}}
\coordinate (r\number);
\globallist\refpoints{r\number=1,}
}
},
postaction = {decorate}
}
}
\def\refpoints{}
\def\docentroid{
\coordinate (fake) at (5,0);
\globallist\refpoints{fake=0}
\node [circle = 3pt, fill = black] at (barycentric cs:\refpoints) {};
\global\def\refpoints{}
}
\begin{document}
\begin{tikzpicture}
{ [shift = {(0,0)}]
\draw [bary markings] (90:2cm) -- (210:2cm) -- (-30:2cm) -- cycle;
\docentroid
}
{ [shift = {(2.5cm,0)}]
\draw [bary markings] (0,0) .. controls (1,1) and (2,-1) .. (3,0) -- (3,2) -- (0,2) -- cycle;
\docentroid;
}
{ [shift = {(6.5cm,0)}]
\draw [bary markings]
(0,0) parabola bend (2,2) (3,1) .. controls (2.5, -1) and (1,-1/3) .. (1,-1) -- cycle;
\docentroid
}
{ [shift = {(10cm, 3cm)}]
\draw [bary markings] (0,0) -- (1,0) -- (1,-4) -- (4,-4) -- (4,-5) -- (0,-5) -- cycle;
\docentroid
}
\end{tikzpicture}
\end{document}
```

The last example demonstrates (at xport's request) finding the centroid of a very nonconvex region.

An explanation for the unfamiliar. The important computation here is not so much the barycentric coordinates as the marked points, which are constructed with:

```
decoration = {
markings,
mark = between positions 0 and 1 step .1 with
{
\edef\number{\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}}
\coordinate (r\number);
\globallist\refpoints{r\number=1,}
}
},
postaction = {decorate}
}
```

As the manual (v2.10, section 30.5) explains, the `markings`

decoration destroys the original path, but since I am only using it to produce coordinates, I have to apply it as a postaction (thus, it just destroys a copy of the path). I chose to put only 10 (well, 11) markings on the curve to avoid unnecessary slowdown, since decorations proceed at a stately pace. Also, as przemoc noted, the numerical computations can go wrong when very large or very small numbers are involved.

I've also gathered the future argument of the barycentric coordinate in a list `\refpoints`

, which I'm assembling with a quick-and-dirty macro `\globallist`

:

```
\newcommand{\globallist}[2]{%
\global\edef#1{#1#2}%
}
\def\refpoints{}
```

I wanted to use `\pgfkeys`

(namely, the `.append`

handler) to assemble the list, but it turns out that the markings are executed in a TeX group and so the list needs to be set globally, and I couldn't figure out how to coerce `\pgfkeys`

into doing that.

Once the path is prepared, the centroid can be computed:

```
\def\docentroid{
\coordinate (fake) at (5,0);
\globallist\refpoints{fake=0}
\node [circle = 3pt, fill = black] at (barycentric cs:\refpoints) {};
\global\def\refpoints{}
}
```

The point of the coordinate `(fake)`

is to eat up the comma at the end of the list `\refpoints`

. Alas, although `barycentric cs:`

takes a comma-separated list of `node=weight`

assignments, that list does not have the conveniences of a list of PGF keys, and neither spaces nor trailing commas are allowed. One hopes that this will be fixed in a future version, but in any case, giving `(fake)`

a weight of zero makes TikZ ignore it anyway.

## Best Answer

Use the TikZ

`external`

library for this. In your preamble place:You need to make sure that LaTeX is allowed to use external commands with

`-enable-write18`

or`-shell-escape`

. This will turn each TikZ figure into its own PDF image, and then TikZ knows to grab that instead (i.e. you won't need to change any of your code). For more information, see section 32 of the PGF/TikZ manual.The benefit of this approach is that LaTeX typesets each figure individually, so that the memory overhead is broken down into smaller chunks, and you will be less likely to run out. You will also notice that each time you typeset it'll save some time after the figures have been generated.