1

Is there any way to make Figures-label with automatically incremented counter. I have this code:

\documentclass[10pt,a4paper,openany]{book}
\usepackage{graphicx}
\begin{document}

\begin{figure} \includegraphics{natural-number-line-1.pdf} \caption{image-Numbe-1} \label{fig:f1.1} \end{figure} \ref{fig:f1.1}\ %%%%%%%%%%%%%%%% \begin{figure} \includegraphics{natural-number-line-2.pdf} \caption{image-Numbe-2} \label{fig:f1.2} \end{figure} \ref{fig:f1.2}\ %%%%%%%%%%%%%%%% \begin{figure} \includegraphics{natural-number-line-3.pdf} \caption{image-Numbe-3} \label{fig:f1.3} \end{figure} \ref{fig:f1.3}\ %%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%% \end{document}

when i insert a new image and reference it, so i must change all the labels after it, like that:

\documentclass[10pt,a4paper,openany]{book}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\includegraphics{newimage.pdf}
\caption{newimage}
\label{fig:f1.1}
\end{figure}
\ref{fig:f1.1}\\
%%%%%%%%%%%%%%%%
\begin{figure}
\includegraphics{natural-number-line-1.pdf}
\caption{image-Numbe-1}
\label{fig:f1.2}
\end{figure}
\ref{fig:f1.2}\\
%%%%%%%%%%%%%%%%
\begin{figure}
\includegraphics{natural-number-line-2.pdf}
\caption{image-Numbe-2}
\label{fig:f1.3}
\end{figure}
\ref{fig:f1.3}\\
%%%%%%%%%%%%%%%%
\begin{figure}
\includegraphics{natural-number-line-3.pdf}
\caption{image-Numbe-3}
\label{fig:f1.4}
\end{figure}
\ref{fig:f1.4}\\
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
\end{document}

I have more than 1000 figures, and my question is: Is there any way to make label with automatically incremented counter, so i can insert and reference figures in any place without change all the labels of figures after that?

AAA
  • 81
  • 2
    The argument of \label is just an arbitrary name. It has nothing to do with the figure number you are getting. Thus, use descriptive labels (such as fig:introductory_figureor fig:scope,...) rather than labels that include chapter/figure numbers. – leandriis Sep 30 '20 at 18:28
  • 3
    The whole purpose of the \label, \ref mechanism is that figures, tables,... are automatically numbered and referenced correctly, even if you change their order. Therefore, don't use labels that just make sense if your images stay in a spcific order. Probably you could use the filename as the label? – leandriis Sep 30 '20 at 18:36
  • Ok, How can i insert a new figures and reference it, without change all the labels after the new inserted figure,so i get the right order of the figures? Must i use counter and increment it, If yes, how can i do that? – AAA Sep 30 '20 at 18:41
  • 1
    Just make sure you have unique labels. The number you see in the PDF is completely unrelated to the text used in the labels. The figures are numbered in the order they appear in the document, if you add a new figure at the start, the numbering of the figures, and the cross-references, will update. – Torbjørn T. Sep 30 '20 at 18:47
  • use unique descriptive labels(text) for 1000 figures is very difficult,so man must use numbers as label. The problem appears when inserting new figures and reference it, which will not be in the right order – AAA Sep 30 '20 at 18:59
  • I can sort of see that I guess, but I don't really understand what you mean at the end there. What is not in the right order, and what exactly do you mean by "right order" in the first place? – Torbjørn T. Sep 30 '20 at 19:09
  • 1
    Probably take a look at this answer: https://tex.stackexchange.com/a/123511/134144 It shows a way to automatically use the file name as the label name as well. Hence no need to invent 1000 labels and no order confusion if you add images later on since the labels are not tied to the figure numbers. – leandriis Sep 30 '20 at 19:52
  • Very many thanks, i have first thought that you mean with "filename" the tex file name, but you mean the image file name, many thanks. But i still have the idea that it is possible to use step counter as figure-label, i have made it, but it gives me the last counter value in the pdf file!!! – AAA Sep 30 '20 at 20:21
  • 1
    the entire reason for having the \label mechanism is so that you do not need to adjust the numbering if you add or insert figures (or sections) it is normally considered bad style but if you want to use an incremental number to generate unique references that will work fine, but there is no relation between that internal numbered id used for \label and the actual printed figure number, so there is no need to change the labels or references when you re-order the figures. Auto-generating the label defeats the process as then you have no way to have a stable id to use with \ref. – David Carlisle Sep 30 '20 at 22:02
  • 1
    I suspect this is a duplicate of https://tex.stackexchange.com/questions/341948/a-good-strategy-for-labeling-sections-equations-figures-or-tables (but haven't voted to close yet) – David Carlisle Sep 30 '20 at 22:19
  • Automatic labels have not any sense, because in the text you must \ref{} to know and constant unique labels. If you have 40 figures and you remove the third figure, and the figure fig:1.4 is relabelled automatily to fig:1.3, what happend with already made reference to later figures (from \ref{fig:1.4} to \ref{fig:1.40})? Now all this reference will point to the wrong figure. What now? Manually change all the references? – Fran Oct 04 '20 at 14:41

0 Answers0