# Defining custom labels

Is it possible to define a directive which creates a custom label which I can later reference on multiple locations with \ref{...}?

For example:

...
\createlabel{l-foo}{22}
...
% later in document:
In line \ref{l-foo} foo-bar. Furthermore, line \ref{l-foo}...
...


Should get translated into:

In line 22 foo-bar. Furthermore, line 22...


Alternatively, is there a way to define a set of predefined values somewhere in the beginning of the document (it doesn't have to be the label mechanism) so that I can later use them in the text.

Thanks.

-
\label refers to the last \refstepcounter{<counter>}. If you use your own counter like that than it isn't a problem. Numbering every line is a different challenge. –  Martin Scharrer May 13 '11 at 21:45
I was using the lineno package to do line numbering, but defining labels based on line numbers within figure environments doesn't work there. I would settle with defining the line numbers for the labels manually in some way - just so that if something changes, I only have to update the label definitions, and not the refs too. If there is some other, non-label related way to do it, I would be satisfied with that as well. –  axel22 May 13 '11 at 21:50
You could use a counter which you set to your manual value - 1 and then use \refstepcounter{...} followed by a \label. This would cause proper references I think. Also have a look on the zref package. It provides a wide variety of referencing features. –  Martin Scharrer May 13 '11 at 21:56
Thanks a lot - this can help! –  axel22 May 13 '11 at 22:24

Do you really need to use \ref? You could just say \newcommand{\foobar}{22}, so that when you type \foobar you get 22. Of course this crude approach does not work if any references occur before the label is defined. To do it with \ref, try this.

\documentclass{minimal}
\makeatletter
\newcommand{\customlabel}[2]{%
\protected@write \@auxout {}{\string \newlabel {#1}{{#2}{}}}}
\makeatother
\begin{document}
Here is some text. \customlabel{foobar}{22}
Here is some more text \ref{foobar}.
\end{document}

-
Brilliant - not only does this work, but it also creates a label inside figure environments, which can be referenced outside of them. This means that I can take lineno's directive \LineNumber and use it to generate a line number within a figure - like this: \customlabel{labelname}{\LineNumber}. Thanks a lot! –  axel22 May 13 '11 at 22:22
It does not work for me. I get the following error Paragraph ended before \Hy@setref@link was complete. –  Gastón Jan 6 '12 at 3:49
@Mario --- Under what circumstances? The example works fine for me. –  Ian Thompson Jan 6 '12 at 17:21
@Ian Perhaps the reason is that I'm using KOMA... –  Gastón Jan 8 '12 at 4:59
@Mario --- I think it's the hyperref package. I do not know how to fix this. Ask a new question and someone will sort out the problem (or suggest an alternative solution). –  Ian Thompson Jan 8 '12 at 22:55

I found a solution for the custom label what works with hyperref, been looking everywhere and I keep ending back to this page. (By google searching)

Solution by Ian (does not work with hyperref)

\makeatletter
\newcommand{\customlabel}[2]{%
\protected@write \@auxout {}{\string \newlabel {#1}{{#2}{}}}}
\makeatother


The reason it does not work with hyperref, is that the package change the \newlabel command so it has 6 parameters.

Modified version of Ian's answer, that works with hyperref package.

\makeatletter
\newcommand{\customlabel}[2]{%
\protected@write \@auxout {}{\string \newlabel {#1}{{#2}{\thepage}{#2}{#1}{}} }%
\hypertarget{#1}{#2}
}
\makeatother


However doing this means that the modified version requires hyperref package.

-
\documentclass{article}

\usepackage{lineno}

\begin{document}

\linenumbers

\modulolinenumbers[255] % To switch off the showing of the numbers on margins

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur
dictum gravida mauris. \linelabel{line:Nam}Nam arcu libero,
nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula
augue eu neque. Pellentesque habitant morbi tristique senectus et
netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras
viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla
ultrices.  Phasellus eu tellus sit amet tortor gravida placerat.
Integer sapien est, iaculis in, pretium quis, viverra ac, nunc.
Praesent eget sem vel leo ultrices bibendum.  Aenean faucibus.  Morbi
dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla.  Curabitur
auctor semper nulla. Donec varius orci eget risus. Duis nibh mi,
congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit
amet orci dignissim rutrum

The sentence Nam arcu libero'' starts at line~\ref{line:Nam}

\end{document}

-
Thanks! I was aware of the \linelabel directive, unfortunately, it does not work within figure environments, even if you use \internallinenumbers in the beginning. –  axel22 May 13 '11 at 22:23

\newcommand{\mylabel}[2]{%
\makeatletter%
\def\@currentlabel{#2}%
\label{#1}%
\makeatother%
}


another example - define custom labels for a new environment:

\newenvironment{myenvironment}{%
\makeatletter%
\def\@currentlabel{{\thesection.\themycounter}}%  custom labelling for this environment
\makeatother%
%%... etc.,  other environment definitions here
}{%
%...
}

-
Welcome to TeX.SX! Could you please demonstrate how your solution works? You can maybe cook up a minimal working example (MWE), that is the recommended way. –  Claudio Fiandrino Nov 19 at 10:57