TransWikia.com

Adapting babel specific code for polyglossia in XeLaTeX

TeX - LaTeX Asked by LinusDieLinse on January 12, 2021

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}
addtoextrasngerman{
defsubsectionautorefname{Abschnitt} % subsection autoref -> Abschnitt ... 
defsubsubsectionautorefname{Abschnitt} % subsubsection autoref -> Abschnitt ... 
defpageautorefname{S.}} % Was vor der Seitenzahl in der Klammer der Referenz steht
letoldautorefautoref 
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{currentthecPage}%
 ifthenelse{equal{pageref{#1}}{pageref{currentthecPage}}}%
  {}{~(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}

addtoextrasngerman{
defsubsectionautorefname{Abschnitt} % subsection autoref -> Abschnitt ... 
defsubsubsectionautorefname{Abschnitt} % subsubsection autoref -> Abschnitt ... 
defpageautorefname{S.}} % Was vor der Seitenzahl in der Klammer der Referenz steht
letoldautorefautoref 
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{currentthecPage}%
 ifthenelse{equal{pageref{#1}}{pageref{currentthecPage}}}%
  {}{~(autopageref{#1})}}

begin{document}

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

end{document}

One Answer

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}


renewcommandsubsectionautorefname{Abschnitt} % subsection autoref -> Abschnitt ... 
renewcommandsubsubsectionautorefname{Abschnitt} % subsubsection autoref -> Abschnitt ... 
renewcommandpageautorefname{S.} % Was vor der Seitenzahl in der Klammer der Referenz steht

LetLtxMacrooldautorefautoref % NOT let

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

begin{document}

section{Einleitung}label{sec:intro}

newpage

Text. autoref{sec:intro}

end{document}

Doing letoldautorefautoref 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

Answered by egreg on January 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP