# Logic tree with tikz-tree — placing nodes on specific places

I am trying to reproduce the following with tikz:

And also:

\documentclass{article}
\usepackage{tikz-qtree}
\usepackage{tikz-cd}

\tikzstyle{every picture}+=[remember picture]

\begin{displaymath}
((\neg Qb \wedge x (Px \rightarrow Qx))
\tikz[baseline]\node (n1)     {$\rightarrow$}; \neg Pb)
\end{displaymath}

\begin{displaymath}
(\neg Qb \wedge \tikz[baseline]\node (n2) {$\forall$};x
(Px \rightarrow Qx)) \qquad \tikz[baseline]\node (n3) {$\neg Pb$};
\end{displaymath}

\begin{tikzpicture}[overlay]

\draw (n1) -- (n2);
\draw (n1) -- (n3);

\end{tikzpicture}

\end{document}


The result, however, is far from elegant, and I do not know how to proceed from that:

• Does it have to use tikz? I can do the first if I don't have to use tikz! (Not sure about the second as I've not tried but I've typeset a bunch of the first kind.) – cfr Jan 6 '14 at 23:48
• By the way, why are you trying to typeset logical errors? Is this for some sort of spot-the-error exercise? (I only ask because I'm curious having just typeset one - it is much harder to typeset it wrong than right! I don't mean the typesetting is harder - just I kept finding myself doing a bit correctly instead of copying the picture.) – cfr Jan 7 '14 at 0:18

For the first tree, if the branches do not need to start from the main logical connectives, then the TikZ-based forest package would be a good option.

The second tree can also be drawn using forest, as if it were growing from bottom to top.

# Code

\documentclass{article}
\usepackage{forest}
\renewcommand\b[1]{\textbf{#1}}

\begin{document}
\begin{forest}
for tree={parent anchor=south,child anchor=north}
[$((\lnot Qb \wedge \forall x(Px \to Qx)) \to \lnot Pb)$, s=1cm
[$(\lnot Qb \wedge \forall x (Px\to Qx))$
[$\lnot Qb$
[$Qb$]
]
[$\forall x (Px\to Qx)$
[$(Px\to Qx)$
[$Px$]
[$Qx$]
]
]
]
[$\lnot Pb$
[$Pb$]
]
]
\node[below,red]at(current bounding box.south){Figure: First Tree};
\end{forest}
%
\hskip20pt
%
\begin{forest}
for tree={
child anchor=south,grow'=north,
edge path={\noexpand\path[\forestoption{edge}](!u.parent anchor)-|(.child anchor)\forestoption{edge label};}
},
[\b{V}
[\b{F}
[\b{V}[$(C$,no edge]]
[$\wedge$,no edge,tier=final]
[\b{F}[$B)$,no edge]]
]
[\b{V},name=n
[$\to$,no edge,tier=final]
[,no edge,name=nc
[$\lnot$,no edge,tier=final]
]
[\b{F}[$D$,no edge,tier=final]]
]
]
\draw(n.north)--(nc.north);
\node[below,red]at(current bounding box.south){Figure: Second Tree};
\end{forest}
\end{document}


# Output

• Ouch - the thing about centring on main connectives hadn't occurred to me though I can see why you might want to do that. I wonder if there is any neat way of achieving that. Can forest cope with the second example? I've only ever used qtree and have no idea how to typeset a 'tree' of that sort. – cfr Jan 7 '14 at 0:45
• @cfr: I can't think of any neat way of making the tree branching off from the main connectives... I tried manually drawing it, but it's too messy. I'm working on the second example now. The way I'm approaching it is to draw a tree that "grows" from bottom to top. – Herr K. Jan 7 '14 at 0:50
• I think the xyling package might possibly be able to do it but it looks to be extremely complicated to adapt for symbolic logic. (It's default assumption is all node labels will be short as in linguists' trees.) I think it would require so much manual adjustment that it would either be a real mess or an (under-appreciated) work of art! – cfr Jan 7 '14 at 1:12
• Forest looks really fantastic - too bad it wasn't around the year before when I was looking into this stuff. (Though qtree worked out pretty well for my needs.) – cfr Jan 7 '14 at 1:19
• @cfr: Yeah, forest is a really great. I like it for its slightly simpler syntax than the other alternatives, and that it's TikZ-based. But it takes a bit of learning, even if one is already sufficiently familiar with TikZ. I still have a long way to go before mastering this package. – Herr K. Jan 7 '14 at 2:14

I don't know if this is any use as it does not use tikz but here is how I would typeset the first example using the qtree package:

\documentclass{article}
\usepackage{qtree}
\usepackage{amssymb}
\begin{document}
\Tree
[.{$((\neg Qb \wedge \forall x (Px \rightarrow Qx)) \rightarrow \neg Pb)$}
[.{$(\neg Qb \wedge \forall x (Px \rightarrow Qx))$}
[.{$\neg Qb$}
[.{$Qb$}
]
]
[.{$\forall x (Px \rightarrow Qx)$}
[.{$(Px \rightarrow Qx)$}
[.{$Px$}
]
[.{$Qx$}
]
]
]
]
[.{$\neg Pb$}
[.{$Pb$}
]
]
]
\end{document}


Warning: This is NOT correct logically but is, in fact, full of errors. Do not use this if you are learning logic just because it happens to be on a website!

Note that older versions of qtree apparently had issues with pdflatex but the current version is compatible (and I've always used it with pdflatex).

• It was just someone who happens to teach logic who asked for help to typeset this as a presenation to his students. I had no idea it was wrong. Do the connectives have to be as in the example, or can they appear as in your answer above? – BlackBird Jan 7 '14 at 8:35
• My use of qtree was originally based on recommended packages from the LaTeX for Logicians website and I used this to teach logic seminars. Logically, it doesn't matter at all. Pedagogically, branching from the main connective would be nice but I haven't seen any solution which permits that. Plus I think that any such solution is going to make it really hard to create balanced trees of any kind as sometimes it will just make things ridiculously lopsided. One alternative would be to highlight the main connective which I think would be equally effective for teaching but much easier to implement. – cfr Jan 7 '14 at 15:15
• I am really not sure if this is the place, but could you please tell me what errors you found in the tree? Or would that be a question for http://math.stackexchange.com/? – BlackBird Jan 7 '14 at 20:24
• I'm not sure if there is an SE site for philosophy though I guess it is maths, too. Anyway, at the first branch, the left hand side should be negated. So everything else in the left main branch is also wrong. Also, there is nothing which would justify entering Pb in the terminal of the right hand branch. On the left, x occurs as an unbound variable which is almost certainly not permitted and, even if it were, Px would need to be negated. And there is nothing which would justify entering Qb even if everything above it was correct. – cfr Jan 7 '14 at 21:32
• The and elimination is the only case where the rules are applied correctly. (Obviously since the preceding line is wrong, this is also wrong but if the preceding line were correct, the application of and elimination would be correct.) That is, unless this is some very, very funky logic which is extremely non-standard. I'm assuming this is basically first-order propositional calculus and that the connectives have their standard meanings, that the system is sound etc. – cfr Jan 7 '14 at 21:33