5

Can I global change all figures and references to figures up one starting from figure 10?

So figure 10 and all references to 10 become 11 and so on?

I should probably add that I have about 30 figures and added a figure right above figure 9 displacing my current count. That is why I need to re-label all figures and references to the figures.

Current figure labeling:

\label{fig:fig1}, \label{fig:fig2}, and so on

lockstep
  • 250,273
dustin
  • 18,617
  • 23
  • 99
  • 204
  • 1
    If I understand what you mean, a simple \stepcounter{figure} just before the figure in question should do. But I am not sure this is what you are trying to do; why would you want no figure 10? – Corentin May 29 '13 at 21:25
  • @Corentin I have a figure 10. I just added it so all the figures are off now. – dustin May 29 '13 at 21:26
  • @egreg This will replace all my current numbering? – dustin May 29 '13 at 21:27
  • 4
    @dustin Just compile again a couple of times and the numbering will adjust itself. LaTeX numbers figures automatically. – egreg May 29 '13 at 21:29
  • @egreg the numbering on the pdf will be correct then but not in the latex document then I take it? – dustin May 29 '13 at 21:31
  • 2
    Do you use the number as part of the label? If you do that, better stop it and use a descriptive name inside of the label, because otherwise you get the described problem. If that should be the case, re-label them once and for all and use names without numbering. – Patrick Häcker May 29 '13 at 21:31
  • @dustin I can't understand. Can you show an example of your figures? – egreg May 29 '13 at 21:32
  • @MMM I will show in OP how I am labeling. Can you then tell me how I should adjust it? – dustin May 29 '13 at 21:33
  • @egreg I added how I label my figures in the OP – dustin May 29 '13 at 21:34
  • 3
    Labels are arbitrary strings; even if you use \label{fig:fig911992}, the number it refers to will be the one automatically assigned. It's better to use significant names, such as \label{fig:duck} if the image represents a duck, rather than hard coding numbers that are bound to change. – egreg May 29 '13 at 21:36
  • 3
    You should use something like the following: \label{fig:sketchOfAHouse}, \label{fig:racingCar}, \label{fig:distanceVelocityPlot}. The exact style may differ, but the idea is to have a short label which somehow describes the figure (for you), so you can move it anywhere and when you read the label, you instantly know which figure is meant. Do not use numbers in the labels. The only exception is, if it is descriptive as in \label{fig:my5PairsOfShoes}. – Patrick Häcker May 29 '13 at 21:38
  • @MMM Would you like to write an answer? – egreg May 29 '13 at 21:39
  • @egreg, I have the feeling, that this is written somewhere already and probably much better than I would write it. Do you know a link to some source? Unfortunately I have to leave now, so it's ok if you or someone else answers. – Patrick Häcker May 29 '13 at 21:45
  • I wonder if one can automate the labelling with \includegraphics[]{figure-file-name} \caption{Figure File Name} \label{fig:figure-file-name}, Note: figure-file-name.pdf and label key figure-file-name are same when figure file names match keys while reading fig file names from figures folder. – texenthusiast May 29 '13 at 21:48
  • @texenthusiast I had a similar idea regarding the labeling, but not as complete as yours. As the label and the file name both should be descriptive, it would indeed be logical to keep them synchronized. Obviously, there are some limitation, as a colon, for example, does not work in a file name under windows, but a sane operating system should not have problems supporting LaTeX's character sets. LaTeX's file loading routine might be more problematic with exotic characters (spaces?). Another problem: This breaks if a figure is included more than once in a document. But these seem rather exotic. – Patrick Häcker May 30 '13 at 09:37

2 Answers2

10

Labeling figures or other elements by their number is not recommended. A label is just an arbitrary string that should be memorable or at least meaningful and independent of any particular number the object to reference is assigned.

You have incurred in a problem that is quite common: a document is finished only when it's complete. A tautology? Perhaps, but until a document is complete you can't know what number a section or a figure will be assigned, because you or your supervisor or the editor might request splitting a section into two, unifying two sections, moving a figure after another one; the list of possibilities is endless.

