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 ?

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149

2 Answers2

6

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

Moriambar
  • 11,466
Qrrbrbirlbel
  • 119,821
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

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
daleif
  • 54,450