# tikz diagram showing range, domain, and co-domain of a function

While I am gradually becoming more proficient at TeX, I am still very new to TikZ. I am typesetting some notes for Analysis, and would like to be able to produce a map which shows 2 sets (X and Y) and a function (f: X \to Y) which also indicates a few points in the domain and shows where they land; in the codomain and also diagrams the range of the function f. I am not quite sure even how to start. Any help will be appreciated.

• It would be helpful to show an image of what you are looking for. And, even if you are new, you should at least be able to make a start at it. For instance, can you show a small filled circle to represent the points in the domain? Commented May 16, 2012 at 21:57

Here is a starter for you to speed up the learning curve.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,fit}
\begin{document}
\begin{tikzpicture}
%put some nodes on the left
\foreach \x in {1,2,3}{
\node[fill,circle,inner sep=2pt] (d\x) at (0,\x) {};
}
\node[fit=(d1) (d2) (d3),ellipse,draw,minimum width=1cm] {};
%put some nodes on the center
\foreach \x[count=\xi] in {0.5,1.5,...,4}{
\node[fill,circle,inner sep=2pt] (r\xi) at (2,\x) {};
}
\node[fit=(r1) (r2) (r3) (r4),ellipse,draw,minimum width=1.5cm] {};
%put some nodes on the right
\foreach \x[count=\xi] in {0.75,1.5,...,3}{
\node[fill,circle,inner sep=2pt] (c\xi) at (4,\x) {};
}
\node[fit=(c1) (c2) (c3) (c4) ,ellipse,draw,minimum width=1.5cm] {};
\draw[-latex] (d1) -- (r2);
\draw[-latex] (d2) -- (r2);
\draw[-latex] (d3) -- (r4);
\draw[-latex] (r1) -- (c2);
\draw[-latex] (r2) -- (c3);
\draw[-latex] (d3) -- (r4);
\end{tikzpicture}
\end{document}


• Thank you very much. Later this evening, I shall try and make a few minor modifications to what you have here to suit what I would like to end up with. Thanks again. Commented May 16, 2012 at 22:39
• @MichaelDykes Thank you! But please next time include a minimal code so that we can also play around with it. As it is, it's rather a draw-it-for-me question. Commented May 16, 2012 at 23:40
• I am looking for a way to draw function diagrams exactly like this one, with domain and co-domain (range) correspondences, but I get 101 errors when compiling the code above... Message of first error is: "Use of \x doesn't match its definition. \tikz@scan@no@calculator ... \edef \tikz@temp { (#2) }\expandafter \tikz@@scan@... 1.401} Am I doing something wrong? I just took everything between \begin{tikzpicture} and \end{tikzpicture} including, inserted it in file that already compiles and run it -- to no avail. Please help... Thanks, Colin. Commented Sep 16, 2012 at 3:29
• @Diegis It compiles fine for me. Maybe there is an update needed or you missed the necessary TikZ libraries such that some commands are not recognized? Commented Sep 16, 2012 at 7:33

Took me a while to try the stuff out, I'm also pretty new to TikZ. There might be much nicer solutions that don't use absolute coordinates though.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
% draw the sets
\filldraw[fill=blue!20, draw=blue!60] (-1.5,0) circle (1cm);
\filldraw[fill=red!20, draw=red!60] (1.5,0) circle (1cm);
\filldraw[fill=green!20, draw=green!60] (1,0) circle (0.5cm);

% the texts
\node at (1,0) {\tiny$f(x)$};
\node at (0,-2) {$f: X \to Y$};

% the points in the sets (here I just create nodes to use them later on to position
% the circles and the arrows
\node (x1) at (-1,0.7) {};
\node (x2) at (-1.3,-0.7) {};
\node (y1) at (1.5,0.5) {};
\node (y2) at (1.8,-0.5) {};

% position the elements in the sets (at the nodes we just created)
\fill[blue] (x1) circle (1pt);
\fill[blue] (x2) circle (1pt);
\fill[red] (y1) circle (1pt);
\fill[red] (y2) circle (1pt);

% draw the arrows
\draw[->] (x1) -- (y1);
\draw[->] (x2) -- (y2);
\end{tikzpicture}
\end{document}


If you are unsure where to start, the manual is a good place. Most of the stuff I wrote is from the tutorials.

If you are just getting started QTikz is a really nice tool, the editor has a panel that is refreshed every few seconds, makes trial/error much easier than having to compile/switch program all the time.