# Special characters in parsetree

I am using the parsetree package, which according to this documentation can have special characters like parentheses in leaf nodes when not using shortcuts like ".N.". However even if I follow these rules, I still cannot get my special characters to be shown.

MWE:

\documentclass{article}
\usepackage{parsetree}
\begin{document}

\begin{parsetree}
\ptbegtree
\ptbeg{}\ptnode{ROOT}
\ptbeg{}\ptleaf{((parentheses))}
\ptend\ptend{}
\ptendtree
\end{parsetree}
\end{document}


Instead of "((parentheses))", just "parentheses" is shown.

• I get ‘You don't have permission to view “parsetree.sty”’ – egreg Dec 9 '15 at 11:04
• I provided an alternative link. – Konrad Höffner Dec 9 '15 at 11:06
• Please change the tick. – egreg Dec 9 '15 at 22:52

The examples in the documentation work fine, but your example is wrong. If you wish to use special characters within the tree, you need to use the low-level commands instead of the parsetree environment - not in addition to it.

Here's what the documentation says:

The parsetree environment is simply a wrapper that: (a) makes these special characters 'active' (i.e. special), and (b) calls the commands \ptbegtree \ptendtree at the beginning and end of the environment respectively. If we use these latter commands directly, then these special characters retain their normal meanings, and can appear in node lables [sic.]. However, we must now use the underlying parsetree commands to draw the tree ...

The point is that, within the parsetree environment, the category codes are changed. So the special characters are short-hands for the lower-level commands.

If you need to use those characters in the nodes of the tree, then you need the category codes to be their usual selves.

One solution would be to create a mechanism to temporarily change the codes back. However, that is not the solution supported by the package.

What the package supports instead is direct use of the lower-level commands, so that the category codes remain their usual selves. Obviously, this means you cannot use the special characters as short-hands. But you can use them within the nodes.

