4

I would like to have references that automatically print the number of the page on which the referenced section(heading)/table/figure... happens to be in parentheses, additionally to the rest of the reference. The output should look like this for example:

As we can see in \autoref{table}, ... -> As we can see in Table 3 (S.15), ...

"S" stands for "Seite", "page" in german, the greatest solution would involve that the "S." automatically appears because of the babel package option being ngerman, therefore allowing this to work for every language. I'm using the hyperref package already because of \autoref. Here's a minimal working example:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{blindtext}
\usepackage{hyperref}

\begin{document}

\section{Section}
\label{Section}

\blindtext

\newpage
\section{Another Section}

As mentioned in \autoref{Section}, ...

\end{document}

1 Answer 1

6

You can redefine the \autoref to include an \autopageref. Since ngerman defines page to be Seite and not S. you need to redefine it and add it to babel in the regular way:

\documentclass{article}
\usepackage[ngerman]{babel}

\usepackage{blindtext}
\usepackage{hyperref}
\addto\extrasngerman{%
\def\pageautorefname{S.~}}
\let\oldautoref\autoref
\renewcommand{\autoref}[1]{\oldautoref{#1} (\autopageref{#1})}
\begin{document}

\section{Section}
\label{Section}
\begin{table}
\caption{A table\label{Table}}
\end{table}

\blindtext

\newpage
\section{Another Section}

As mentioned in \autoref{Section} and in \autoref{Table} ...

\end{document}

output of code

Avoiding same page references

Since adding the page number to reference on the same page as the referred item is a bit annoying, you can use code adapted from this answer to make the page reference only appear if the referred element is not on the current page:

\usepackage{ifthen}
\newcounter{cPage}

\renewcommand{\autoref}[1]{\oldautoref{#1}%
\refstepcounter{cPage}\label{current\thecPage}%
 \ifthenelse{\equal{\pageref{#1}}{\pageref{current\thecPage}}}%
  {}{~(\autopageref{#1})}}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .