1

I have defined the following command:

\newcommand*{\namerefLabeled}[1]{\nameref{#1} (\autoref{#1})}

It largely does what it's supposed to, i.e. turning something like:

For further details, see \namerefLabeled{sec:cleancode}.

into

For further details, see Clean Code (section 1.2.3).

However, if I reference a label within a paragraph, it does not choose the desired section naming. See this MWE:

\documentclass{article}
\usepackage{hyperref}

\newcommand*{\namerefLabeled}[1]{\nameref{#1} (\autoref{#1})}

\begin{document}
    \section{A}
    \label{A}
        \subsection{B}
        \label{B}
            \subsubsection{C}
            \label{C}
                \paragraph{D}
                \label{D}
    \section{where stuff is referenced}
    For reference, see \namerefLabeled{A}, \namerefLabeled{B}, \namerefLabeled{C} or \namerefLabeled{D}.
\end{document}

Output:

output

Does someone know how to fix this issue, e.g. by defining an autoref-name for paragraphs, if that's possible? My guess is there is no value set for it, and so it chooses (what I assume to be) the default labeling name "section".

Using \renewcommand\paragraphautorefname{paragraph} did not solve it.

2
  • Welcome to TeX.SX!! Rather than posting code fragments it is better to give a full minimal working example. Currently we have to guess what packages etc you are using and this makes it really hard to help you. A MWE should start with a \documentclass command, have a minimal preamble and then \begin{document}...\end{document}. The code should compile and be as small as possible to demonstrate your problem. This said, I'd guess that you just need \renewcommand\paragraphautorefname{paragraph}.
    – user30471
    Mar 15, 2018 at 11:20
  • MWE added. Your suggestion did not solve the issue, though (thanks nevertheless) Mar 15, 2018 at 12:31

2 Answers 2

1

The problem is not paragraph, you will have the same problem e.g. with \subsection*. When the sectioning command is not numbered, it is always registered as a unnumbered section. To change this you have to patch some internal macros: Insert in your preamble

\makeatletter
\newcommand\currenthrefautoname{}
\newcommand\criticalhrefautoname{section*}
\newcommand\currentsectioningcommand{section}
\let\@StartSection\@startsection
\def\@startsection#1{%
  \edef\currentsectioningcommand{#1}%
  \@StartSection{#1}%
}
\let\HY@MakeCurrentHrefAuto\Hy@MakeCurrentHrefAuto
\def\Hy@MakeCurrentHrefAuto#1{%
  \long\edef\currenthrefautoname{#1}%
  \ifx\currenthrefautoname\criticalhrefautoname%
    \edef\currenthrefautoname{\currentsectioningcommand*}%
  \fi%
  \expandafter\HY@MakeCurrentHrefAuto\expandafter{\currenthrefautoname}%
}
\makeatother
4
  • I'm having difficulties comprehending why someone would know these commands, but it solves my problem. Here, have a cookie * gives cookie * Mar 15, 2018 at 17:44
  • I noticed that my original desire actually doesn't make that much sense, since no "paragraph 1.2.3" exists - "1.2.3" would be a subsubsection. Could your code snippet be changed to print out the corresponding name for whichever section's number is listed? I.e. referencing an unnumbered paragraph within a numbered subsubsection would print "paragraphname (subsubsection 1.2.3)"? Mar 16, 2018 at 12:12
  • @PixelMaster Of course, but I think you should either edit the question or ask a new one then, otherwise the new answer will not fit the question. Mar 16, 2018 at 12:28
  • I figured. I think I'll just replace \autoref with \cref - that just uses the same label for everything (in englisch, probably "section". Since I'm german, everything gets called "Abschnitt"). Mar 16, 2018 at 12:50
3

The problem is that \paragraph (or more precisely \@sect) does not call \refstepcounter when the TOC will not include these entries. This can be fixed by changing the secnumdepth counter.

\documentclass{article}
\usepackage{hyperref}

\newcommand*{\namerefLabeled}[1]{\nameref{#1} (\autoref{#1})}

\setcounter{secnumdepth}{4}

\begin{document}
    \section{A}
    \label{A}
        \subsection{B}
        \label{B}
            \subsubsection{C}
            \label{C}
                \paragraph{D}
                \label{D}
    \section{where stuff is referenced}
    For reference, see \namerefLabeled{A}, \namerefLabeled{B}, \namerefLabeled{C} or \namerefLabeled{D}.
\end{document}
2
  • while that indeed solves the problem, I would rather have "paragraphs" called "sections" than numbering them. Thanks nevertheless :) Mar 15, 2018 at 17:39
  • \label uses whatever the last call to \refstepcounter saved in \@currentlabel, \@currentlabelname and \@currentHref. \@currentlabelname could easily be set to "equation" or "page" instead. Mar 16, 2018 at 16:19

You must log in to answer this question.

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