# Create a diagram to display composition of functions

So as to produce exercises about compositions of function, I would like to produce something like this (ugly) picture.

From LaTeX point of view, the syntax could be like this :

\compoDiagram[$G \circ f$]{$x$}{$F$}{$y=F(x)}{G}{$z=G(y)=G \circ F(x)$}  The optional argument would be to display or not the "composition" arrow. - ## 2 Answers Here is a possibility: \documentclass{article} \usepackage{xparse,tikz} \usetikzlibrary{positioning,calc} \NewDocumentCommand{\compoDiagram}{o m m m m m}{% \begin{tikzpicture}[baseline=-0.5ex, on grid, node distance=1.5cm] \node (diagraminit) {#2}; \node[draw,right of=diagraminit] (func1) {#3}; \node[right= 2cm of func1] (diagrammid) {#4}; \node[draw,right= 2cm of diagrammid] (func2) {#5}; \node[right= 3cm of func2] (diagramend) {#6}; \draw[-] (diagraminit)--(func1); \draw[-latex] (func1)--(diagrammid); \draw[-] (diagrammid)--(func2); \draw[-latex] (func2)--(diagramend); \IfNoValueTF{#1}{%true } { \node[draw] (diagramcomposition) at ($(diagraminit)!0.5!(diagramend)-(0,1)$){#1}; \draw[-latex](diagraminit)|-(diagramcomposition)-|(diagramend); } \end{tikzpicture} } \begin{document} \compoDiagram[$G \circ F$]{$x$}{$F$}{$y=F(x)$}{$G$}{$z=G(y)=G \circ F(x)$} \vspace{2cm} \compoDiagram{$x$}{$F$}{$y=F(x)$}{$G$}{$z=G(y)=G \circ F(x)$} \end{document}  Result: Beware: the node distances are optimized for this kind of inputs (in terms of node width). Since the basic use is in math mode, perhaps is it is boring typing $ inside the \compoDiagram each time; it is for sure better:

\compoDiagram[G \circ F]{x}{F}{y=F(x)}{G}{z=G(y)=G \circ F(x)}


Thus, a possible modification of the command is:

\NewDocumentCommand{\compoDiagram}{o m m m m m}{%
\begin{tikzpicture}[baseline=-0.5ex, on grid, node distance=1.5cm]
\node (diagraminit) {\ensuremath{#2}};
\node[draw,right of=diagraminit] (func1) {\ensuremath{#3}};
\node[right= 2cm of func1] (diagrammid) {\ensuremath{#4}};
\node[draw,right= 2cm of diagrammid] (func2) {\ensuremath{#5}};
\node[right= 3cm of func2] (diagramend) {\ensuremath{#6}};
\draw[-] (diagraminit)--(func1);
\draw[-latex] (func1)--(diagrammid);
\draw[-] (diagrammid)--(func2);
\draw[-latex] (func2)--(diagramend);
\IfNoValueTF{#1}{%true
}
{
\node[draw] (diagramcomposition) at ($(diagraminit)!0.5!(diagramend)-(0,1)$){\ensuremath{#1}};
\draw[-latex](diagraminit)|-(diagramcomposition)-|(diagramend);
}
\end{tikzpicture}
}

-
Thanks. I will play with this. Indeed the situation will look alqays the same for the names of the functions. For the formulas, I will test your solution. If I need some "customizations", I will post another post later. –  projetmbc Oct 6 '12 at 11:33

I would use TikZ to produce diagrams like the above. You could do something like

\documentclass{memoir}

\usepackage{amsmath,amssymb}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[node distance=.3\textwidth]
\node (x)  {$x$};
\node (y) [right of=x] {$y=f(x)$};
\node (z) [below of=y] {$z=g(y)=g\circ f(x)$};
\draw[->,thick] (x) -- node[above] {$f$} (y);
\draw[->,thick] (y) -- node[right] {$g$} (z);
\draw[->,thick] (x) -- node[anchor=north east] {$f\circ g$} (z);
\end{tikzpicture}

\end{document}


I don't know if I see the point in making a command to make such diagrams. How many diagrams do you need? And should they all have the same structure?

-
I will use it for exercices, corrections and also some part of my course. This is for 16 years old students. So one command is very usefull. –  projetmbc Oct 6 '12 at 11:31