[Tex/LaTex] Placing ref/pageref target labels inside a section

cross-referencing

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.

Best Answer

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.

Related Question