latex3 / hyperref

Hypertext support for LaTeX
165 stars 35 forks source link

hyperref overrides definition of \oe, \th and probably many other #233

Closed Maelan closed 2 years ago

Maelan commented 2 years ago

hyperref apparently defines or overrides commands such as \OE, \oe, \TH, \th, \DH, \dh, and I guess many other. I think it is an unexpected side effect and that hyperref shouldn’t do it, because (unless I’m mistaken) it sounds independent of its purpose. It’s especially annoying given that hyperref is supposed to be loaded last.

Minimal example:

\documentclass{minimal}
\usepackage[T1]{fontenc}

% Let's redefine our own \th command
% (note: "th" is an alternative spelling of "tanh" for the hyperbolic tangent function):
\let\th\relax
\newcommand*\th{\textrm{th}}

% Let's load hyperref:
\usepackage[implicit=false]{hyperref}
% the problem shows up even with implicit=false

\begin{document}
\th in text, and $\th$ in math
\end{document}

The definition of \th that gets picked in this document is seemingly the initial (La?)TeX definition (i.e. this example produces a þ (the Icelandic letter “thorn”) in text mode and whatever in math mode). Of course, redefining \th after loading hyperref gives the intended behavior of using our custom definition.

Perhaps there is a technical reason for that (for getting the right Unicode character in the PDF?), but if it’s still needed as of 2022, could hyperref at least allow to disable the overriding selectively? Something resembling:

% ...

% Let's redefine our own \th command:
\let\th\relax
\newcommand*\th{\textrm{th}}
\newcommand*\@pleasedontredefine@th{true}

% Let's load hyperref:
\usepackage{hyperref}

% ...
u-fischer commented 2 years ago

Your redefinition of \th is wrong. This is a command that depends on the font encoding and it should be redefined with the suitable tools. E.g.

\documentclass{article}
\usepackage[T1]{fontenc}
\DeclareTextCommand{\th}{T1}{don't redefine this}

\usepackage{hyperref}
%\DeclareTextCommand{\th}{PU}{th in the bookmarks} %definition of \th in the bookmarks 
\begin{document}
\section{\th}
\th 
\end{document}

In case you want to use \th for something font independent, you will have to do the redefinition later. And please use \renewcommand to make clear that you are redefining an existing LaTeX command.

Maelan commented 2 years ago

I don’t get the explanation that it “depends on the font encoding” (isn’t it just inserting two ASCII letters? the \textrm bit was just an example of course), but at least I learned that \DeclareTextCommand{\th}{T1}{...} defines the command just as well and is apparently not overridden by hyperref as far as I’m concerned. So, good enough I guess?

u-fischer commented 2 years ago

it means that every font encoding can have its own independant definition of such a command. T1-encoding setups \th, and hyperref sets it up for PU encoding. Generally it is not a good idea to overwrite such commands with something completly different. Internal code can depend on \th meaning the th-glyph.

Maelan commented 2 years ago

I see, thanks and sorry for the trouble.