# How to include the number of a certain figure in the typeout command?

It quite often happens to me when working on a living document I mix up the order of the figures, e.g., the first figure that I discuss actually is figure number 3. I don't feel like browsing through a lengthy pdf file each time to check the order of the figures, therefore I'd like to implement something like the following in the LaTeX source file when I discuss a figure:

..in Figure~\ref{figure:someplot} it is shown that..
\typeout{reference to figure [number of figure {figure:someplot}]}


so that I can use grep on the logfile to check that the figures are referred to in the proper order.

In short, my question is: How to include the number of a certain figure in the typeout command?

I naively tried something like \the\ref{figure:someplot}, which didn't work of course...

-

## migrated from stackoverflow.comSep 26 '11 at 19:30

This question came from our site for professional and enthusiast programmers.

Welcome to TeX.sx! Your question was migrated here from StackOverflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Sep 26 '11 at 19:47

It is easiest to use the refcount package to access the number associated with a reference. Here is a minimal example showing this:

\documentclass{article}
\usepackage{refcount}% http://ctan.org/pkg/refcount
\begin{document}
\newcommand{\printfig}[1]{%
\typeout{reference to figure \getrefnumber{#1}}% Print information to .log file
}
\begin{figure}
\centering test 1
\caption{This is a test figure} \label{first-fig}
\end{figure}
\begin{figure}
\centering test 2
\caption{This is another test figure} \label{second-fig}
\end{figure}

See Figure~\ref{first-fig} \printfig{first-fig} and~\ref{second-fig} \printfig{second-fig}.
\end{document}​


This outputs the following in your log file:

...
reference to figure 1
reference to figure 2
...


Of course, this requires the use of \printfig whenever it is used. However, this can also be included in the use of the traditional \ref command, if needed.

-
Wouldn't it be easier \typeout{reference to figure \getrefnumber{#1}}? Setting a counter is not required. –  egreg Sep 26 '11 at 20:01
@egreg: That is so true! I was stuck on counters when I wrote the solution. I've updated my answer accordingly. –  Werner Sep 26 '11 at 20:05
Thanks, you guys are awesome! –  Ruud Dirksen Sep 26 '11 at 21:13

I programmed a little macro for you, but in the meantime Werner posted a good package based solution which might be better. Anyway, here the code:

\documentclass{article}

\usepackage{hyperref}

\makeatletter
\newcommand*\theref[1]{%
\expandafter\expandafter
\expandafter\theref@\csname r@#1\endcsname\@@nil
}
\def\theref@#1#2\@@nil{#1}%
\makeatother

\usepackage{lipsum}
\begin{document}

\lipsum

\begin{figure}
\rule{5cm}{5cm}%
\caption{Test}\label{A}
\end{figure}

\typeout{figure \theref{A}}

\lipsum

\begin{figure}
\rule{5cm}{5cm}%
\caption{Test}\label{B}
\end{figure}

\typeout{figure \theref{B}}

\lipsum

\begin{figure}
\rule{5cm}{5cm}%
\caption{Test}\label{C}
\end{figure}

\lipsum

\typeout{figure \theref{C}}

\end{document}

-