2

I want to renew the command \t which can easily be done with \RenewDocumentCommand or \renewcommand, but if that definition is used with hyperref, it produces errors. How to make my definition override the hyperref one? What implications will it have?

\documentclass{article}
\RenewDocumentCommand{\t}{ }{foobar}
% \usepackage{hyperref} % Uncommenting this results in errors.
\begin{document}
\t
\end{document}

PS - I am using \RenewDocumentCommand as it is relatively easier to produce a starred variant with it. I need to develop a starred variant of command \t too, hence I need a command which can easily do that task.

3
  • 4
    \t is accent command defined by latex. Don't overwrite it like this globally. Apr 24, 2021 at 6:38
  • 3
    As Ulrike says it is really a bad idea to redefine the accent commands. you might get away with \t as the tie accent isn't so often used but if you redefine \c or \' etc then things will break in ways your users will not expect. Apr 24, 2021 at 7:46
  • sorry for unrelated ping here but I don't have your email:-) Did you see my comment on the learnlatex Marathi translation github.com/learnlatex/learnlatex.github.io/issues/… ? May 28, 2021 at 7:39

2 Answers 2

3

Put it after loading hyperref:

\usepackage{hyperref}
% ...
\RenewDocumentCommand{\t}{ }{foobar}

The reason why hyperref seems to be the cause of the error is because it defines \t as a tie, which requires an argument. This overrides your definition, leaving \t to grab the \end as it's argument... and that causes problems.

I'd suggest using something more descriptive than \t, as these shorthand single-character macros are typically reserved for diacritic marks on letters/symbols.

3
  • Is it absolutely not possible to declare something global/robust which can override the hyperref-definition even if hyperref is loaded after my definition?
    – Niranjan
    Apr 24, 2021 at 5:59
  • 2
    @Niranjan: You can do that by delaying the (re)definition within (say) \AtBeginDocument{...}. Then, even if hyperref is the last package loaded, whatever is included in \AtBeginDocument{...} will be executed after hyperref.
    – Werner
    Apr 24, 2021 at 6:16
  • Thanks :) It works now!
    – Niranjan
    Apr 24, 2021 at 6:34
6

In your own documents you can redefine commands at your hearts desire, but you are writing a package and a package has to take care to fit into the LaTeX ecosystem. You overwrite an existing command with the \Renew and you get errors from hyperref. Both should ring alarm bells.

The original tipa defines \t like this:

\DeclareTextCommand{\t}{T3}[0]{\@ifstar{\tipaloweraccent[.3ex]{60}}%
  {\tipaupperaccent[-.1ex]{62}}}

So it is an encoding dependent command. If you try \t in a small document, or check the definition, you can learn that LaTeX itself only defines a OML-definition. So it is safe for you to provide a TU-definition. This can be done in a similar way as tipa does it. Be aware that hyperref doesn't recognize the star in the bookmarks as this is an invention of tipa which hasn't been carried over (and perhaps can't be carried over, I didn't test).

\documentclass{article}
\usepackage{fontspec}
\setmainfont{charissil}
\usepackage{hyperref}

\makeatletter
\newcommand\my@tlowertie[1]{#1^^^^035c}
\newcommand\my@tuppertie[1]{#1^^^^0361}

\DeclareTextCommand{\t}{TU}{\@ifstar{\my@tlowertie}{\my@tuppertie}}
\makeatother
\begin{document}
\section{ \t*a \t a }
 \t*a \t a

\end{document}

enter image description here

You must log in to answer this question.

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