logo
Jiff Slater
🤔 About
✍️ Contact
📚Knowledge
30 Jul 2021
These articles have been archived. You may find them useful but I am no longer offering support for them. Check out my latest articles on plkt.io.
Making Diagrams with Xy-pic
8 July 2008

Recently, I upgraded my $latex \LaTeX$ installation from teTeX to TeX Live.  As I was reviewing the available packages, I noticed $latex Xy-pic$, a set of macros for creating diagrams and graphs.  I was immediately intrigued and I started reading the relatively short manual.

Today, I will show you a few of the things $latex Xy-pic$ can do.

1. First, create a file and insert the $latex \LaTeX$ document class and the macro package.

~ % cd ~/tmp
~ % touch example.tex
~ % vim example.tex
1 \documentclass{article}
2 \usepackage[all]{xy}
3 \begin{document}
4
5 \end{document}

All of our editing will stay between “\begin{document}” and”\end{document}”.

2. Next, let’s begin with our first example.

1 \documentclass{article}
2 \usepackage[all]{xy}
3 \begin{document}
4 \xymatrix@1{A \ar[r] & B}
5 \end{document}

The above example will create:

Explanation: \xymatrix initialises the macro environment and @1 restricts the output to a single line.  \ar creates an arrow and the direction, [r], points the arrow in the right direction.  & separates the columns.  The direction is translated from its mnemonic form (right,left,up,down) into a coordinate pair based upon its initial position.  In this case, [r] means (1,0).  Using [l] would result in an error because [l], which is (-1,0), doesn’t exist.

Throughout the rest of this tutorial, I’ll remove everything except the \xymatrix macro; remember to add the LaTeX commands (lines 1,2, 3, and 5) before you compile the document.  Also, I will bold changes in each progressive example.

3. Let’s add some more letters and arrows to the previous example. (It would have better to use a double headed arrow style, see ex. #7.)

5 \xymatrix@1{A \ar[r] & B \ar[r] & C \ar[l]}

4. Let’s change the C to bullet.

6 \xymatrix@1{A \ar[r] & B \ar[r] & \bullet \ar[l]}

Explanation: \bullet creates a bullet

5. Add another row.

7 \xymatrix{
8 A \ar[r] & B \ar[r] & \bullet \ar[l] \\
9 D & &
10 } 

Explanation: The “\\” creates a linebreak.  Even though no content follows the D, we still have to create the empty columns with &’s.  Since we are now dealing with multiple rows, we have to remove the @1.

6. Create an arrow from A to D.

11 \xymatrix{
12 A \ar[r] \ar[d] & B \ar[r] & \bullet \ar[l] \\
13 D & &
14 }

Explanation: \ar[d] creates a downward arrow to (0,-1), which in this case, is D.

7. Now, let me introduce you to styles.  This time, we’ll begin with a fresh example.

15 \xymatrix@1{
16 A \ar@{<->}[r] & B}

Explanation: The @{<->} is in a @{tail shaft head} format, so in this case, it creates a double-headed arrow.

8. Another example of styling arrows.

17 \xymatrix@1{
18 A \ar@{-}[r] & B}

Explanation: @{-} creates an arrow with only a shaft.

9. Next, an example of labelling arrows.

19 \xymatrix@1{
20 A \ar@{-}[r]^f & B \ar@{-}[r]_f & C }

Explanation: This is not as complicated as it looks.  “^” means up; likewise “_” means down.

10. What about a label with more than one character?

21 \xymatrix@1{
22 A \ar@{-}[r]^{more} & B \ar@{-}[r]_f & C }

Explanation: The “{}” is a container for extra characters.

11. What happens if we lengthen the arrow?

23 \xymatrix@1{
24 A \ar@{-}[rr]^{more} & & B \ar@{-}[r]_f & C }

Explanation: Here, the label stays centered.  Note that we had to modify the direction “[rr]” to account for the blank column between A and B.

12. What about curved arrows?

25 \xymatrix@1{
26 A \ar@{-}@/^/[rr]^{more} & & B \ar@{-}@/_/[r]_f & C }

Explanation: All we did here was add another style “@/^/” and “@/_/”.  /^/ means curve upwards; likewise, /_/ means curve downwards.

13. The last example: a square.

First, we create the outline of the square.

27 \xymatrix{
28 A & B \\
29 C & D
30 }

Next, we connect the corners with arrows.

31 \xymatrix{
32 A \ar[r] \ar[d] & B \ar[d] \\
33 C \ar[r] & D
34 }

Next, we change the styling of the arrows and add labels to each side.

35 \xymatrix{
36 A \ar@{-}[r]^e \ar@{-}[d]_f & B \ar@{-}[d]^g \\
37 C \ar@{-}[r]_h & D
38 }

Finally, let’s add some curved arrows, just for the sake of it.

39 \xymatrix{
40 A \ar@{-}[r]^e \ar@{-}[d]_f & B \ar@{-}[d]^g \ar@/^1pc/[d]\\
41 C \ar@{-}[r]_h \ar@/_1pc/[r]& D
42 }

The added “1pc” makes the curve more prominent so it does not overlap the label.

This tutorial only covers pages 1 – 4 of the 16 page manual.  As you can tell, it is dense.
Xy-pic is a great $latex \LaTeX$ package that can be used to create beautiful diagrams when typesetting documents.