# Figure of a map with TikZ

This code creates two sets, call them A and B, in the complex plane:

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{tikz}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\draw (1,0) arc (0:360:1);
\fill [pattern=north east lines] (1,0) arc (0:360:1);
\end{tikzpicture}

\begin{tikzpicture}
\draw (1,1) -- (-1,1) -- (-1,-1) -- (1,-1) -- (1,1);
\fill [pattern=north east lines] (1,1) -- (-1,1) -- (-1,-1) -- (1,-1);
\end{tikzpicture}
\end{document}


Now imagine that I have a map f which maps A to B and I want to show this in a figure: So is should look like: Right to A there is an arrow with a f above. One step further on the right we then have B.

How can I realize this with TikZ? Thank you!

• So, you want to draw an arrow from A to B with an f above? – Alenanno Sep 3 '16 at 20:43
• Something like this? – Alenanno Sep 3 '16 at 20:47
• For example, yes! :) It should look nice. – user113359 Sep 3 '16 at 20:47
• Yes with some space between the arrow and the sets. – user113359 Sep 3 '16 at 20:48
• Ed Tufte argues vehemently against hatching. I agree. – JPi Sep 3 '16 at 22:06

Like this:

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{tikz}
\usetikzlibrary{patterns,positioning}

\begin{document}
\begin{tikzpicture}[
set/.style={draw, pattern=north east lines}
]
\node (a) [set,circle, minimum size=10mm] {};
\node (b) [set,minimum size=10mm,right=of a] {};
\draw[->, shorten >=1mm, shorten <=1mm]   (a) -- node[above] {$f$}    (b);
\end{tikzpicture}
\end{document}


It's harder to draw arrows between simple shapes in Tikz because they cannot be referenced to not having a name. Luckily we have nodes that can help in this case.

So if we call the first one (A) and the second (B), we can then draw an arrow using something like \draw[->] (A) -- (B);.

To shorten the arrow (or line), you can use shorten <= and shorten >= (start and end, respectively).

## Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}

\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}

\node[draw, circle, inner sep=0, minimum size=2cm, pattern=north east lines] (A) at (0,0) {};
\node[draw, inner sep=0, minimum size=2cm, pattern=north east lines] (B) at (3,0) {};

\draw[->, shorten <=1mm, shorten >=1mm] (A) -- (B) node[midway, above] {$f$};

\end{tikzpicture}
\end{document}


There are several ways to do this. If you need to use paths rather than nodes for the shapes, then you can name a local bounding box around each path and use those to draw the arrow.

Here's the bounding box around the circle, which we'll call A.

  \begin{scope}[local bounding box=A]
\filldraw [pattern=north east lines] circle (1);
\end{scope}


And here's one around the square, which we'll call B.

  \begin{scope}[xshift=30mm, local bounding box=B]
\filldraw [pattern=north east lines] (1,1)  rectangle (-1,-1);
\end{scope}


Now we can draw the arrow from A to B, placing the label half-way and above the line.

  \draw [->] (A) -- (B) node [midway, above] {$f$};


If you want a little spacing, use ([xshift=2.5pt]A1.east) -- ([xshift=-2.5pt]B1.west) instead, adjusting the spacing to suit.

If nodes are an option, these are easier - just name the nodes and then draw the arrow.

    \path node (a) [draw, circle, minimum size=20mm, pattern=north east lines] {}  ++(30mm,0) node (b) [draw, minimum size=20mm, pattern=north east lines] {};
\draw [->] (a) -- (b) node [midway, above] {$f$};


Again, use (a.east) and a spacing adjustment, if desired.

You could also draw the arrow as part of the same operation you use to draw the nodes, or you could specify the coordinates directly for the arrow or you could use the fit library. Or you could use chains or a graph or .... Lots of ways ;).

Complete code:

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=A]
\filldraw [pattern=north east lines] circle (1);
\end{scope}
\begin{scope}[xshift=30mm, local bounding box=B]
\filldraw [pattern=north east lines] (1,1)  rectangle (-1,-1);
\end{scope}
\draw [->] (A) -- (B) node [midway, above] {$f$};
\begin{scope}[yshift=30mm]
\begin{scope}[local bounding box=A1]
\filldraw [pattern=north east lines] circle (1);
\end{scope}
\begin{scope}[xshift=30mm, local bounding box=B1]
\filldraw [pattern=north east lines] (1,1)  rectangle (-1,-1);
\end{scope}
\draw [->] ([xshift=2.5pt]A1.east) -- ([xshift=-2.5pt]B1.west) node [midway, above]  {$f$};
\end{scope}
\begin{scope}[yshift=-30mm]
\path node (a) [draw, circle, minimum size=20mm, pattern=north east lines] {}  ++(30mm,0) node (b) [draw, minimum size=20mm, pattern=north east lines] {};
\draw [->] (a) -- (b) node [midway, above] {$f$};
\end{scope}
\end{tikzpicture}
\end{document}