2

I provide this pre-amble of my main desired output, having been told that my below approach is inherently misguided (due to \nameref not being designed or able to obtain only a numeral, instead of a descriptive label).

I would nonetheless like to be able to have a command, like \nameref, that given a label retrieves the enumerate counter associated with its respective item.

My goal is to be able to refer to enumerate items in both a single (as currently accomplished with \ref) and multiple-label contexts. For example, I would like to be able to continue to do \ref{<label1>}, but also to do something like \multirefs{<label1>, <label2>} and have this result in the following outputs:

\ref{<label1>} : (A_{1}) ← this currently works

\multirefs{<label1>, <label2>} : (A_{1}, A_{2}) ← this is what I would like

I am, however, most interested in more general approaches, similar to the linked answer.


I am trying to get \nameref to work properly with enumitem. To do so, I am using the preamble code provided in the existing answer on this topic.

I am then wrapping the document code from that answer, within a custom command, as shown below (and partly adapted from another answer):

\newcommand{\labelleditem}[1]{%
                                 \stepcounter{enumi}%
                                 \item[{\theenumi{}}\label{#1}]%
                             }

I want to make use of the description environment, with the optional arguments of an enumerate environment, as follows:

\begin{description}[label={($\text{A}_{\arabic*}$)}, series=name, resume=name]
    \labelleditem{Broken} XXX
    \item XXX
\end{description}

This does not work, presumably in part due to the description environment not fully-implementing or making accessible the enumitem optional arguments.

To clarify, I only wish to use the enumerate list, as shown in the below MWE. I do not, however, know how to properly adapt the existing answer on this topic, which uses a description list, to an enumerate list.

How might I be able to get this to work, similar to the existing answer on this topic, but with my additional specifications, shown above?

Alternatively, how can I get \nameref to work via a more general or direct patch to enumitem?


MWE:

\documentclass{article}

\usepackage{enumitem}

\makeatletter
\newcommand\enit@descriptionlabel@hook[1]{}
\def\enit@description@i#1#2#3#4{%
  \ifnum#1>#3\relax
    \enit@toodeep
  \else
    \enit@list{}%
      {\let\enit@type\tw@
       \advance#1\@ne
       \labelwidth\z@
       \enit@align@left
       \let\makelabel\descriptionlabel
       \enit@style@standard
       \enit@preset{#2}{#1}{#4}%
       \enit@calcleft
       \let\enit@svlabel\makelabel
       \def\makelabel##1{%
         \NR@gettitle{##1}%
         \labelsep\z@
         \ifenit@boxdesc
           \enit@svlabel{\enit@align{\enit@format{\enit@descriptionlabel@hook{##1}}}}%
         \else
           \nobreak
           \enit@svlabel{\enit@format{\enit@descriptionlabel@hook{##1}}}%
           \aftergroup\enit@postlabel
         \fi}%
       \enit@before
       \enit@negwidth%
       }%
     \enit@keyfirst
  \fi}

\usepackage{hyperref}

\AtBeginDocument{\let\descriptionlabel\NRorg@descriptionlabel}
\renewcommand\enit@descriptionlabel@hook[1]{\NR@gettitle{#1}#1}

\newcommand{\labelleditem}[1]{%
                             %\label{#1}%
                             \stepcounter{enumi}%
                             \item[{\theenumi{}}\label{#1}]%
                             %
                             }
% -------------------------------------

\begin{document}


\begin{description}
  \item [{Test}\label{Test}] Some text
\end{description}

\begin{description}[label={($\text{A}_{\arabic*}$)}, series=name, resume=name]
    \labelleditem{Broken} XXX
    \item XXX
\end{description}

\nameref{Test}

\nameref{Broken}

\end{document}

The above produces:

Compiled output of the above MWE

It also outputs the following error:

Missing control sequence inserted.
<inserted text> 
                \inaccessible 
l.57 ...}_{\arabic*}$)}, series=name, resume=name]

I also provide this MWE on Overleaf.

Coby Viner
  • 1,939
  • 1
    Your goal is quite unclear. You seem to mix a description list with an enumerate list, but add the numbers only for some items. Why are you doing this? – Ulrike Fischer Oct 10 '19 at 07:21
  • @UlrikeFischer Yes, that's not my intention. I just don't know how to properly adapt the linked solution to an enumerate-based list. I only want an enumerate list, but I'd like it to work with \nameref, as implemented in the description modifications from the linked answer. I must just be misunderstanding how that was done, and I'm not sure how to adapt it. – Coby Viner Oct 10 '19 at 17:13
  • Enumerate lists don't have "names" only numbers so what do you expect to get back? – Ulrike Fischer Oct 10 '19 at 19:50
  • @UlrikeFischer The numbers themselves, e.g. (A_{1}) vs., say, (A_{10}) , in this example. – Coby Viner Oct 10 '19 at 19:58
  • That is, I'd like to be able to do, e.g., \hyperref[\nameref{<label>}]{<label>}, obtaining, say, (A_{1}), that properly links to that item. – Coby Viner Oct 10 '19 at 20:06
  • Have you looked at cleveref? As far as I can tell, it has already invented the wheel you want. – cfr Oct 11 '19 at 23:28
  • @cfr It does indeed, but unfortunately I need this to work with hyperref. – Coby Viner Oct 12 '19 at 00:33

2 Answers2

0

From the comments I guess that you don't want description or nameref but simply an enumerate list with a special label:

\documentclass{article}

\usepackage{enumitem}
\usepackage{hyperref}

\begin{document}

\begin{enumerate}[label={(A\textsubscript{\arabic*})}, series=name, resume=name]
    \item\label{test} XXX
    \item XXX
\end{enumerate}

\ref{test}


\end{document}

enter image description here

Ulrike Fischer
  • 327,261
  • Thanks, but I really need \nameref to function, as poorly attempted in my question. This simple approach is what I am already using, but I need \nameref to work for my custom command \labelleditem, as shown in my (admittedly broken) MWE. – Coby Viner Oct 11 '19 at 18:18
  • I don't see what nameref has to do with your command. It simply increases a counter and doesn't set a name. – Ulrike Fischer Oct 11 '19 at 20:39
  • Well, that's fair enough, but I nonetheless wish to use \nameref to manually construct more complex references. I thought I could elide those details, but my eventual goal is to have the option to construct, e.g., (\hyperref[\nameref{<label1>}]{<label1>}, \hyperref[\nameref{<label2>}]{<label2>}), to produce, say, (A_{1}, A_{2}), where each properly links to their items. – Coby Viner Oct 11 '19 at 21:28
  • You are misunderstanding nameref. – Ulrike Fischer Oct 11 '19 at 21:42
  • How so? I want to encode the enumerate numeral into the nameref-retrievable label. I realize that will require patching, beyond its present configuration, but why can't nameref be used in that manner? – Coby Viner Oct 11 '19 at 21:43
  • Because you don't want to retrieve a name, you want the normal ref(s). – Ulrike Fischer Oct 11 '19 at 21:46
  • But I don't (or at least I don't think I do). I do indeed want to obtain names, in the form of the labelled numerals, to permit my construction of more complex references, involving multiple items. – Coby Viner Oct 11 '19 at 21:51
  • 2
    For a section ref gives the number and nameref gives the title. There is nothing similar in an enumerate. It only has a number, there is no text that nameref can retrieve. Stop to cling on the nameref idea and better describe what you really want, so that someone can tell you how to implement it. – Ulrike Fischer Oct 11 '19 at 21:56
  • I'd still quite like for it to retrieve the number, but very well. Apologies for the lack of clarity. I will try to edit the question accordingly. – Coby Viner Oct 11 '19 at 22:00
  • I have now edited the question, to hopefully provide a better exposition of my goal. I really appreciate your engaging with me on this! Please let me know if my question is still not sufficiently clear. – Coby Viner Oct 11 '19 at 22:06
  • better ask a new question. This one is quite a mess already. (But I don't see what you gain by an additional \multiref compared to \ref{A}, \ref{B}.) – Ulrike Fischer Oct 12 '19 at 15:02
  • The difference in this example is (A_{1}), (A_{2}) vs, (hopefully, once corrected) (A_{1}, A_{2}). – Coby Viner Oct 13 '19 at 16:23
0

The answer provided on a more recent iteration of this question has proven sufficient.

Coby Viner
  • 1,939