2

I like to use \let to alias a macro to another. For example, I often do something like \let\brand\textit and then I can just do \brand{Aloc Acoc}.

This seems to work well, but causes problems when hyperref is loaded.

Consider this MWE:

\documentclass{article}

\usepackage{hyperref}

\let\foo\textit

\begin{document}

\section{Command \foo{aliasing}}

Here I use \texttt{\textbackslash foo} to
\foo{to typeset this in italic}.

\end{document}

Compilation fails with ! Argument of \@sect has an extra }. at the \section.

Any ideas as to why this happens? Thanks in advance!

2 Answers 2

5

First of all, doing \let\foo\textit is wrong, see When to use \LetLtxMacro?.

With an up-to-date LaTeX you should do

\NewCommandCopy{\foo}{\textit}

or \LetLtxMacro instead of \NewCommandCopy if you're stuck with an outdated version.

But that's not sufficient. Indeed, in hyperref.sty you find

346 \def\pdfstringdef#1#2{%
347   \begingroup
[...]
403     \let\emph\@firstofone
404     \let\textnormal\@firstofone
405     \let\textrm\@firstofone
406     \let\textsf\@firstofone
407     \let\texttt\@firstofone
408     \let\textbf\@firstofone
409     \let\textmd\@firstofone
410     \let\textit\@firstofone
411     \let\textsc\@firstofone
412     \let\textsl\@firstofone
413     \let\textup\@firstofone
[...]

so \textit is added to the “bookmark exceptions”, which your \foo command isn't.

If you want to alias the command, you need to add it to the exception list.

\NewCommandCopy{\foo}{\textit}
\makeatletter
\pdfstringdefDisableCommands{\let\foo\@firstofone}
\makeatother

Full example.

\documentclass{article}
\usepackage{hyperref}

\NewCommandCopy{\foo}{\textit}
\makeatletter
\pdfstringdefDisableCommands{\let\foo\@firstofone}
\makeatother

\begin{document}

\section{Command \foo{aliasing}}

Here I use \texttt{\textbackslash foo} to
\foo{to typeset this in italic}.

\end{document}

enter image description here

2
  • Thank you, that explains it. Now I feel stupid I didn’t try to locate the problem within hyperref’s source code. Could you please also provide some explanation why doing \let\foo\textit is wrong?
    – bp99
    Apr 9, 2022 at 10:56
  • 1
    @bp99 see my answer at tex.stackexchange.com/q/88001/4427
    – egreg
    Apr 9, 2022 at 12:06
2

Package hyperref has some interactions. If I recall correctly, some are even described in the documentation. Find more here.

Why don't you just use /newcommand?

\documentclass{article}

\usepackage{hyperref}

\newcommand\foo\textit % <<<

\begin{document}

\section{Command \foo{aliasing}}

Here I use \texttt{\textbackslash foo} to
\foo{to typeset this in italic}.

\end{document}

enter image description here

And if you can't let /let go, use /renewcommand to redefine your let.

1
  • You are right, I know \newcommand works; I should have clarified this in my question. I mainly wanted to find out why \let fails.
    – bp99
    Apr 9, 2022 at 10:57

You must log in to answer this question.

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