5

I have just tried out the main=<language> package option to babel and found that it seemingly does not play nicely with hyperref's \autoref.

Using the traditional syntax works nicely:

\documentclass{scrartcl}
\usepackage[english,ngerman]{babel}
\usepackage{hyperref}
\begin{document}
\section{Test}
\label{sec:test}
\autoref{sec:test}
\end{document}

mwe1: working example

The main= version does not:

\documentclass{scrartcl}
\usepackage[english,main=ngerman]{babel}
\usepackage{hyperref}
\begin{document}
\section{Test}
\label{sec:test}
\autoref{sec:test}
\end{document}

mwe2: non-working version

Things that don't work to fix this

  • Using \setmainlanguage

Things that mitigate the problem (but seem hack-ish)

  • Using the “old” form (obviously)
  • Using a document option of the “old” form

What would be the best way to deal with this?

Edit: To be more precise, what can I do on the side of hyperref or the like to deal with this when I'm not able to change document options or babel's options?

4
  • Why not use one of the ways that works? Sorry, but you've given 2 perfectly good ways to deal with the issue. Why don't you want to use them? In general, it is probably a good idea to pass the language options to the class so that other packages pick up the settings if appropriate. [I think this might not be true for polyglossia but I don't remember. But you are not using that anyway.]
    – cfr
    Commented Jul 28, 2015 at 1:56
  • 1
    Why are either of those "hackish"? The babel documentation suggests them alongside the use of main= and even makes the point I made earlier: namely, that setting them as class options allows other packages to pick them up as well. Where does it say they are "old"?
    – cfr
    Commented Jul 28, 2015 at 2:24
  • You are right. I was under the impression that they should work the same. Also, I find that the package option depend on the order (while documented and sensible) is easy to forget or get wrong.
    – Tobias
    Commented Jul 28, 2015 at 6:28
  • Moreover, I don't want to blame babel here, I thought this is more a problem with hyperref
    – Tobias
    Commented Jul 28, 2015 at 6:29

1 Answer 1

2

The hyperref package just looks for language options without main=, but it should since now babel officially supports this.

Here is a hack, in the sense that the code (possibly made shorter) should go in hyperref.sty.

\documentclass{scrartcl}
\usepackage[english,main=ngerman]{babel}
\usepackage{hyperref}

\makeatletter
\def\HyLang@DeclareLang#1#2#3{%
  \@ifpackagewith{babel}{#1}{%
    \expandafter\HyLang@addto
        \csname extras#1\expandafter\endcsname
        \csname HyLang@#2\endcsname
    \begingroup
      \edef\x{\endgroup
        #3%
      }%
    \x
    \@namedef{HyLang@#1@done}{}%
  }{% added check for "main=#1" (presumably Heiko can do better)
    \@ifpackagewith{babel}{main=#1}{%
      \expandafter\HyLang@addto
        \csname extras#1\expandafter\endcsname
        \csname HyLang@#2\endcsname
      \begingroup
        \edef\x{\endgroup
          #3%
        }%
      \x
      \@namedef{HyLang@#1@done}{}%
     }{}%
  }%
  \begingroup
    \edef\x##1##2{%
      \noexpand\ifx##2\relax
        \errmessage{No definitions for language #2' found!}%
      \noexpand\fi
      \endgroup
      \noexpand\define@key{Hyp}{#1}[]{%
        \noexpand\@ifundefined{HyLang@#1@done}{%
          \noexpand\HyLang@addto{\noexpand##1}{\noexpand##2}%
          #3%
          \noexpand\@namedef{HyLang@#1@done}{}%
        }{}%
      }%
    }%
  \expandafter\x\csname extras#1\expandafter\endcsname
                \csname HyLang@#2\endcsname
}
% repeat the known language declarations
\HyLang@DeclareLang{english}{english}{}
\HyLang@DeclareLang{UKenglish}{english}{}
\HyLang@DeclareLang{british}{english}{}
\HyLang@DeclareLang{USenglish}{english}{}
\HyLang@DeclareLang{american}{english}{}
\HyLang@DeclareLang{german}{german}{}
\HyLang@DeclareLang{austrian}{german}{}
\HyLang@DeclareLang{ngerman}{german}{}
\HyLang@DeclareLang{naustrian}{german}{}
\HyLang@DeclareLang{russian}{russian}{\noexpand\hypersetup{unicode}}
\HyLang@DeclareLang{brazil}{portuges}{}
\HyLang@DeclareLang{brazilian}{portuges}{}
\HyLang@DeclareLang{portuguese}{portuges}{}
\HyLang@DeclareLang{spanish}{spanish}{}
\HyLang@DeclareLang{afrikaans}{afrikaans}{}
\HyLang@DeclareLang{french}{french}{}
\HyLang@DeclareLang{frenchb}{french}{}
\HyLang@DeclareLang{francais}{french}{}
\HyLang@DeclareLang{acadian}{french}{}
\HyLang@DeclareLang{canadien}{french}{}
\HyLang@DeclareLang{italian}{italian}{}
\HyLang@DeclareLang{magyar}{magyar}{}
\HyLang@DeclareLang{hungarian}{magyar}{}
\makeatother

\begin{document}
\section{Test}
\label{sec:test}
\autoref{sec:test}
\end{document}

enter image description here

0

You must log in to answer this question.

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