# Placing ref/pageref target labels inside a section

Is it somehow possible to place a "virtual label" that will behave just like the most recent "structural label" when used with \ref but will still produce the page number where the label is actually placed when used with \pageref?

Consider the following example:

\documentclass{article}
\usepackage{lipsum}

\begin{document}

\section{First Section}
\label{first}

This is my first section. Let me add enough text to fill up more than one page here.
\lipsum[1-6]

Fine, so we've written quite a lot of text. Now let's talk about \textbf{some important topic} --
\lipsum[7-10]

\section{Second Section}

This is where things get interesting. In this section, I would like to discuss something
entirely different, which is nonetheless related to \textbf{some important topic} that I
introduced earlier in section \ref{first} on page \pageref{first}.

\end{document}


For some reason, the first section spans more than one page and also covers more than one topic. At a later point, I would like to add a reference back to that topic just in case the reader wants to look it up again. The reference generated with \ref is entirely correct -- the important topic is indeed covered in the first section. However, the page reference is not correct - it points to the first page the section appears on, which is page 1. In this case, the important topic is only mentioned on page 2, so that the reader will have to sift through parts of the section that are irrelevant.

I would like to improve the behavior so that the page number points to the place where the actual topic is discussed. The obvious solution might be to split the text up into different \subsections, but in the real document, that's not an option: The document is already huge and very thoroughly structured, and in the introduction I have lots of definitions that I want to be able to refer to but that only span one or two paragraphs each. Adding a subdivision for each definition would make the introduction very hard to read.

I would also like to be able to keep the same label for the \ref and \pageref command because I use the fancyref package. And finally, the referrable (is that a word?) \labelss may occur at different depths inside the structure -- sometimes I need to refer to an entire \part, sometimes just to a \subsubsection.

## 3 Answers

Use another label for the pageref of the important fact:

\documentclass{article}
\usepackage{lipsum}
\makeatletter
\def\magicref#1{\expandafter\magicref@i#1\@nil}
\def\magicref@i#1::#2\@nil{section \ref{#1} on page \pageref{#1::#2}}
\makeatother
\begin{document}

\section{First Section}
\label{first}

This is my first section. Let me add enough text to fill up more than one page here.
\lipsum[1-6]

Fine, so we've written quite a lot of text. Now let's talk about
\textbf{some important topic}\label{first::important} --  %%%%%%%%%%%%%%%
\lipsum[7-10]

\section{Second Section}

This is where things get interesting. In this section, I would like to discuss something
entirely different, which is nonetheless related to \textbf{some important topic} that I
introduced earlier (see \magicref{first::important}).

\end{document}


\ref{first} and \ref{first:important} are always the same. but \pageref can be different. \magricrefis split into a \ref and \pageref.

• \ref{first} and \ref{first:important} may not give the same result if a \subsection comes in between them, or the latter \label appears in an enumerated list. Just to be picky, this is what I'd do anyway. – egreg Jul 13 '14 at 9:52
• That's obvious! – user2478 Jul 13 '14 at 10:10
• This is what I'd do, too. If I expected to do this a lot, I might define the 'body labels' with something like \label{p:important} to remind myself that this labels is intended to be used with \pageref only. I'd be surprised if there were any alternative to this method. – Norman Gray Jul 13 '14 at 16:24
• Thank you, @Herbert. Just curious: Would it be possible to use something like \label{first} and \label{first::important} and then create some kind of macro like \magicref{first::important} that would split the argument at a predefined delimiter - in this case :: - and create an output similar to fancyref's \fref, something like section \ref{first} on page \pageref{first::important}? After my recent struggles with seemingly simple string processing in macro arguments, I'm not sure whether it's worth the effort... – vwegert Jul 15 '14 at 12:47
• @vwegert: See my edited answer – user2478 Jul 15 '14 at 14:13

Why can't you simply use a lable after your important topic?

i.e.

\documentclass{article}
\usepackage{lipsum}

\begin{document}

\section{First Section}
\label{first}

This is my first section. Let me add enough text to fill up more than one page here.
\lipsum[1-6]

Fine, so we've written quite a lot of text. Now let's talk about
\textbf{some important topic}\label{important} --
\lipsum[7-10]

\section{Second Section}

This is where things get interesting. In this section, I would like to discuss something
entirely different, which is nonetheless related to \textbf{some important topic} that
I introduced earlier in section \ref{important} on page \pageref{important}.

\end{document}


I'm a little late (well, I had this issue, and the above did not work for my particular document setup), but since the page reference went to (my personal paper has internal hyperlinks) the first page of the section, I thought, after reading that the original poster mentioned "virtual label", why don't I try \phantomsection from the hyperref package, as I am using that package anyway.

That is, I used the hyperref package and placed "\phantomsection" to the left of the \label{important} tag, both being to the left of important topic.

In addition, I set the parameters colorlinks=true and allcolors=black so that the output document looks like the desired output from the original poster. Of course the only difference is that clicking on the page number brings you directly to the important topic.

\documentclass{article}
\usepackage{lipsum}

\usepackage{hyperref}
\hypersetup{
colorlinks=true,
allcolors=black,
pdfencoding=auto,
psdextra
}

\begin{document}

\section{First Section}
\label{first}

This is my first section. Let me add enough text to fill up more than one page here.
\lipsum[1-6]

Fine, so we've written quite a lot of text. Now let's talk about \phantomsection\label{important}\textbf{some important topic} --
\lipsum[7-10]

\section{Second Section}

This is where things get interesting. In this section, I would like to discuss something
entirely different, which is nonetheless related to \textbf{some important topic} that I
introduced earlier in section \ref{first} on page \pageref{important}.
\end{document}