2

Using pdflatex and babel I have this code (taken from the answer to this question) to redefine the way \autoref from the hyperref package prints my references, it also adds automatically the respective page number of the referenced content:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{hyperref}
\addto\extrasngerman{
\def\subsectionautorefname{Abschnitt} % subsection autoref -> Abschnitt ... 
\def\subsubsectionautorefname{Abschnitt} % subsubsection autoref -> Abschnitt ... 
\def\pageautorefname{S.}} % Was vor der Seitenzahl in der Klammer der Referenz steht
\let\oldautoref\autoref 
\renewcommand{\autoref}[1]{\oldautoref{#1} (\autopageref{#1})} % autoref prints respective pagenumber

% No pagenumber if the reference is on the same page
\usepackage{ifthen}
\newcounter{cPage}
\renewcommand{\autoref}[1]{\oldautoref{#1}%
\refstepcounter{cPage}\label{current\thecPage}%
 \ifthenelse{\equal{\pageref{#1}}{\pageref{current\thecPage}}}%
  {}{~(\autopageref{#1})}}

\begin{document}

\section{Einleitung}\label{sec:intro}
\newpage
Text. \autoref{sec:intro}

\end{document}

Using XeLaTeX and therefore polyglossia instead of babel, how would I go about adapting this code to achieve the same output? Is it possible?

MWE (for XeLaTeX):

\documentclass{article}

\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage[]{german}

\usepackage[german]{hyperref}

\addto\extrasngerman{
\def\subsectionautorefname{Abschnitt} % subsection autoref -> Abschnitt ... 
\def\subsubsectionautorefname{Abschnitt} % subsubsection autoref -> Abschnitt ... 
\def\pageautorefname{S.}} % Was vor der Seitenzahl in der Klammer der Referenz steht
\let\oldautoref\autoref 
\renewcommand{\autoref}[1]{\oldautoref{#1} (\autopageref{#1})} % autoref druckt automatisch die zugehörige Seitenzahl

% No pagenumber if the reference is on the same page
\usepackage{ifthen}
\newcounter{cPage}
\renewcommand{\autoref}[1]{\oldautoref{#1}%
\refstepcounter{cPage}\label{current\thecPage}%
 \ifthenelse{\equal{\pageref{#1}}{\pageref{current\thecPage}}}%
  {}{~(\autopageref{#1})}}

\begin{document}

\section{Einleitung}\label{sec:intro}
\newpage
Text. \autoref{sec:intro}

\end{document}
3
  • 5
    "using xelatex and therefore polyglossia" is the wrong assumption. babel works fine with xelatex too. May 3, 2020 at 14:12
  • Ah thanks. Should I delete or edit this question? @UlrikeFischer May 3, 2020 at 14:16
  • 1
    @LinusDieLinse Well, it's up to you, but I think editing the question would be useful. I'm convinced many of the questions here about ‘how to do with polyglossia what can I do with babel’ are based on the assumption babel cannot be used with xelatex or lualatex. May 4, 2020 at 7:01

1 Answer 1

0

If you use just one language, there's no need to go the \extrasngerman way, even with babel.

\documentclass{article}

\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage{ifthen}
\usepackage{letltxmacro}

\usepackage[german]{hyperref}

\setmainlanguage[]{german}


\renewcommand\subsectionautorefname{Abschnitt} % subsection autoref -> Abschnitt ... 
\renewcommand\subsubsectionautorefname{Abschnitt} % subsubsection autoref -> Abschnitt ... 
\renewcommand\pageautorefname{S.} % Was vor der Seitenzahl in der Klammer der Referenz steht

\LetLtxMacro\oldautoref\autoref % NOT \let

% No pagenumber if the reference is on the same page
\newcounter{cPage}
\renewcommand{\autoref}[1]{%
  \oldautoref{#1}%
  \refstepcounter{cPage}\label{current\thecPage}%
  \ifthenelse{\equal{\pageref{#1}}{\pageref{current\thecPage}}}%
    {}
    {~(\autopageref{#1})}%
}

\begin{document}

\section{Einleitung}\label{sec:intro}

\newpage

Text. \autoref{sec:intro}

\end{document}

Doing \let\oldautoref\autoref is very risky, because \autoref is a “robusted” command. Use \LetLtxMacro instead as indicated.

I find it much better to declare all packages first, then do the settings. This helps in maintenance of the document.

I see no reason to do \renewcommand on \autoref twice, I only left the second declaration.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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