17

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

2 Answers 2

15

One possibility:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,shapes,fit,calc}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{document}

\begin{tikzpicture}[
  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},
  >=latex
]

% 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
\begin{pgfonlayer}{background}
\node[fit= (a)  (c) ] {};
\node[fit= (x) (z) ] {};
\node[fit= (m) (p)] {};
\end{pgfonlayer}
\end{tikzpicture}

\end{document}

enter image description here

3
  • 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, 2012 at 5:12
  • 3
    @Diegis there's the comprehensive pgfmanual (with lots of examples), the Minimal introduction to TikZ, and a good example gallery at TeXample.net. Sep 16, 2012 at 5:24
  • How would you import it into Emacs org mode?
    – phoxd
    May 11, 2019 at 7:00
4

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.

\documentclass[border=3bp]{standalone}
\usepackage{luamplib}
    \mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
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;

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

    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);
endfig;
\end{mplibcode}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .