Take the 2-minute tour ×
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.

i was idly wondering instead of having see Section \ref{labelname} produces

see Section 4.1.1

i could make it so that see Section \ref{labelname} produces

see Section 4.1.1 (p65)

is this possible? Would using hyperref mess this up?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

You can define a command, like \myref

\newcommand{\myref}[1]{\ref{#1} (p\pageref{#1})}

Then you can use \myref{xxx}

share|improve this answer
    
thanks, that works good, i cant seem to get it replace the \ref command with this though. –  GreyCloud Aug 21 '12 at 10:12
1  
You can use \let\oldref=\ref \renewcommand{\ref}[1]{\oldref{#1} (p\pageref{#1})}. It's simple yet maybe not very robust. –  goodluck Aug 21 '12 at 11:50
    
Heiko Oberdiek's answer should be more robust. –  goodluck Aug 21 '12 at 11:56
add comment

The cleaner way would be a new macro like in the answer of goodluck. But also \ref can be redefined. It should be done as late as possible, because many packages redefines it. In the solution it is done in \AtBeginDocument at the end of the preamble, especially after package hyperref is loaded.

Also package hyperref supports a star form that generates the reference without the link. For example, this is useful if the reference is part of another reference to avoid nested links.

\documentclass{article}
\usepackage{hyperref}

\makeatletter
\AtBeginDocument{%
  \newcommand*{\original@ref}{}%
  \let\original@ref\ref
  \@ifpackageloaded{hyperref}{%
    \renewcommand*{\ref}{%
      \@ifstar\newrefstar\newref
    }%
    \newcommand*{\newrefstar}[1]{%
      \original@ref*{#1} \textbf{(p\pageref*{#1})}%
    }%
    \newcommand*{\newref}[1]{%
      \hyperref[#1]{\newrefstar{#1}}%
    }%
  }{%
    \renewcommand*{\ref}[1]{%
      \original@ref{#1} \textbf{(p\pageref{#1})}%
    }%
  }%
}
\makeatother

\begin{document}
\section{Introduction}
\section{Foo bar}
\subsection{Hello World}
\label{sec:hello}
This is subsection \ref{sec:hello}.
\end{document}

Reference link with page

share|improve this answer
add comment

Maybe you would like to take a look at the varioref package with its command \vref. This will generate your demanded output. In addition, this does not have an effect on your hyperref settings.

share|improve this answer
add comment

Your Answer

 
discard

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.