tikz - Idiomatic padding

For this TikZ figure, the shorten statements keep the tail of the top-most arrow from colliding with the input node and similarly the head of the bottom-most node from colliding with the output node.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}

\begin{tikzpicture}[>=stealth]
\node [draw] (compiler) {Compiler};
\node [coordinate, above of=compiler] (input) {};
\node [coordinate, below of=compiler] (output) {};
\draw [->, shorten <=0.5em] (input) node {source program} -- (compiler);
\draw [->, shorten >=0.5em] (compiler) -- (output) node {target program};
\end{tikzpicture}
\end{document}


The output of the above code is,

Is there a more appropriate which does not involve setting spacing by hand but rather an approach that resolves this issue automatically?

I've been unsuccessful in finding any examples similar to this figure. Most diagrams I have found have arrows running in the horizontal direction and the text hovering above the arrows.

• Welcome to the site! Thanks for providing such a clear mwe, it really helps :) perhaps inner and/or outer Sep might be helpful? – cmhughes Nov 7 '15 at 17:52

1 Answer

I don't see why you add two nodes at the same place. Add the text in the input and output nodes when they are defined, and remove the coordinate option. Then the arrows are drawn from the edge of node shape encompassing the text.

Note also that the of= syntax is deprecated in favor of loading the positioning library and saying =of, see Difference between "right of=" and "right=of" in PGF/TikZ

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,positioning}
\begin{document}

\begin{tikzpicture}[>=stealth]
\node [draw] (compiler) {Compiler};
\node [above=of compiler] (input) {source program};
\node [below=of compiler] (output) {target program};
\draw [->] (input) -- (compiler);
\draw [->] (compiler) -- (output);
\end{tikzpicture}
\end{document}