GeneralSetting figure counter using \ref

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
greengrass
Posts: 12
Joined: Sun Oct 26, 2008 1:22 am

Setting figure counter using \ref

Post by greengrass »

Is it possible to obtain the figure number from its \ref?

For example. In chapter

Code: Select all

\ref{chap2}
I have a figure

Code: Select all

\ref{chap2_fig5}
which ends up being "Figure 2.5" in my document.

In a different chapter, I would like to have a figure, with the figure number corresponding to

Code: Select all

\ref{chap2_fig5}
i.e. I want it to also appear as "Figure 2.5".
I have done

Code: Select all

\setcounter{chapter}{\ref{chap2}}
but cannot figure out how to go about setting the figure counter.
Would it be something similar to

Code: Select all

\setcounter{figure}{\ref{chap2_fig5}} 
Any help would be greatly appreciated!!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10347
Joined: Mon Mar 10, 2008 9:44 pm

Re: Setting figure counter using \ref

Post by Stefan Kottwitz »

Hi,

I wouldn't repeat a figure and also the caption of the figure. Imagine you would to it several times, the order of the numbers wouldn't be ascending any more. I would refer to the figure in the other chapter instead of repeating, or, if the figure was changed, use a new number.

Though a straightforward solution would be to create a new counter (\newcounter), give it the value of the figure counter in chapter two and use the value of this new counter later.

Stefan
LaTeX.org admin
greengrass
Posts: 12
Joined: Sun Oct 26, 2008 1:22 am

Re: Setting figure counter using \ref

Post by greengrass »

Thanks for your input. I'm not quite sure how to go about using \newcounter in my case.

I actually don't need (or want) the order of the numbers to be sequential or ascending. All I want is for them to match (exactly) with previously defined figures. For example, it this special chapter, I want to be able to set the counter for chapter and figure (for each figure) so that the resulting figures may appear as "Figure 2.5", "Figure 2.8", "Figure 2.13". As mentioned, I can get the "2" by setting the chapter counter. I am just unsure of what to put in for the figure counter to get the 5, 8 and 13 in this example. Am I right that the value of \ref{chap2_fig5} is actually 2.5 and not just 5? if so, is there a command that would extract just the "5"?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10347
Joined: Mon Mar 10, 2008 9:44 pm

Re: Setting figure counter using \ref

Post by Stefan Kottwitz »

Could you use a different way than to manipulate counters? What's the point exactly? If needed in text or captions you could use \ref{chap2_fig5}, also in captions, just don't use the counter in the caption, use \ref{chap2_fig5} instead.

Stefan
LaTeX.org admin
greengrass
Posts: 12
Joined: Sun Oct 26, 2008 1:22 am

Setting figure counter using \ref

Post by greengrass »

Its for the caption. I'm puzzled - are you saying that I could set the figure caption within the \caption?
I currently have:

Code: Select all

\begin{figure}[htp]
\centering
\subfloat
{\includegraphics[width=.9\linewidth]{pic1}} \\
\subfloat
{\includegraphics[width=.9\linewidth]{pic2}} 
\caption[]{figure caption here}
\label{fig: blah}
\end{figure}
It will appear as "Figure X.X figure caption here". Isn't the first bit "Figure X.X" controlled by the chapter and figure counter, and can only be changed by changing the counters?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10347
Joined: Mon Mar 10, 2008 9:44 pm

Setting figure counter using \ref

Post by Stefan Kottwitz »

Here's a quick demonstration using the caption package:

Code: Select all

\usepackage{caption}
...
\begin{figure}[htp]
\centering
\subfloat
{\includegraphics[width=.9\linewidth]{pic1}} \\
\subfloat
{\includegraphics[width=.9\linewidth]{pic2}}
\caption*{\figurename~\ref{fig: blah}: figure caption here}
\end{figure}
See also the caption documentation.

Stefan
LaTeX.org admin
greengrass
Posts: 12
Joined: Sun Oct 26, 2008 1:22 am

Re: Setting figure counter using \ref

Post by greengrass »

Thank you so much!! That works and is so much better!! :P :P :P
greengrass
Posts: 12
Joined: Sun Oct 26, 2008 1:22 am

Setting figure counter using \ref

Post by greengrass »

While using the following works beautifully

Code: Select all

\caption*{\figurename~\ref{fig: blah}: figure caption here}
I realised that it does not allow a label to be stored. Is there any way to have both the self defined figure number as well as a label so that I could reference it with \pageref?

I tried the following

Code: Select all

\caption[]{\figurename~\ref{fig: blah}: figure caption here}\label{fig_label}
and while this enables \pageref{fig_label}, the resulting figure caption turns out to be "Figure X.X: Figure 2.5: figure caption here", where the 2.5 came from \ref{fig: blah} and the X.X is from the current chapter and figure counter. Is there a neat way to get rid of "Figure X.X: "?
Post Reply