Thus if your section is about Pythagoras' theorem, it's better to label it with a a string that reminds you what the section is about:

\section{Pythagoras' theorem}\label{sec:pythagoras}

You'll be able to reference it with \ref{sec:pythagoras} and the right number will appear. No knowledge of the actual number is required.

It's the same for figures:

\begin{figure}
\centering

\includegraphics{duck}

\caption{Picture of a duck}\label{fig:duck}
\end{figure}

For getting the figure number, just type \ref{fig:duck}.

When writing the document, loading a package such as showkeys is very helpful, because it will show the labels next to the element they refer to, so it's easy to pick up the right name when referencing.


How can you solve your particular problem? Just load the showkeys package and look up the names you've already got when referencing. Add \label{fig:new} or anything you like for the new figure, leaving the other untouched. The next figure will still have \label{fig:fig10}, but with

\ref{fig:fig10}

you'll get the correct number 11, without any other intervention. Just remember it when referencing.

Caveat For the final version of the document, remove the loading of showkeys.

egreg
  • 1,121,712
  • So I relabelled figures but now I am an encounter figure.number has been already used. I also use the figure environment for figures that aren't captioned. So these figures are begin numbered but not used. Will this be causing the problem? – dustin May 29 '13 at 22:28
  • @dustin How did you call the new label? – egreg May 29 '13 at 22:30
  • @dustin I've reworded the part where I talk about the workaround for your problem. Don't give \label{fig:fig10} to the newly inserted figure, but another name (whatever you want). Leave the other labels untouched; numbering and references will adjust automatically. – egreg May 29 '13 at 22:33
  • I already changed the labels to descriptions but I am being told identifiers have already been used. The only thing I could think that would cause this would be the figures I am using that don't have captions or labels. – dustin May 29 '13 at 22:36
  • @dustin That's a problem with hyperref that should disappear at the next LaTeX run. But uncaptioned figures might give issues; why shouldn't they have a caption? – egreg May 29 '13 at 22:36
  • Just checked every label and they are all different. – dustin May 29 '13 at 22:39
  • @dustin I can't reproduce the problem – egreg May 29 '13 at 22:45
  • I see no problem – egreg May 29 '13 at 22:49
  • I change that from the figure environment to the \begin{center}\end{center} and the problem went away. – dustin May 29 '13 at 22:55
3

Based on an idea from @texenthusiast, it's also possible to use the figure's file name as label. As both should be descriptive anyway, it's quite logical to keep them synchronized. This could be the code:

\documentclass{article}

\usepackage{graphicx}
\usepackage{letltxmacro}

% Save the figure's name in a macro to use the name at the end of the figure environment.
% This is needed, as the label has to be given after the caption command and directly before
% the end of the figure environment is the only place where this is guaranteed.
\LetLtxMacro{\includegraphicsOld}{\includegraphics}
\renewcommand{\includegraphics}[2][]{%
    \includegraphicsOld[#1]{#2}%
    \def\currentFigureName{#2}%
}
\let\endfigureOld\endfigure
\renewcommand{\endfigure}{%
    \label{fig:\currentFigureName}%
    \endfigureOld
}

\begin{document}
    \begin{figure}
        \includegraphics{mtsbwy}
        \caption{Description of my figure}
    \end{figure}
    This is my figure \ref{fig:mtsbwy}.
\end{document}

The reference is correct, although no label has been set explicitly. Note, that a label could have been set and used manually, too, so nothing breaks if that were enabled by default.

The added functionality might break for the first label if a figure is used a second time in one document. But this seems to be rather exotic and could easily be fixed by saving all figure names in global macros. Then there would be a check to see if the figure has already been loaded and a new label could be choosen (fig:mtsbwy2 or something like that, maybe even recursive).

I could imagine a package, which incorporates this code, probably also with support for the subfloat commands, if they are loaded. Would that be a good idea, or are there reasons to better not do that? I am looking forward to comments.