0

I wish to redefine \nameref so that its output is printed in italics. However, I'm apparently doing something wrong, because I'm getting an error saying

\nameref undefined. \renewcommand{\nameref}

MWE:

\documentclass{article}
\usepackage{hyperref}

\let\namerefOld\nameref
\renewcommand{\nameref}[1]{\textit{\namerefOld{#1}}}

\begin{document}
    \section{A}
    \label{sec:A}
    bla bla foo bar

    \section{B}
    yada yada yada \nameref{sec:A}

\end{document}

What is the issue here? I would expect this to work, following the example in this question.
Using \nameref without the \let and \renewcommand in the preamble works fine, as well as simply surrounding it with \textit{...}, which yields the desired output. I don't want to surround every occurrence of \nameref with \textit, though.

PixelMaster
  • 255
  • 1
  • 10
  • 2
    Try adding \typeout{\meaning\nameref} before and after \begin{document} and look in the log (after you outcomment your two lines. As you'll notice \nameref is actually not defined in the preamble, it is delayed. Try wrapping your two lines in \AtBeginDocument{...}. BTW: I'd probably now overwrite the original macro like this, it just make \itnameref and use that instead. – daleif Mar 16 '18 at 13:20
  • @daleif: Agreed, except that I would recommend using \show\nameref instead, since it also writes the name of the macro whose definition is shown and thus makes it easier to find in the log file. – schtandard Mar 16 '18 at 13:23
  • sure, I could define a new command. I'm just a fan of user-friendlyness and error-resistant code - and if I use a custom command, then I might still accidentally use \nameref, leading to inconsistency. – PixelMaster Mar 16 '18 at 13:31
  • @schtandard erh, adding \show\nameref to the preamble will not compile, my version with two \typeout{\meaning...} does – daleif Mar 16 '18 at 14:18
  • @daleif I can confirm that, didn't work for me either. – PixelMaster Mar 16 '18 at 14:19
  • @daleif, @PixelMaster: Really? That seems very weird, since \show is a TeX primitive.. What is the error you are getting? (If you are not running LaTeX in nonstopmode (which most editors use), it will stop when it reaches a \show (which is good, because then you can just see the definition you are looking for in the command line without looking at the log file), but that is not an error. Simply pressing enter makes LaTeX continue and compile the document just fine.) – schtandard Mar 16 '18 at 15:13
  • Argument of \nameref has an extra }. typeout{\show\nameref} and Paragraph ended before \nameref was complete. \typeout{\show\nameref} – PixelMaster Mar 16 '18 at 15:30
  • @schtandard the whole point here was to show that there is no definition in the preamble, and there is one in the body. This should be independent of the run mode – daleif Mar 16 '18 at 15:47
  • @PixelMaster: Ah, try \show\nameref, not \typeout{\show\nameref}. (\show doesn't put anything into your document.) – schtandard Mar 16 '18 at 15:52
  • @daleif: That's right, which is why it is weird that adding \show\nameref to the preamble would produce an error. After all, it is the designated TeX primitive for printing the definition of a macro to the log file (unlike the rather akward workaround of combining \meaning with \typeout). It should produce > \nameref=undefined.. – schtandard Mar 16 '18 at 15:57
  • @schtandard the point is to give something that does not give the error, it should show both at once, and there by show the OP that in the preamble it is not defined and in the body it is, using \show you'll need two compilations and an out comment to to that generally (assuming everyone is running nonstopmode is bad, I don't for example) – daleif Mar 16 '18 at 16:17
  • @daleif: You seem to misunderstand: Even without nonstopmode, you need only one compilation and there is no error message (you do however need to tell LaTeX to continue after it stopped, when not in nonstopmode). If you don't like that LeTeX stops, you should probably use nonstopmode. The advantage of using \show is that it tells you which macro it is showing and on which line you asked for that, which can be very helpful when you want to know the definitions of several macros. (Also, \show is shorter). But whatever, the OP's problem seems to be solved. – schtandard Mar 16 '18 at 16:42
  • @schtandard on my system show stops the compilation – daleif Mar 16 '18 at 17:15

1 Answers1

1

\nameref is not defined in the preamble, so you can't capture its definition there. You either have to move

\let\namerefOld\nameref
\renewcommand{\nameref}[1]{\textit{\namerefOld{#1}}}

after \begin{document} or write

\AtBeginDocument{
  \let\namerefOld\nameref
  \renewcommand{\nameref}[1]{\textit{\namerefOld{#1}}}
}

in the preamble.

  • problem solved, thanks! Props to @daleif as well, who provided this answer in the comments earlier already (anti-props for answering in comments, though) – PixelMaster Mar 16 '18 at 13:46
  • @PixelMaster that is actually quite normal, when trying to figure out what is wrong – daleif Mar 16 '18 at 14:16