TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a big bunch of polygons plot in 3D with TikZ. The code is generated by a program, so it's hard for me to print the "foreground" polygons last, so that they are well printed in space.

In other words, if the last line of my TikZ code is a polygon "behind" the others with respect to the projection I use in TikZ, I want this last polygon to be "hidden" by the foreground ones, even though the \fill command was used before.

Is there an option to do this?

share|improve this question
What you're asking for is a "z level" in TikZ. Take a look at the solutions to tex.stackexchange.com/q/20425/86. If that isn't what you want, it would help if you could edit your question to explain why not (and also include some sample code generated by your program). – Loop Space Feb 21 '12 at 10:57
Try including \usetikzlibrary{backgrounds} in your preamble and put the polygon drawing commands into \begin{scope}[on background layer] ...... \end{scope}. See the PGF/TikZ manual for more layering capabilities. – percusse Feb 21 '12 at 11:15
What I was looking for was Sketch. Thanks! The z-layer was not enough, for example to draw that kind of picture better: irisa.fr/symbiose/people/asiegel/Dessins/markov_tribo.gif – Omit Feb 21 '12 at 13:15
I'd like to mark this question "answered", but I can't. You should use the reply box when you provide a real answer! – Omit Feb 21 '12 at 13:28

I don't think thats possible with TikZ alone, but you could have a look at Sketch, and also an introduction to it for TikZ users.

An example, also presented on TeXample.net:

enter image description here

If this example should be too hard for introductory purposes, the introduction for TikZ users will take you through it step by step. I can also recommend Sketch's manual, which is quite extensive and well readable.

share|improve this answer

Pgfplots 1.5.1 comes with a patch type=polygon: the idea is to provide vertices and a connectivity matrix, some 3d view angle and pgfplots does the rest - including z buffer sorting.

Pgfplots defines the "depth" of one polygon to be the mean of the depth of its vertices and sorts polygons according to the polygon depth ("painters algorithm").

Here is an example taken from the pgfplots 1.5.1 manual, section "5.6 Patch plots library", page 311:

enter image description here





    table/row sep=\\,
    patch type=polygon,
    vertex count=5,
    patch table with point meta={%
    % pt1 pt2 pt3 pt4 pt5 cdata
        0 1 7 2 2 0\\
        1 6 5 5 5 1\\
        1 5 4 2 7 2\\
        2 4 3 3 3 3\\
table {
    x y z\\
    0 2 0\\% 0
    2 2 0\\% 1
    0 1 3\\% 2
    0 0 3\\% 3
    1 0 3\\% 4
    2 0 2\\% 5
    2 0 0\\% 6
    1 1 2\\% 7
% replicate the vertex list to show \coordindex:
\addplot3[only marks,nodes near coords=\coordindex]
table[row sep=\\] {
0 2 0\\ 2 2 0\\ 0 1 3\\ 0 0 3\\
1 0 3\\ 2 0 2\\ 2 0 0\\ 1 1 2\\

the first \addplot command (up to the semicolon ;) provides two tables: the argument for patch table with point meta contains connectivity information, i.e. zero-based integer indices into the other table. Each row in that connectivity table makes up one polygon. The number of vertices per polygon is fixed by vertex count=5, although it is permitted that a polygon has the same vertex multiple times. The last column of the connectivity table here is color data; it is mapped linearly into the current colormap to control which color is used to fill the patch. The outer table is the table of vertices (8 here).

The second \addplot3 command is just to add a nodes near coords plot on top of the rest (i.e. to show labels for every vertex).

The patch plots library features z buffering by means of z buffer=sort, color mapping, and 3d axis support.

Note that annotations on top of the plot can be added by means of TikZ drawing instructions.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.