[Tex/LaTex] Figures in appendix are not referenced correctly (or not the way I want them to :) )

appendicescross-referencing

In my document, I have some figures in my appendix I want to reference in my text but the output of \ref{} is only "Figure .2 shows…". It should be something like "Figure B.2".

Here's my minimal example:

\documentclass[bibliography=totoc,index=totoc,numbers=noenddot]{scrbook}
\usepackage[demo]{graphicx}
\begin{document}

\frontmatter
    \include{Titel}
    \tableofcontents

\mainmatter
    Here is my text \ref{fig:XYZ}

\backmatter
\appendix
\renewcommand\thesection{\Alph{section}}
\chapter{Appendix}
\section{UML-Diagrams}
\begin{figure}[h]
  \begin{center}
    \includegraphics[width=15cm]{xyz.jpg}
    \caption{\label{fig:XYZ}XYZ}
  \end{center}
\end{figure}

\end{document}

Best Answer

You also need to redefine \thefigure to your own format. By default scrbook removes the chapter number from it outside the main matter. I assume now you actually want the section number in it.

Also note that you should use \centering not the center environment in floats like figure. Then {figure}[h] is never a good idea. If you don't want to make the figure float use an own environment (like center which now is OK) instead and use \captionof{figure}{...} for the caption (needs either the caption (big) or capt-of (small) package). Also, please put the \label after \caption, not inside it. It works both ways but this is better.

\documentclass[bibliography=totoc,index=totoc,numbers=noenddot]{scrbook}
\usepackage[draft]{graphicx}
\usepackage{capt-of}
\begin{document}

\mainmatter
Here is my text \ref{fig:XYZ}

Here is my text \ref{fig:XYZ2}

\backmatter
\appendix
\renewcommand\thesection{\Alph{section}}
\renewcommand{\thefigure}{\thesection.\arabic{figure}}
\chapter{Appendix}
\section{UML-Diagrams}

\begin{center}
    \includegraphics[width=15cm]{xyz.jpg}
    \captionof{figure}{XYZ}\label{fig:XYZ}
\end{center}

\begin{figure}
    \centering
    \includegraphics[width=15cm]{xyz.jpg}
    \caption{XYZ2}\label{fig:XYZ2}
\end{figure}

\end{document}
Related Question