# Convenient way of typesetting boxes with multiple input and output wires?

I want to typeset diagrams describing functions which take multiple inputs and return multiple outputs. These should look like boxes with labels in them like "f" or "g" and with some input wires coming in and some output wires going out. The diagrams I'd like to typeset will involve several such boxes with some output wires connected to some input wires. I can draw some examples of what I want if this description is unclear.

I would prefer to be able to do this in xy-pic, for example using xy-graph somehow. Is this a good idea, or should I just learn how to do this in TikZ? Or is there an even better way?

Edit: To be more specific, here are the diagrams I want (although their orientation is negotiable, e.g. if you show me how to do this with the diagrams going to the right instead of down that's great). I should have mentioned that I also want to label the wires.

-
There are a lot of ways of doing this using TikZ (and I would say that TikZ is going to be one of the easiest ways of doing this - though I confess to a slight bias), as the answers you've already gotten show. As there is not a unique way, what you should do now is try them out and then ask new questions when you encounter specific difficulties. – Loop Space Oct 11 '12 at 10:58

(20121012, after the question edit) Here's the right figure drawn trying to stick to TikZ's basics. But, here and there I couldn't resist throwing in one of the non-basic features. (Also, normally I would try to do the same thing in the same way, but here I tried to do the same thing in different ways, so that you can pick which one you like.)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning}
\begin{document}
\begin{tikzpicture}[node distance=1cm,auto]
\tikzset{o/.style={rectangle,draw,minimum size=6mm,rounded corners=1pt,thick}}
\tikzset{a/.style={->,>=stealth',shorten >=1pt,thick,draw}}

\node[o] (f1) {$f$};
\node[o] (g1) [below right=of f1] {$g$};
\node[o] (f2) [below left=of g1] {$f$};
\node[o] (f3) [below=of f2] {$f$};
\node[o] (g2) [below=of f3] {$g$};
\node[o] (g3) [below=of g2] {$g$};

\path[a] (f2.180) ..controls+(-1.5,0)and+(-1.5,0).. coordinate (foo) node[pos=.25]{$T$} node[pos=.75]{$T\times T$} (f1.150);
\path[a] (foo) ..controls+(.5,.5)and+(-1,0).. (f1.210);
\path[a] (f1.0) to[out=0,in=90] node{$S$} (g1.90);
\path[a] (g1.270) to[out=270,in=0] node{$S$} (f2.0);
\coordinate (f23) at ($(f2)!.5!(f3)$);
\path[a] (f2) to node{$T$} (f23);
\path[a] (f23) to[out=225,in=90] (f3.120);
\path[a] (f23) to[out=315,in=90] node[pos=.85] {$T\times T$} (f3.60);
\path[a] (f3) to node{$S$} (g2);
\path[a] (g2) to node{$S$} (g3);
\path[a] (g3) to node{$S$} +(0,-1);
\end{tikzpicture}
\end{document}


(Old answer here.) Here's one that uses only basic TikZ.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{fun/.style={draw,thick,rectangle,minimum size=1cm}}
\node[fun] (f) at (0,0) {$f$};
\node[fun] (g2) at (2,2) {$g^2$};
\node[fun] (h) at (2,-2) {$h$};
\path
(f) edge[->,bend left=80] (g2.west)
(f) edge[<-,bend right] (g2.west)
(g2) edge[<-,very thick,dashed] (h);
\draw[<->] (f) |- (h);
\end{tikzpicture}
\end{document}

-
Thanks for the example! – Qiaochu Yuan Oct 13 '12 at 6:16

When I saw this question I immediately thought of TikZ library circuits.logic.IEC:

\input tikz
\usetikzlibrary{circuits.logic.IEC}
\tikzset{
func/.style={% define a re-usable style named 'func'
and gate,% IEC AND-gate has a '&' normally at the top,
and gate IEC symbol={}% so overwrite that here to empty
}
}
\tikzpicture[circuit logic IEC]
\node[
func,% use the earlier defined style, i.e., AND-gate without '&'
inputs={nnn} % and set it to have three (non-inverted) input anchors, named:
% 'input 1', 'input 2', and 'input 3'
] (func-g) % give this node a name
{$g$}; % and what is going to be typeset inside of it
\foreach \n in {1,...,3} % for each input defined earlier with 'inputs={nnn}'
\draw (func-g.input \n) -- +(-1,0); % draw a line one unit to the left
\draw (func-g.output) -- +(1,0); % and a line one unit to right from output
\endtikzpicture
\bye


Here's another suggestion based on the question edit:

\input tikz
\usetikzlibrary{matrix,decorations.markings,scopes,fit,shapes.geometric}
\tikzset{fun/.style={rectangle, draw, minimum size=2em},
midarr/.style={postaction=decorate, decoration={markings,
mark=at position .7 with {\arrow{stealth}}}}}
\tikzpicture
\matrix[matrix of math nodes, column sep=1em, row sep=3ex,nodes=fun] (mx) {
& f
\\ \coordinate[xshift=-5pt]; && g \\
& f^+ \\
& \coordinate; \\
& f \\
& g \\
};
\path
{[bend left]
(mx-1-2) edge[midarr] node[above] {$_S$} (mx-2-3)
(mx-2-3) edge[midarr] node[below] {$_S$} (mx-3-2)
(mx-3-2) edge[midarr] node[below] {$_T$} (mx-2-1)
(mx-2-1) edge[midarr] node[left] {$_{T\times T}$} (mx-1-2.160)
edge[midarr] (mx-1-2)
(mx-4-2) edge[midarr] node[right] {$_{T\times T}$} (mx-5-2)
}
(mx-3-2) edge[midarr] node[right] {$_T$} (mx-4-2)
(mx-4-2) edge[midarr,bend right] (mx-5-2)
(mx-5-2) edge[midarr] node[right] {$_S$} (mx-6-2)
;
\node[draw,dashed,ellipse,fit=(mx-3-2) (mx-5-2)] {};
\endtikzpicture
\bye


-
Great! Any chance I can 1) put directional arrows on the wires (some of them will be in the opposite direction), 2) allow three wires to meet at a point, and 3) curve the wires, possibly a lot? I should probably have given an example of the kind of diagram I want to draw... – Qiaochu Yuan Oct 10 '12 at 18:12
Thanks for the example! Unfortunately, I'm not familiar with the syntax you used for beginning and ending a TikZ picture (and it doesn't compile in TeXnicCenter). Could you explain that? – Qiaochu Yuan Oct 13 '12 at 5:51
@QiaochuYuan: Ah, yes, sorry about that. It is plain TeX syntax for TikZ; you can change those to \begin{tikzpicture} and \end{tikzpicture} if you use LaTeX. Also the \input tikz should be \usepackage{tikz} with LaTeX, and the \bye at the end is \end{document} with LaTeX. – morbusg Oct 13 '12 at 9:02

