Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am making a tree that reflects a number of different sequential choices. The lower parts of the tree will be repeated, so I'd like to store them in some shorter definition earlier on to make it possible to modify them and keep the code clean.

MWE:

\documentclass[11pt]{article}
\usepackage{qtree}
\title{Map}

\begin{document}

\maketitle

\Tree[.O.2-30Hz [.{Raw Stats} [.Ensemble Bag 
                                         [.Boost Ada Total LP ] ]
                [.LDA [.SWLDA ]]]
          [.PCA [.Ensemble Bag 
                                         [.Boost Ada Total LP ] ]
                [.LDA [.SWLDA ]]]]

\end{document}

mwe

How can I store the redundant parts of the tree separately in the TeX?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

The manual for qtree covers the case I think you have in mind:

5.2 Embedding one tree in another

The internal implementation of qtree only supports one tree being built at one time; hence it does not allow a complete tree to be recursively embedded as part of another one. This is not actually needed very often (trees are already recursive, after all); for most constructions that seem to require it, there’s a simpler way to get the same effect. But it might be necessary, for example, if you need to put a subtree under a roof.

Fortunately it’s straightforward to embed one tree in another using TEX’s facilities, without ever calling qtree recursively. We can do this by saving the first tree in a named box, and then using it as part of the larger tree.

Here’s how:

\newsavebox{\partbox} % Declare this only once, in your preamble!

% Build the subtree and save it in the box
\setbox\partbox=\hbox{\Tree [.DP my pony ]}

% Use the subtree in the containing tree
\Tree [ how
  \qroof{about \usebox{\partbox} ...}.PP
].S

Obviously you don't want to use roofs but the principle should be the same. Save the trees in boxes and then use those boxes at the appropriate points in the tree:

\documentclass[11pt]{article}
\usepackage{qtree}
\title{Map}

\begin{document}

\maketitle

\newsavebox{\ldabox}
  \sbox{\ldabox}{\hbox{\Tree [.LDA [.SWLDA ]]}}
\newsavebox{\boostbox}
  \sbox{\boostbox}{\hbox{\Tree [.Boost Ada Total LP ]}}

\Tree[.O.2-30Hz [.{Raw Stats} [.Ensemble Bag
                                         \usebox{\boostbox} ]
                \usebox{\ldabox} ]
          [.PCA [.Ensemble Bag
                                         \usebox{\boostbox} ]
                \usebox{\ldabox} ]]

\end{document}

produces:

box tree?

EDIT

To make this a bit more convenient, you could define a macro to create and populate boxes on the fly. The following code is an alternative way of generating the above output:

\documentclass[11pt]{article}
\usepackage{qtree}
\title{Map}

\newcommand{\myTree}[2]{%
  \def\tempa{#1box}%
  \expandafter\newsavebox\csname\tempa\endcsname%
  \expandafter\sbox\csname\tempa\endcsname{\hbox{\Tree [#2]}}%
  }

\begin{document}

\maketitle

\myTree{lda}{ .LDA [.SWLDA ] }
\myTree{boost}{ .Boost Ada Total LP }


\Tree[.O.2-30Hz [.{Raw Stats} [.Ensemble Bag
                                         \usebox{\boostbox} ]
                \usebox{\ldabox} ]
          [.PCA [.Ensemble Bag
                                         \usebox{\boostbox} ]
                \usebox{\ldabox} ]]

\end{document}

\myTree takes two arguments. The first is a string of letters which form the prefix of the box name. The second is the 'inside' of the tree.

Suppose you want to reuse the following tree:

\Tree [ .a b c ]

You could write:

\myTree{abc}{ .a b c }

You would then be able to use the corresponding tree either alone or within other trees:

\usebox{\abcbox}
share|improve this answer

This is an easy job for the powerful forest package without any user defined boxes (please refer to Section 3.3.8 Dynamic tree of the package documentation, where keys for dinamyc changes of a tree are intrduced):

\documentclass[11pt]{article}
\usepackage{forest}

\begin{document}

\begin{forest}
for tree={parent anchor=south},
copy name template={copy of #1}
[O.2-30Hz 
  [Raw Stats,delay={prepend'=ensamble} 
    [LDA,name=lda 
      [SWLDA ]
    ]
  ]
  [PCA,delay={append'=lda} 
    [Ensemble,name=ensamble
      [Bag] 
      [Boost 
        [Ada] 
        [Total] 
        [LP] 
      ] 
    ]
 ]
]
\end{forest}

\end{document}

enter image description here

share|improve this answer
    
I'm looking at the documentation; it's very concise, but is it possible to define trees that won't be displayed until appended? –  Trevor Alexander Mar 6 at 3:58
    
@TrevorAlexander I'm not sure I understand your question. Did you see the example on top of page 41 using before typesetting nodes? –  Gonzalo Medina Mar 6 at 12:27
\documentclass[11pt]{article}
\usepackage{qtree}
\title{Map}

\begin{document}

\maketitle
\newsavebox\boxA
\sbox{\boxA}{\Tree[.Ensemble Bag 
                                         [.Boost Ada Total LP ] ]}
\newsavebox\boxB
\sbox{\boxB}{\Tree[.LDA [.SWLDA ]]}

\Tree[.O.2-30Hz [.{Raw Stats} \usebox{\boxA}
                \usebox{\boxB} ]
          [.PCA \usebox{\boxA}
                \usebox{\boxB} ]]

\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.