2

I have a code for a tree

\[
\xymatrix@-1.25pc{
   *={\bullet}  &  &*={\bullet} &  
   \frm{}\\
   &*={\bullet}\ar@{-}[lu] \ar@{-}[ru]& & 
 }
\]

which will produce this

complete_tree

as its output. However, I would like to number the top nodes as follows:

complete_tree-2

yet I would like the line segments to be connected to the center of the vertices. I tried replacing \bullet with \stackrel{i}{\bullet} as follows:

\[
\xymatrix@-1.25pc{
*={\stackrel{1}{\bullet}}  &  &*={\stackrel{2}{\bullet}} &  
\frm{}\\
&*={\bullet}\ar@{-}[lu] \ar@{-}[ru]& & 
 }
\]

but the nodes are no longer connected to the edges:

complete-tree-3

which is not what I want.

Alan Munn
  • 218,180

2 Answers2

4

A solution with TiKz. (I never used the xy package).

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\tikzset{%
pics/bullet/.style args={#1,#2}{
    code={
    \node at (0,1) [draw,circle,fill,minimum size=2mm,inner sep=0pt,label={$#1$}](#1){};
    \node at (2,1) [draw,circle,fill,minimum size=2mm,inner sep=0pt,,label={$#2$}](#2){};
    \node at (1,0) [draw,circle,fill,minimum size=2mm,inner sep=0pt](bb){};
    \draw (bb)--(#1) (bb)--(#2);
    }}}
\begin{document}

\begin{tikzpicture}
\pic at (0,0) {bullet={1,2}};
\pic at (3,0) {bullet={3,4}};
\end{tikzpicture}

\end{document}

enter image description here

4

The XY syntax is quite concise and powerful, but it takes some getting used to. But it's easy to add the labels relative to the position of the bullet using the \save and \restore function of XY. See my answer to Pushout commutative diagram for a detailed explanation of how that works.

\documentclass{article}
\usepackage[all]{xy}
\begin{document}

\[
\xymatrix@-1.25pc{
   *={\bullet}\save+<0ex,2ex>*={1}\restore  &  &*={\bullet}\save+<0ex,2ex>*={2}\restore&  
   \frm{}\\
   &*={\bullet}\ar@{-}[lu] \ar@{-}[ru]& & 
 }
\]
\end{document}

output of code

Alan Munn
  • 218,180