TeXample is an endless resource of tikz illustrations, here it is one that looks a lot like the one you want. Original made by Till Tantau.

http://www.texample.net/tikz/examples/nodetutorial/

-

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,positioning}

\begin{document}
\begin{tikzpicture}
\node[draw](f){f};
\node[below right=3em of f,draw] (g) {g};
\node[below =6em of f,draw] (f1) {f1};
\node[below=3em of f1,draw](f2){f};
\node[below=3em of f2,draw](f3){f};
\coordinate[below left=3em of f] (g3);

\draw[-latex] (f.east) to[bend left] node[pos=0.5,right]{s}(g.north) ;
\draw[-latex] (g.south) to[bend left] node[pos=0.5,right]{s}(f1.east) ;
\draw[-latex] (f1.west) to[bend left] node[pos=0.5,left]{T}(g3) to[out=90,in=180]node[pos=0.5,left]{TxT} (f.200);
\draw[-latex] (g3) to[out=90,in=180] (f.160);

\end{tikzpicture}

\end{document}


-

a proposal (if I understand your request) but there will be other

1. function \Mybox creates a node in the inputs and outputs are named
2. function \link is a link to a right angle
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\newcommand{\Mybox}[3][]{
\node[draw,minimum size=8em,text width=6em,#1](#3){#2};
\foreach \nn in{0,1,2,3,...,5}{
\coordinate (#3-s-\nn) at ($(#3.south east)!\nn/5!(#3.north east)$);
\coordinate (#3-e-\nn) at ($(#3.south west)!\nn/5!(#3.north west)$);
}
}

\path (#1) -- (#2) coordinate[pos=0.5](mil);
\draw (#1) -| (mil) |- (#2);
}
\begin{document}
\begin{tikzpicture}
\Mybox[red]{my fonction}{A}
\Mybox[red,below=8em of A]{my fonction 2}{C}
\Mybox[blue, below right=5em and 6em of A]{other fonction}{B}
\draw (A-e-1) -- ++(-1,0) node[left]{x};
\draw (A-e-2) -- ++(-1,0) node[left]{y};