TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 seen the previous tikz example, similar with this one, but that example does not compile for me, I get 101 errors! How would one code the diagram below in tikz? Thanks, Diegis.

enter image description here

share|improve this question
up vote 10 down vote accepted

One possibility:




  every node/.style={on grid},
  setA/.style={fill=blue,circle,inner sep=2pt},
  setC/.style={fill=red,rectangle,inner sep=2pt},
  every fit/.style={draw,fill=blue!15,ellipse,text width=25pt},

% set A
\node[setA,label=left:$a$] (a) {};
\node [setA,below = of a,label=left:$b$] (b) {};
\node [setA,below = of b,label=left:$c$] (c) {};
\node[above=of a,anchor=south] {$A$};

% set B
\node[inner sep=0pt,right=3cm of a] (x) {$x$};
\node[below = of x] (y) {$y$};
\node[inner sep=0pt,below = of y] (z) {$z$};
\node[above=of x,anchor=south] {$B$};

% set C
\node[setC,label=right:$m$,right = 3cm of x] (m) {};
\node[setC,label=right:$n$,below = of m] (n) {};
\node[setC,label=right:$p$,below = of n] (p) {};
\node[above=of m,anchor=south] {$C$};

% the arrows
\draw[->,shorten >= 3pt] (a) -- node[label=above:$f$] {} (x);
\draw[->,shorten >= 3pt] (b) -- node[label=above:$f$] {} (x);
\draw[->] (c) -- node[label=above:$g$] {} (y);
\draw[->,shorten <= 3pt] (x) -- node[label=above:$h$] {} (m);
\draw[->] (n) -- node[label=above:$u$] {} (y);

% the boxes around the sets
\node[fit= (a)  (c) ] {};
\node[fit= (x) (z) ] {};
\node[fit= (m) (p)] {};


enter image description here

share|improve this answer
Thank you very much, it works beautifully!! Is there a good start, a good tutorial to learn tikz? Nothing beats experience, obviously, but nothing beats a little bit of home reading either! I know nothing on the subject, while I know my way around to write pretty complex math, I am at a complete loss when it comes to drawings, and I am tired of making all visuals in Visio and then inserting them as pictures -- nothing wrong with that, but you don't get a lot of the math symbols in Visio, no Greek letters, no subscript/superscript, a lot of inconveniences... Thank you again, Diegis. – Diegis Sep 16 '12 at 5:12
@Diegis there's the comprehensive pgfmanual (with lots of examples), the Minimal introduction to TikZ, and a good example gallery at TeXample.net. – Gonzalo Medina Sep 16 '12 at 5:24

To kill the time, I drew this with MetaPost, with help of the boxes package. The result is certainly perfectible.

To be executed with LuaLaTeX.

input boxes;

u := 3cm; v := 1.5cm; % Horizontal and vertical spaces between consecutive nodes 
path ellipse[]; 
ellipse1 = fullcircle xscaled .6u yscaled 3.2v;
for i = 2, 3: ellipse[i] = ellipse[i-1] shifted (u, 0); endfor

def midlabelarrow(expr A, B, labl) = drawarrow A--B; label.top(labl, .5[A,B]); enddef;

    for i = 1, 2, 3:
        fill ellipse[i] withcolor .95[blue, white]; draw ellipse[i];

    circmargin := 2bp; forsuffixes i = A, B, C: circleit.i(); endfor
    boxit.X("$x$"); boxit.Y("$y$"); boxit.Z("$z$");
    defaultdx := 2bp; defaultdy := 2bp; forsuffixes i = M, N, P: boxit.i(); endfor

    B.c = origin = A.c - (0, v) = C.c + (0, v); 
    Y.c = (u, 0) = X.c - (0, v) = Z.c + (0, v);
    N.c = (2u, 0) = M.c - (0, v) = P.c + (0, v);

    drawunboxed(X, Y, Z);
    forsuffixes i = A, B, C: fill bpath i withcolor blue; endfor
    forsuffixes i = M, N, P: fill bpath i withcolor red; endfor

    midlabelarrow(A.e, X.w, "$f$"); midlabelarrow(B.e, X.sw, "$f$"); 
    midlabelarrow(C.e, Y.w, "$g$"); midlabelarrow(X.e, M.w, "$h$"); 
    midlabelarrow(N.w, Y.e, "$u$");

    label.lft("$b$", B.w); label.lft("$a$", A.w); label.lft("$c$", C.w);
    label.rt("$n$", N.e); label.rt("$m$", M.e); label.rt("$p$", P.e);

    labeloffset := 6bp;
    label.top("{\large $A$}", point 2 of ellipse1);
    label.top("{\large $B$}", point 2 of ellipse2);
    label.top("{\large $C$}", point 2 of ellipse3);

enter image description here

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.