1

Suppose I'm writing an .sty for people to use. This .sty might be loaded before or after hyperref, and possibly before some other packages define \chaptername, \chapterautorefname, \sectionname etc. to lowercase names (actually, they probably don't, but I want to be robust).

How should I enforce the capitalization of these names (i.e. for \autoref{chap:foo} to produce "Chapter 2" rather than "chapter 3")?

  • Define it as late as possible, but be prepared: any user will find a way to screw your plans. – Johannes_B May 31 at 17:02
  • How about "Don't use the \autoref machinery. Instead, (a) load the cleveref package and (b) base your own .sty file on the \cref and \crefrange macros. Be sure to load the cleveref package with the options nameinlink (to mimic the output of \autoref) and capitalize (capitalise "works too...). Finally, for good measure, issue the instruction \let\autoref\cref`. – Mico May 31 at 17:52
  • 1
    @Mico: Could you flesh this out a bit with a concrete example and it an answer? I have some comments to make about it... – einpoklum May 31 at 18:46
  • @einpoklum - Done. :-) – Mico May 31 at 19:15
2

For a step up in robustness against user interference, I suggest you not employ the \autoref machinery. Instead, I suggest you work with the cleveref package and its user-level macro \cref.

Something along the following lines, maybe. First, create a .sty file called, say, myrefs.sty.

\ProvidesPackage{myrefs}[2019/05/31 einpoklum's helper style file]
\RequirePackage{hyperref}
\hypersetup{colorlinks,allcolors=blue} % or whatever options are needed

\RequirePackage[nameinlink]{cleveref} % 'nameinlink' to mimic \autoref's behavior
%% Use low-level controls to hard-code the required appearance.
\crefname{chapter}{Chapter}{Chapters}
\crefname{section}{Section}{Sections}
\crefname{equation}{Equation}{Equations}
% etc

\let\autoref\cref % set \autoref as an alias for \cref
\endinput

Then, load the style file in the preamble of your tex file and use either \autoref or \cref to create cross-references. Of course, you should still advise the users of your package to (a) not load the hyperref package and (b) load the myrefs package last.

enter image description here

\documentclass{report}
\usepackage{myrefs}
\begin{document}
\chapter{AAA} \label{chap:aaa}
\section{BBB} \label{sec:bbb}
\begin{equation}1+1=2\label{eq:triv}\end{equation}
Cross-references to \autoref{chap:aaa,sec:bbb,eq:triv}.
\end{document}
  • 1. How is this better than using autoref? People can just as easily change \crefname's, cant they? 2. Won't it be confusing/cause trouble for authors, for me to sneekily replace their \autoref with a \cref 3. The myrefs package will probably not be loaded last. – einpoklum May 31 at 19:34
  • 1
    @einpoklum - I deliberately call my solution a "step up in robustness" against user interference, not "the ultimate in robustness." Indeed, if somebody wants to sabotage the package, they can look inside the sty file and modify the \crefname macros to their hearts' content. Nevertheless, this solution is more robust than any solution that relies on the values of the LaTeX-kernel names. About the package loading order: Untoward things can happen if hyperref and cleveref aren't loaded last. However, that's no different from any other method that might use these packages. – Mico May 31 at 20:38
  • 1
    I think this is a nice suggestion and have upvoted, but was reflecting how authors using my sty without being aware of the details may see things. – einpoklum May 31 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.