2

Whenever I am trying to use \caption, \label and cross-references in xymatrix, it is not working. An MWE :

\documentclass{book}
\usepackage[a4paper,margin=.45in]{geometry}
\usepackage{xypic}

\begin{document}
$$
\xymatrix{ A \ar[r]\ar[d] & B\ar[d]\\
C\ar[r] & D
\caption{Rectangular diagram}
\label{Fig1}
}
$$
The above figure \ref{Fig1} is a commutative diagram.
\end{document}

Anyone can tell me how do I fix this problem ?

  • "It is not working" is not descriptive enough. Why not post a minimal working example (MWE) to illustrate what the problem is? – jub0bs May 23 '13 at 12:27
  • 2
    You need to use a float environment like figure or table to use the \caption macro. If you don't want a float, take a look at the \captionof macro from the caption package. Besides that, the package is simply called xy: \usepackage[all]{xy} works. – Qrrbrbirlbel May 23 '13 at 13:15
  • 2
    You need to wrap your diagram, label and caption in \begin{figure} ... \end{figure}. See en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions. Also, don't use $$ for display math, use \[ ... \] (see Why is [ … ] preferable to $$ … $$?) – Jake May 23 '13 at 13:16
  • 4
    @MdKutubuddinSardar xymatrix is environment defined in xy-pic package which is loaded by \usepackage[all]{xy} in preamble – texenthusiast May 23 '13 at 13:25
  • 4
    Friendly advice my dear: forum can better serve you when one learns from past and kindly accept answer with green tick to encourage/thank people. All the best. – texenthusiast May 23 '13 at 13:37
5

The \caption macro only works in float environments like table or figure (unless you want a non-floating description for a figure, then you can use the \captionof macro provided by the caption package or the KOMA-Script classes.

The \xymatrix is not horizontally centered by default, you can use either LaTeX’s \[ … \] macros (which adds vertical space that is removed with \tightdisplaymath) or—not recommended—a manual centering with the \centerline macro or a pair of \hspace{\fill}.

Do not use [h] or [h!] in your document. It is only used here to show the floats on one page. If you want fixed positions in your running text use \captionof or simply a numbered math environment.

Code

\documentclass{book}
\usepackage[a4paper,margin=.45in]{geometry}
\usepackage[all]{xy}
\makeatletter
\newcommand*{\tightdisplaymath}{\abovedisplayskip\z@\belowdisplayskip\z@}
\makeatother
\begin{document}
\hrulefill

\begin{figure}[h] 
 \[ \xymatrix{
  A \ar[r]\ar[d] & B\ar[d]\\
  C \ar[r]       & D
 } \]
\caption{Rectangular diagram}\label{Fig1a}
\end{figure}

\hrulefill

\begin{figure}[h] \tightdisplaymath
 \[ \xymatrix{
  A \ar[r]\ar[d] & B\ar[d]\\
  C \ar[r]       & D
 } \]
\caption{Rectangular diagram}\label{Fig1b}
\end{figure}

\hrulefill

\begin{figure}[h] \centerline{%
 \xymatrix{
  A \ar[r]\ar[d] & B\ar[d]\\
  C \ar[r]       & D
 }}
\caption{Rectangular diagram}\label{Fig2}
\end{figure}

\hrulefill

\begin{figure}[h!]
 \hspace{\fill}%
 \xymatrix{
  A \ar[r]\ar[d] & B\ar[d]\\
  C \ar[r]       & D
 }%
 \hspace{\fill}
 \caption{Rectangular diagram}\label{Fig3}
\end{figure}

\hrulefill

\hrulefill

Figure \ref{Fig1a} and \ref{Fig1b} are commutative diagrams.

Figure \ref{Fig2} is the same commutative diagram.

Figure \ref{Fig3} is also the same commutative diagram.
\end{document}

Output

enter image description here

5

Caption cannot be used that way. And please don't use $$...$$ in a LaTeX document. Basically you are trying to add a caption inside the cell containing D. Normally one would not make a commutative diagram into a figure, but leave it as a mathematical construction can giving it an equation number, say using

\documentclass{book}
\usepackage[a4paper,margin=.45in]{geometry}
\usepackage[all]{xy}
\usepackage{amsmath}
\begin{document}

\begin{gather}
\begin{aligned}
\xymatrix{ A \ar[r]\ar[d] & B\ar[d]\\
C\ar[r] & D }
\end{aligned}
\label{Fig1}
\end{gather}
The above  \eqref{Fig1} is a commutative diagram.
\end{document}

the use of aligned is to ensure the eqn number is vertically centered on the diagram

  • I'd use equation for the outer environment and gathered for the inner one. – egreg May 23 '13 at 13:29
  • True, but I don't think it would have made much of a difference in this case. – daleif May 23 '13 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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