Sign up ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

For example:

% 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.


share|improve this question
\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

6 Answers 6

up vote 9 down vote accepted

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.

\protected@write \@auxout {}{\string \newlabel {#1}{{#2}{}}}}
Here is some text. \customlabel{foobar}{22}
Here is some more text \ref{foobar}.
share|improve this answer
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
up vote 11 down vote

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)

\protected@write \@auxout {}{\string \newlabel {#1}{{#2}{}}}}

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.

   \protected@write \@auxout {}{\string \newlabel {#1}{{#2}{\thepage}{#2}{#1}{}} }%

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

\hypertarget is added to make the links work when using \ref.

share|improve this answer
The second works well, but using \customlabel{refname}{displaytext} puts the displaytext also where \customlabel is called. Do you know how to get rid of that text? – barto Feb 4 at 21:52
its been a while but its in the \newcommand one of the # number is making that, you could remove one of them at the time to test out if you get what you want – Henrik Bøgelund Lavstsen Feb 27 at 11:26
@barto: The second argument to \hypertarget is the culprit; using \hypertarget{#1}{} seems to work perfectly. – Kundor Apr 22 at 16:26
@HenrikBøgelundLavstsen: Can you mention what the arguments to hyperref's redefined version of \newlabel are, or where this is documented? By the way, I am giving you a bounty for this answer. Thanks! – Kundor Apr 22 at 16:43
@Kundor Werner's answer at… is very much related to your problem. – Steven B. Segletes Apr 22 at 18:31

I guess the shortest solution is to use \def:


\def \lfoo {22 }

    In line \lfoo foo-bar. Furthermore, line \lfoo...

enter image description here

share|improve this answer




\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}

share|improve this answer
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

what about this:


another example - define custom labels for a new environment:

 \def\@currentlabel{{\thesection.\themycounter}}%  custom labelling for this environment
 %%... etc.,  other environment definitions here
share|improve this answer
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 '14 at 10:57

I am not sure I understand. I think this answer the question (no use of aux file)

Edit: as mentioned below by egreg this work only if \refis called after \createlabel

\expandafter\edef\csname #1\endcsname{#2}\@esphack}
\newcommand{\Ref}[1]{\csname #1\endcsname}
\createlabel{l-foo}{22} and leater \Ref{l-foo}
share|improve this answer
Then \Ref can only be used after the corresponding \createlabel command. – egreg Apr 22 at 17:47
@egreg as i said may be i don't undestand but it was clear for me that it will be call later no? – touhami Apr 22 at 17:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.