2

Why are the two trees different?

\documentclass[tikz,12pt,letterpaper]{article}
\usepackage{qtree}
\newcommand{\en}{\phantom{1}}
\begin{document}

Using phantom:

\Tree[.50   [.25    [.12 \phantom{1} 22 ]
                    [.47 33 50 ]]
            [.75 66 99 ]]

Using custom command:

\Tree[.50   [.25    [.12 \en 22 ]
                    [.47 33 50 ]]
            [.75 66 99 ]]
\end{document}

The first one recognizes the \phantom{1} as a node. However, when the same command is put into a custom command, it's no longer read as a node. Why does this occur and is there a work around? I need to make a tree with several empty nodes.

I am looking into forest but as of yet have not figured out how to make it work.

Thanks for your help.

2 Answers2

1

Here's a Forest version:

\documentclass[border=10pt,tikz]{standalone}
\usepackage[linguistics]{forest}
\begin{document}
\begin{forest}
  delay={
    where content={}{content=\strut}{}
  }
  [50
    [25
      [12
        []
        [22]
      ]
      [47
        [33]
        [50]
      ]
    ]
    [75
      [66]
      [99]
    ]
  ]
\end{forest}
\end{document}

Forest version

cfr
  • 198,882
  • Thanks for your response, it helped decipher forest a little bit. But, I was already in the process of migrating to forest as stated in my question. I was more curious about why Latex was exhibiting this strange behavior with custom commands in qtree. – The Great Java Oct 06 '16 at 18:13
1

Qtree reads your tree and breaks it into tokens whenever it sees a space. Because of the way TeX works, when you write \en 22 the space is processed by TeX's input routine before qtree has had a chance to see it. To get it to work, use one of the following standard approaches:

  1. Since your goal is an empty branch, just write ~ (a non-breaking space) instead of your custom phantom command:

    \Tree [.12 ~ 22 ]
    
  2. More generally, for any custom macro you want to use this way, add harmless syntax that avoids having a space immediately after your command:

    \Tree [.12 \en{} 22 ]
    \Tree [.12 {\en} 22 ]
    

Or you can try one of the following, slightly more creative solutions

  1. Use a single punctuation character as your command name (but make sure you don't override a standard command you will need). Punctuation cannot be used in multi-character names (except by reassigning character classes, which never mind), so TeX does not swallow the space after such a command.

    \renewcommand\~{\phantom{1}}
    \Tree [.12 \~ 22 ]
    
  2. Use TeX (not LaTeX) syntax to define a command that must be followed by punctuation when invoked. The punctuation (which is obligatory, but is not part of the command name) separates the command name from the space that follows.

    \def\en/{\phantom{1}}
    \Tree [.12 \en/ 22 ]
    

You can also arrange for your special command to take effect only in trees (this is especially useful with the third option, since most single-symbol commands are already in use). Instead of defining your command globally, define \qtreeinithook to define your command; the hook will be executed, with local scope, whenever you start a new tree.

    \newcommand\qtreeinithook{\renewcommand\~{\phantom{1}}}
alexis
  • 7,961
  • Please make clear that redefining \- should be done only locally, if at all. (I assume it would be safe locally, though I'm not certain.) And that \def requires the user to be absolutely sure the command does not already exist. \newcommand\en{} and then \def\end... would be safer. – cfr Oct 06 '16 at 01:09
  • It's \~, not \-, and I already wrote "make sure you don't override a standard command you will need". But I added instructions on how to define commands that are only in effect while building a tree. – alexis Oct 06 '16 at 08:02
  • Thanks, this is pretty much exactly what I was looking for. – The Great Java Oct 06 '16 at 18:12