[Tex/LaTex] hyperref package prevents shifting from OT1 to T1 encoding

fontshyperref

As addressed in Temporarily change font encoding with fontenc, I need to switch to T1 encoding to get certain T1 glyphs. This compiles fine with hyperref:

\documentclass{article}
\usepackage[T1, OT1]{fontenc}
\usepackage{hyperref}
\begin{document}
Har{\fontencoding{T1}\selectfont{\dh}}arson
\end{document}

As addressed in \renewcommand and \newcommand for accented letters, I'm redefining some T1 macros in my preamble. In this case, however, hyperref seems to insist that the encoding is still in OT1 (it compiles fine without hyperref).

\documentclass{article}
\usepackage[T1, OT1]{fontenc}
\renewcommand{\dh}{\fontencoding{T1}\selectfont{\symbol{240}}}
\usepackage{hyperref}
\begin{document}
Har{\dh}arson
\end{document}

LaTeX Error: Command \dh unavailable in encoding OT1.

What is going on here … ?


EDIT:

It turns out that it works fine if the macro is redefined after hyperref is loaded:

\documentclass{article}
\usepackage[T1, OT1]{fontenc}
\usepackage{hyperref}
\renewcommand{\dh}{\fontencoding{T1}\selectfont{\symbol{240}}}
\begin{document}
Har{\dh}arson
\end{document}

So this now begs the question why hyperref causes this behavior.

Best Answer

hyperref loads pd1enc.def which set up the encodings for the bookmarks and overwrite your definition. Moving the definition behind hyperref will avoid the error but you will not be able to use the command e.g. in bookmarks.

LaTeX is trying hard to set up such definitions in a way that works with various encodings. You should not destroy this system by redefining LICR-commands in this way. Use the correct commands.

\documentclass{article}
\usepackage[T1, OT1]{fontenc}
\DeclareTextSymbolDefault{\dh}{T1}
\usepackage{hyperref}
\begin{document}
\section{Har{\dh}arson } %to test bookmarks
Har{\dh}arson 
\end{document}