nltk/nltk — pull request #348 by stesh (2013-01-23 01:26:11+00:00)
Title: Escape reserved LaTeX characters in nltk.tree.Tree.latex_pprint_qtree()

The dollar sign, a reserved character in LaTeX, appears in the possessive pronoun tag from the Penn tagset (PRP$). For example:

```python
from nltk.tree import Tree

mytree = Tree('''(S
(NP (NP (PRP$ Their) (N_N mother)))
(VP (VBZ is) (ADVP (RB inside)) (NP (NP (DT the) (NN house))))
(PP (IN with) (NPPP (DT the) (NN cat)))
(. .))''')
```

If you call `pprint_latex_qtree()`on this Tree, you get

```
>>> print mytree.pprint_latex_qtree()
\Tree [.S
[.NP [.NP [.PRP$ Their ] [.NN mother ] ] ]
[.VP
[.VBZ is ]
[.ADVP [.RB inside ] ]
[.NP [.NP [.DT the ] [.NN house ] ] ] ]
[.PP [.IN with ] [.NPPP [.DT the ] [.NN cat ] ] ]
[.. . ] ]
```

Because the reserved characters aren't escaped, this won't compile into a QobiTree Tree in a LaTeX document.

This quick fix escapes common LaTeX reserved characters (`#`, `$`, `%`, `&`, `~`, `_`, `{` and `}`):

```
>>> print mytree.pprint_latex_qtree()
\Tree [.S
[.NP [.NP [.PRP\$ Their ] [.NN mother ] ] ]
[.VP
[.VBZ is ]
[.ADVP [.RB inside ] ]
[.NP [.NP [.DT the ] [.NN house ] ] ] ]
[.PP [.IN with ] [.NPPP [.DT the ] [.NN cat ] ] ]
[.. . ] ]
```