# Setting default edges to be square (orthogonal) in a tree drawn with TikZ

I'm searching for a way to have orthogonal edges (only going horizontal and vertical) by default when I draw a tree. I can't seem to find a way to set this as the default style (without having to add edges with path each time).

\documentclass{article}
\usepackage{tikz}
%%% <
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{10pt}%
%%% >

\begin{document}
\begin{tikzpicture}
\tikzstyle{every node}=[draw, rectangle, fill=gray!20]

\node {root}
child {node {left}}
child {node {right}
child {node {child}}
child {node {child}}
};
\end{tikzpicture}
\end{document}


I have included an example of the edges I'm searching for. There is not need for labels on the edges.

• Are you looking for the key edge from parent fork down? – Claudio Fiandrino Mar 6 '13 at 14:17
• The provided example is not a tree... You can obtain such lines using (node A) -| (node B) for drawing from node A to node B first horizontal, then vertical, or |- for the opposite? – Lionel MANSUY Mar 6 '13 at 14:20

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
every node/.style={draw, rectangle, fill=gray!20},
edge from parent path={
(\tikzparentnode) |-   % Start from parent
($(\tikzparentnode)!0.5!(\tikzchildnode)$) -| % make an ortho line to mid point
(\tikzchildnode)}]                            % make another ortho to the target

\node {root}
child {node {left}}
child {node {right}
child {node {child}}
child {node {child}}
};
\end{tikzpicture}
\end{document}


• I get artifacts using this approach (with more "interesting" node labels): the left & right branches to the children have a discontinuity along the horizontal line, I think because \tikzchildnode do not align exactly. ----- Workaround: (\tikzparentnode) -- +(0.0,-0.5\lvl) -| (\tikzchildnode), where \lvl is the level distance. – jessexknight Oct 30 '18 at 12:46