latextikz

Set distance for every sibling individually in TikZ


I have the following code in LaTeX:

\begin{figure}
    \centering
    \begin{tikzpicture}[nodes={draw, circle}, -, 
        level/.style = {level distance = 1.5cm, sibling distance = 1cm},
        level 1/.style={sibling distance=3cm}]
        \node [minimum size=0.75cm]{$\rightarrow$\strut}
        child { node[rectangle, minimum size=0.75cm] {a\strut} }
        child { node [minimum size=0.75cm] {$\land$} 
            child { node [rectangle, minimum size=0.75cm] {b\strut}}
            child { node [rectangle, minimum size=0.75cm] {c\strut}}
            child { node [rectangle, minimum size=0.75cm] {d\strut}}
            child { node [rectangle, minimum size=0.75cm] {e\strut}}
        }
        child { node[rectangle, minimum size=0.75cm] {g\strut} }
        child { node [minimum size=0.75cm] {$\times$} 
            child { node [rectangle, minimum size=0.75cm] {h\strut}}
            child { node [rectangle, minimum size=0.75cm] {\tau \strut}}
        }
        child { node [minimum size=0.75cm] {$\land$} 
            child { node [rectangle, minimum size=0.75cm] {i\strut}}
            child { node [rectangle, minimum size=0.75cm] {j\strut}}
            child { node [rectangle, minimum size=0.75cm] {k\strut}}
            child { node [rectangle, minimum size=0.75cm] {l\strut}}
            child { node [minimum size=0.75cm] {$\times$} 
                child { node [rectangle, minimum size=0.75cm] {m\strut}}
                child { node [rectangle, minimum size=0.75cm] {\tau \strut}}
            }
        }
        child { node[rectangle, minimum size=0.75cm] {n\strut} };
    \end{tikzpicture}
\end{figure}

It produces this output:

enter image description here

How can i set the distance between g, the logical and, the multiplication symbol and the right and so that the tau and i node do not overlap?


Solution

  • Please, always provide the full code as a MWE we can compile and test!


    There's already an answer since you have cross-post over TeX.SE as well. However, I'd like point a few things in this answer.


    I'd have a few remarks regarding your code.

    Firstly, if it's just for this one node, you could apply xshift to apply a local distance; this is also affects the whole sub tree.

    Secondly, You can significantly minimise your code by avoiding all those repeated specifications, which in turn can be handled by global styles; similarly what you did with tree levels. Styles also accept more meaningful names.

    Consider using macros. This is one of the main advantages of LaTeX and you should exploit this advantage wherever you can. For instance, avoid repeated application of \strut in every node. Just for the demo, I applied bold font to each leaf including math symbols, which require \boldsymbol instead of \textbf. Thanks to a macro, I could just make one change to remove the bold in each leaf that utilises this macro.

    Here's more optimised code with a sorted branch:

    \documentclass{article}
    \usepackage[margin=1in,a4paper,landscape]{geometry}
    \usepackage{amssymb,amsmath}
    \usepackage{tikz}
    \usetikzlibrary{arrows.meta, positioning}
    
    \tikzset{
      operator/.style = {circle},
      leaf/.style = {rectangle},
      every node/.style = {draw, leaf, minimum size=0.75cm},
      level/.style = {level distance=1.5cm, sibling distance=1cm},
      level 1/.style = {sibling distance=3cm},
    }
    
    % \newcommand\an[1]{#1}
    \newcommand\an[1]{\ifmmode\boldsymbol{#1}\else\textbf{#1}\fi}
    
    
    \begin{document}
    \begin{figure}
        \centering
        \begin{tikzpicture}
            \node [operator] {$\rightarrow$}
            child { node {\an a} }
            child { node [operator] {$\land$} 
                child { node {\an b} }
                child { node {\an c} }
                child { node {\an d} }
                child { node {\an e} }
            }
            child { node {\an g} }
            child { node [operator, xshift=-1cm] {$\times$} 
                child { node {\an h} }
                child { node {$\an \tau$} }
            }
            child { node [operator] {$\land$} 
                child { node {\an i} }
                child { node {\an j} }
                child { node {\an k} }
                child { node {\an l} }
                child { node [operator] {$\times$} 
                    child { node {\an m} }
                    child { node {$\an \tau$} }
                }
            }
            child { node {\an{n}} };
        \end{tikzpicture}
      \end{figure}
    \end{document}
    

    enter image description here