\documentclass{article}
\usepackage{parsetree}
\begin{document}
\ptbegtree
\ptbeg\ptnode{ROOT}
\ptbeg\ptleaf{((parentheses))}
\ptend
\ptend
\ptendtree
\ptbegtree
\ptbeg \ptnode{(VP)}
\ptbeg \ptnode{(V)}    \ptleaf{(saw)}  \ptend
\ptbeg \ptnode{NP}     \ptleaf{Sam's~~~toy.} \ptend
\ptend
\ptendtree
\end{document}


# Alternative Solutions

A modern package such as forest or qtree or tikz-qtree would definitely make life easier.

Here are four examples from above and/or the documentation for parsetree redone using forest. This makes it possible to use common styling and to specify the trees very concisely using bracket notation.

For a brief introduction to forest and an explanation of the brackets syntax, see the second part of my answer to an earlier question.

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage{forest,array}


Load array as we want it for trees whose nodes are multiline maths.

Define two standard styles: my forest for primarily-text-in-nodes trees and my maths forest for primarily-maths-in-nodes trees. You can always switch within a node, of course, but this saves repeating the same switches for every node as a routine.

\forestset{
my forest/.style={
for tree={
parent anchor=south,
child anchor=north,


Branches start from a common point below the parent and end top centre of the child.

      align=center


Align node contents centrally.

    }
},
my maths forest/.style={
for tree={
parent anchor=south,
child anchor=north,
align={@{}>{$}c<{$}@{}}


As before, but the content of the nodes is in maths mode by default.

    }
}
}
\begin{document}


From the question:

\begin{forest}
my forest
[ROOT
[\itshape(parentheses)]
]
\end{forest}


Here's an example with a triangular roof:

\begin{forest}
my forest,
where n children=0{font=\itshape}{}


All terminal nodes should be italicised.

  [S
[NP
[we]
]
[VP
[V
[gave]
]
[NP
[them]
]
[NP
[a toy, triangle]
]
]
]
\end{forest}


The example involving the special characters:

\begin{forest}
my forest
[(VP)
[(V)
[\textit{(saw)}]
]
[NP
[\textit{Sam's toy.}]
]
]
\end{forest}


The more complex example involving special characters from the documentation:

\begin{forest}
my maths forest
[{S\\see' (s, k)\\\lambda y see' ( y,k ) (s)}
[NP\\s
[Sam\\s
]
]
[{VP\\\lambda y see' ( y,k )\\\lambda x \lambda y see' ( y,x ) (k)}
[{V\\\lambda x \lambda y see' ( y,x ) }
[{saw\\\lambda x \lambda y see ( y,x )}
]
]
[NP\\k
[Kim\\k
]
]
]
]
\end{forest}


\end{document}


Complete code:

\documentclass[border=10pt,tikz,multi]{standalone}
\usepackage{forest,array}
\forestset{
my forest/.style={
for tree={
parent anchor=south,
child anchor=north,
align=center
}
},
my maths forest/.style={
for tree={
parent anchor=south,
child anchor=north,
align={@{}>{$}c<{$}@{}}
}
}
}
\begin{document}
\begin{forest}
my forest
[ROOT
[\itshape(parentheses)]
]
\end{forest}
\begin{forest}
my forest,
where n children=0{font=\itshape}{}
[S
[NP
[we]
]
[VP
[V
[gave]
]
[NP
[them]
]
[NP
[a toy, triangle]
]
]
]
\end{forest}

\begin{forest}
my forest
[(VP)
[(V)
[\textit{(saw)}]
]
[NP
[\textit{Sam's toy.}]
]
]
\end{forest}
\begin{forest}
my maths forest
[{S\\see' (s, k)\\\lambda y see' ( y,k ) (s)}
[NP\\s
[Sam\\s
]
]
[{VP\\\lambda y see' ( y,k )\\\lambda x \lambda y see' ( y,x ) (k)}
[{V\\\lambda x \lambda y see' ( y,x ) }
[{saw\\\lambda x \lambda y see ( y,x )}
]
]
[NP\\k
[Kim\\k
]
]
]
]
\end{forest}
\end{document}

• You quoted the documentation: the more I read it, the less I understand. – egreg Dec 9 '15 at 23:04
• Well, I think I understand it. But I'm assuming I must be mistaken if you don't understand it. I thought it was pretty simple: parsetree does 2 things. One of those is switching the cat codes. If you don't use parsetree but just do the second non-cat-code-changing thing, then the cat codes stay what they usually are and you can use the special characters normally in nodes. (\newenvironment{parsetree}{\ptactivechardefs\ptbegtree}{\ptendtree}) – cfr Dec 9 '15 at 23:16
• Fantastic! I wish I could upvote it again. – egreg Dec 10 '15 at 0:08
• It's the thought that counts ;). Thank you. – cfr Dec 10 '15 at 0:27

I see no reason whatsoever that can make the advertised examples work, if you use the parsetree environment. Indeed, they don't, because the \ptnode and \ptleaf macros don't change the category codes of the special characters back before absorbing their arguments.

According to cfr, the parsetree environment shouldn't be used when the \ptnode and \ptleaf commands are used. However, we can use the best of both worlds by just juggling category codes around. Here's how.

\documentclass{article}
\usepackage{parsetree}

% Fix the wrong macros of parsetree.sty
\def\ptnodeaux#1{\setbox\ptx=\hbox\bgroup\ptuncatcodes\ptnodeauxaux{#1}}
\def\ptnodeauxaux#1#2{#1#2\egroup\ptxx=0.5\wd\ptx\ptnext}
\def\ptnode{\ptnodeaux{\ptnodefn\ptnodestrut}}
\def\ptleaf{\ptnodeaux{\ptleaffn\ptleafstrut}}
\def\ptuncatcodes{%
\catcode(=12 \catcode)=12
\catcode.=12 \catcode=12
\def~{\nobreakspace{}}%
}

\begin{document}

\begin{parsetree}
\ptbeg\ptnode{ROOT}
\ptbeg\ptleaf{(parentheses)}
\ptend\ptend
\end{parsetree}

\begin{parsetree}
\ptbeg \ptnode{(VP)}
\ptbeg \ptnode{(V)}    \ptleaf{(saw)}  \ptend
\ptbeg \ptnode{NP}     \ptleaf{Sam's~~~toy.} \ptend
\ptend
\end{parsetree}

\end{document}


• shouldn't there be a space between "Sam's" and "toy"? (and maybe it's from looking too long at math, but i find those italic parentheses jarring.) – barbara beeton Dec 9 '15 at 13:38
• @barbarabeeton Oh, yes, I uncatcoded too much! – egreg Dec 9 '15 at 15:36
• @cfr The documentation was really unclear. I don't think this warrants a downvote and I'm going to remove this answer as soon as the OP reverts the tick. In LaTeX one expects to have \begin{...} and \end{...}, so I thought, like the OP, that the examples simply assumed them. – egreg Dec 9 '15 at 22:54
• @cfr Is it good now? Why don't you give some examples with forest` or the other packages you're expert in? – egreg Dec 9 '15 at 23:13
• @egreg Yes, thank you. (I changed my vote accordingly.) – cfr Dec 9 '15 at 23:18