[Tex/LaTex] Cross reference formatting options

cross-referencingformatting

I have got a quick question; I am writing a document with lots of sections and figures. Cross referencing works fine. So far so good. However, I would prefer when I reference something to have the figures and the sections in different formats.

For instance if I reference a section (provided tha appropriate labels are in place) I would like the output to be: Section 4.3

and when I reference a figure to have: Figure 4-3.

How should I go about it?

Thank you in advance

Best Answer

You can redefine \p@figure to produce the desired formatting (but this will only change the formatting of the string used for cross-references; the string used for captions will remain unaltered):

\documentclass{book}

\makeatletter
\renewcommand\p@figure{\thechapter-\arabic{figure}\expandafter\@gobble}
\makeatother

\begin{document}

\chapter{Test Chapter}
See figure~\ref{fig:test} in section~\ref{sec:test}...

\section{test}\label{sec:test}
\begin{figure}[!ht]
\caption{Test Figure}
\label{fig:test}
\end{figure}

\end{document}

enter image description here

And combining this with the cleveref package, you can say something like this:

\documentclass{book}
\usepackage{cleveref}

\makeatletter
\renewcommand\p@figure{\thechapter-\arabic{figure}\expandafter\@gobble}
\makeatother

\crefname{figure}{figure}{figures}
\Crefname{figure}{Figure}{Figures}

\begin{document}

\chapter{Test Chapter}
See \cref{fig:test} in \cref{sec:test}...

\section{test}\label{sec:test}
\begin{figure}[!ht]
\caption{Test Figure}
\label{fig:test}
\end{figure}

\end{document}

enter image description here

Remarks:

  1. I only used [!ht] as placement specifier just for this example; I am not recommending its usage.
  2. As a personal opinion, this style introduces some level of inconsistency since we are referencing object x.y using a different string x-y. I would suggest you to reconsider this change.

For consistency's sake, I would prefer to change \thefigure so the strings used in the object numbering and the one used in the cross-reference will be the same:

\documentclass{book}
\usepackage{cleveref}

\makeatletter
\renewcommand\thefigure{\mbox{\thechapter-\arabic{figure}}}
\makeatother

\crefname{figure}{figure}{figures}
\Crefname{figure}{Figure}{Figures}

\begin{document}

\chapter{Test Chapter}
See \cref{fig:test} in \cref{sec:test}...

\section{test}\label{sec:test}
\begin{figure}[!ht]
\caption{Test Figure}
\label{fig:test}
\end{figure}

\end{document}

enter image description here