# xypic bounding box

I am using xymatrix for a markov chain diagram. It looks awesome, but the bounding box is not correctly computed, and therefore, it overlaps with other text.

This is special bad as I am also using it on a standalone script, and the resulting image is too cropped.

Follows the MWE:


\documentclass{article}
\usepackage{lipsum}
\usepackage[all]{xy}

\begin{document}

\lipsum[1]

\begin{figure}[h]
$\xymatrix{ & a \ar@(ul,ur) \ar@/^/[rr] \ar@/^/[dd] \ar@/^/[ddrr] \ar@/^4pc/[rrrd] & & b \ar@(ul,ur) \ar@/^/[ll] \ar@/^/[dd] \ar@/^/[ddll] \ar[dr] \\ begin \ar[ur] \ar[dr] \ar@/^4pc/[urrr]\ar@/_4pc/[drrr] & & & & end \\ & c \ar@(dr,dl) \ar@/^/[rr] \ar@/^/[uu] \ar@/^/[uurr] \ar@/_4pc/[rrru] & & d \ar@(dr,dl) \ar@/^/[ll] \ar@/^/[uu] \ar@/^/[uull] \ar[ur] }$
\caption{Exemplo de uma cadeia de Markov\label{mkv1}}
\end{figure}

\lipsum[1]
\end{document}


And this is the image when I use standalone

Any hint on what to wrap the xymatrix into, in order to force it to display completely?

Thanks

EDIT: somewhat related to Add padding to the box of object grouping in xymatrix, but I didn't want to have to specify paddings for each different image...

• As with most other things xy, nowadays I'd do an image like this in tikz instead. Much easier to work with and much more control. Mar 26, 2019 at 11:43
• I am too used to xy-pic. I really have in my to-do list to practice tikz, but when you have too much to do, you tend to use the tools you are more used to. Mar 26, 2019 at 11:45
• If you use amsmath and wrap \xymatrix{} with \boxed{...} you can see the bounding box. I don't see any automatic way out of this. It seems certain constructions in xy does not contribute to the bounding box (note that we can have issues like that in tikz as well, but setting a specific bounding box in tikz is easy). Mar 26, 2019 at 12:54

If you add \fbox{...} around the \xymatrix construction, you'll see that the bounding box is determined by the nodes “a, b, c, d, begin, end”.

(I made a few changes: first of all I added the cmtip option to xy that makes for decent arrow tips, then typed \txt{begin} and \txt{end} for the textual nodes.)

Curved arrows are never taken into consideration by Xy-pic for determining the bounding box. You can adjust it by hand with a rule:

\documentclass{article}
\usepackage{lipsum}
\usepackage[all,cmtip]{xy}

\begin{document}

\lipsum[1]

\begin{figure}[htp]
$\fboxsep=0pt \fbox{% \vrule height 7ex depth 24.5ex width 0pt \xymatrix{ & a \ar@(ul,ur) \ar@/^/[rr] \ar@/^/[dd] \ar@/^/[ddrr] \ar@/^4pc/[rrrd] & & b \ar@(ul,ur) \ar@/^/[ll] \ar@/^/[dd] \ar@/^/[ddll] \ar[dr] \\ \txt{begin} \ar[ur] \ar[dr] \ar@/^4pc/[urrr]\ar@/_4pc/[drrr] & & & & \txt{end} \\ & c \ar@(dr,dl) \ar@/^/[rr] \ar@/^/[uu] \ar@/^/[uurr] \ar@/_4pc/[rrru] & & d \ar@(dr,dl) \ar@/^/[ll] \ar@/^/[uu] \ar@/^/[uull] \ar[ur] }}$
\caption{Exemplo de uma cadeia de Markov\label{mkv1}}
\end{figure}

\lipsum[1]
\end{document}


Adjust the height and depth until satisfied and then remove the \fbox part:

\documentclass{article}
\usepackage{lipsum}
\usepackage[all,cmtip]{xy}

\begin{document}

\lipsum[1]

\begin{figure}[htp]
$\vrule height 7ex depth 24.5ex width 0pt \xymatrix{ & a \ar@(ul,ur) \ar@/^/[rr] \ar@/^/[dd] \ar@/^/[ddrr] \ar@/^4pc/[rrrd] & & b \ar@(ul,ur) \ar@/^/[ll] \ar@/^/[dd] \ar@/^/[ddll] \ar[dr] \\ \txt{begin} \ar[ur] \ar[dr] \ar@/^4pc/[urrr]\ar@/_4pc/[drrr] & & & & \txt{end} \\ & c \ar@(dr,dl) \ar@/^/[rr] \ar@/^/[uu] \ar@/^/[uurr] \ar@/_4pc/[rrru] & & d \ar@(dr,dl) \ar@/^/[ll] \ar@/^/[uu] \ar@/^/[uull] \ar[ur] }$
\caption{Exemplo de uma cadeia de Markov\label{mkv1}}
\end{figure}

\lipsum[1]
\end{document}


• I am doing a similar thing (with vskips) at the moment. But I was trying to find a solution where I didn't need to tweak manually. Nevertheless, thanks for your comments on aspect, and the rule that I did not think of. Mar 26, 2019 at 17:10
• @Alberto I'm not sure there is an automatic way to compute the actual bounding box. TBH, I think there is none, because the computations are deferred to the PDF driver directly. Mar 26, 2019 at 18:19
• Yeah, probably you are right. I solved with an environment where I specify a top and bottom margin, so I can at least tweak that directly without much typing. Mar 27, 2019 at 19:04
• Chose this as the correct answer, given @egreg comment, that there isn't a way to do it. So, if I still want to use xy-pic, I need to deal with the size manually. Mar 27, 2019 at 19:05

Too long for a comment. Here is a version in tikz. This has the oposite problem, here the bounding box is actually too large.

\documentclass{standalone}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
{} & a\mathstrut
\arrow[loop above]
\ar[dd,bend left=10]
\ar[rr,bend left=10]
\ar[rrdd,bend left=10]
\ar[rrrd,out=45,in=95,looseness=1.5]
&
& b\mathstrut
\arrow[loop above]
\ar[dd,bend left=10]
\ar[ll,bend left=10]
\ar[lldd,bend left=10]
\ar[rd]
\\
begin\mathstrut
\ar[ru]
\ar[rd]
\arrow[rrru,out=85,in=135,looseness=1.5]
\ar[rrrd,out=-85,in=-135,looseness=1.5]
&
&
&
& end\mathstrut
\\
{} & c\mathstrut
\ar[uu,bend left=10]
\ar[rr,bend left=10]
\ar[uurr,bend left=10]
\arrow[loop below]
\ar[rrru,out=-45,in=-95,looseness=1.5]
&
& d\mathstrut
\ar[uu,bend left=10]
\ar[ll,bend left=10]
\ar[uull,bend left=10]
\arrow[loop below]
\ar[ru]
\\
\end{tikzcd}
\end{document}


You can easily play with the arrow heads

\usetikzlibrary{arrows,arrows.meta}
\tikzcdset{
arrow style=tikz,
diagrams={>=Latex[round]}
}

• Not relevant, but not sure if you want to fix the code, in order to put the arrows to end in the right direction. Mar 26, 2019 at 17:57
• @Alberto, my bad, updated. As I hope you can see the syntax here isn't that different from xy, but often not as cryptic. Mar 26, 2019 at 18:11
• yep, I did not know about this tikz module. I might start using it. Mar 27, 2019 at 19:03
• @Alberto at least for diagrams, I think it looks better, plus the injective arrow is made properly (looks horrible in xy). Tikz is well worth the time investment Mar 27, 2019 at 19:09