Reading manuals I always find figure labeled as \label{fig:name}
. I wonder where is the advantage of using this notation with fig:
instead to start with the name it self. If in my document I have only figures when I \ref
something using the label without fig:
is more quick because I don't have to type fig:
before to find the right figure that I want. Is there any advantage I'm missing? Should I use fig:
?
4 Answers
Here are at least two reasons:
It expresses the meaning of the label in a very sensible way, in all the below cases the intent of the label is straightforward and it will also let you group things that have meaning to each other,
fig:system
andeq:system
have the meaning of system and one is a figure, the other an equation.fig:
for figurestbl:
/tabl:
for tableseq:
for equationschap:
/chp:
for chapterssec:
for sections
It can typically be parsed by your editor to easier lookup only figure/table/equation labels. In the case of emacs there is RefTeX which eases the cross-reference system. Several other editors also have such instances of cross-reference system.
In addition to the reasons given by others (intelligibility of source; leveraging editor capabilities etc.), if you use fancyref
, you can automate more descriptive cross-referencing. This makes it easy to ensure that your cross-references are consistently formatted throughout the document. The system can be adapted to use your own preferred prefixes and labels, and can be extended to cover cross-references to other elements of your document. It can handle both plain and vario referencing and offers upper and lower case referencing commands out of the box.
\documentclass{article}
\usepackage{fancyref}
\begin{document}
\section{A section}\label{sec:mysec}
\begin{figure}
\centering \fbox{A figure}
\caption{My figure}\label{fig:myfig}
\end{figure}
\Fref{sec:myothersec} will demonstrate that \fref{fig:myfig} is fantastic.
\section{Another section}\label{sec:myothersec}
\Fref{fig:myfig} showed whatever my figure showed in \fref{sec:mysec}.
\end{document}
-
24
-
2cleveref looks really nice. Not so much because of the automated identification (though it does rather undercut my answer here!) but because it promises to automatically compress multiple references which is something I've wished fancyref could do. I will definitely have to take a look. I'm guessing you are the author? Thanks! (Though I guess I would lose my current config which is definitely a disincentive!)– cfrJan 3, 2014 at 2:01
-
-
1Ah, well. Thanks for the pointer in any case. I notice now that he spells his name with a y not an i.– cfrJan 3, 2014 at 2:06
-
Personally I have never used such a convention (and LaTeX doesn't assume any structure to the labels) the label string is really just an opaque internal identifier. If you find it convenient to give a common prefix to labels of similar objects then do so, but it is certainly not a requirement of the system. Some packages and/or editors may try to parse the structure of the labels and look for fig:
etc, but probably they shouldn't.
I'm currently writing some lecture notes for "Geometry and Topology" (in German). With that project I've learned why namespaces for labels make sense.
Avoid double labels: I have had problems without prefixes like fig:
and sec:
or bsp:
(for examples) or thm:
, because of double labels. Quite often, I have had a figure I want to reference and a theorem. Namespaces solve that problem nicely.
Make code easier to read
Avoid mix-ups: Sometimes, I wanted to reference sections. So I was sure the section had a label, e.g. \label{lifting}
. But in fact, that label was chosen for a figure. This would not have happened when I had prefixes. With prefixes, LaTeX would produce some ??
and make it obvious that I made an error.
After I have seen the screenshot from above, I think there is another advantage for you:
Autocompletion: I guess most of the time you know if you want to reference a figure, a section, a table or whatever. This way, you reduce the number of elements you actually need to think about quite a lot. (By the way, what editor do you use? I still type my LaTeX with gEdit / pluma mot of the time ... maybe you have a better alternative?)
Cleveref and Fancref: There are some reasons why you should use \usepackage{cleveref}
(see long answer). With that, you can do something like
\usepackage[nameinlink]{cleveref}
...
As you can see in \cref{sec:abc} ...
which might translate to
As you can see in section 7
where "section 7" is linked. Additionally, you can pass language options to cleveref
which might make translation easier.
So there are some important reasons for using your own convention for label-namespaces.
Collaboration is the only reason for using a "well known namespace scheme" like fig:
for figures and sec:
for sections.
However, LaTeX doesn't care about label namespaces. They are just a part of the name of the label.
fig:
. But in other cases, it is very recommended since you can quickly find the figure labels or table labels or section labels and so on...f
is the same thing...fig=
instead offig:
because I've had issues with packages making:
active, but other than that it is a convenient namespace convention.