1

I read how to make a reference that has as a label the label of a description label (Reference name of description list item in LaTeX):

\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
  \let\orglabel\label
  \let\label\@gobble
  \phantomsection
  \edef\@currentlabel{#1}%
  %\edef\@currentlabelname{#1}%
  \let\label\orglabel
  \orgdescriptionlabel{#1}%
}
\makeatother

This works. However, often description labels end with a special character, like : or ,, which should not be part of the reference label. How can I remove this character from the reference label?

Edit:

Tried:

\usepackage{xstring}
...
  \edef\@currentlabel{\StrGobbleRight{#1}{1}}%
...

But that seems not to work for some reason.

Edit

Here is a full document (from the link above):

\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}

\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
  \let\orglabel\label
  \let\label\@gobble
  \phantomsection
  \edef\@currentlabel{#1}%
  %\edef\@currentlabelname{#1}%
  \let\label\orglabel
  \orgdescriptionlabel{#1}%
}
\makeatother

\begin{document}

\section{Definitions}
\begin{description}
    \item [Vehicle\label{itm:vehicle}] Something
    \item [Bus\label{itm:bus}] A type of \ref{itm:vehicle}
    \item [Car\label{itm:car}] A type of \ref{itm:vehicle} smaller than a    \ref{itm:bus}
\end{description} 

The item `\ref{itm:bus}' is listed on page~\pageref{itm:bus} in section~\nameref{itm:bus}.

\end{document}
  • If one knows already what kind of symbol follows a description label one can leave it out of the description label an add it static to the definition, like \orgdescriptionlabel{#1:} or \orgdescriptionlabel{#1,}. – Daniel Apr 26 '16 at 10:24
  • I would be very nice if you would provide a document to start with, not just fragments – user31729 Apr 26 '16 at 11:49
  • Sorry, I was hoping that the provided link was enough. – Daniel Apr 26 '16 at 15:21
2

You can select to remove a final punctuation based on \spacefactor:

\documentclass{article}
\usepackage{xstring}
\usepackage{hyperref}
\usepackage{nameref}

\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
  \begingroup
  \let\label\@gobble
  \phantomsection
  \protected@edef\daniel@currentlabel{#1}%
  \sbox\z@{\daniel@currentlabel\xdef\daniel@sf{\the\spacefactor}}%
  \ifnum\daniel@sf>1000
    \noexpandarg
    \expandafter\StrGobbleRight\expandafter{\daniel@currentlabel}{1}[\daniel@currentlabel]%
  \fi
  \def\x{%
    \expandafter\endgroup
    \expandafter\def\expandafter\@currentlabelname
    \expandafter{\daniel@currentlabel}}%
  \x
  \orgdescriptionlabel{#1}%
}
\makeatother

\begin{document}

\section{Definitions}
\begin{description}
    \item [Vehicle\label{itm:vehicle}] Something
    \item [Bus,\label{itm:bus}] A type of \nameref{itm:vehicle}
    \item [Car\label{itm:car}] A type of \nameref{itm:vehicle} smaller than a \nameref{itm:bus}
\end{description}

References: `\nameref{itm:bus}' and `\nameref{itm:car}'.
\end{document}

After a punctuation sign, the space factor is greater than 1000 (with standard \nonfrenchspacing setup; if you use \frenchspacing, also load amsthm that adjusts things so the space factor can still be used).

enter image description here

In order to use \ref instead of \nameref, just a small change is needed; I would recommend being consistent in using \ref for numbers and \nameref for text, but I can understand the need of not changing existing documents.

\documentclass{article}
\usepackage{xstring}
\usepackage{hyperref}

\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
  \begingroup
  \let\label\@gobble
  \phantomsection
  \protected@edef\daniel@currentlabel{#1}%
  \sbox\z@{\daniel@currentlabel\xdef\daniel@sf{\the\spacefactor}}%
  \ifnum\daniel@sf>1000
    \noexpandarg
    \expandafter\StrGobbleRight\expandafter{\daniel@currentlabel}{1}[\daniel@currentlabel]%
  \fi
  \def\x{%
    \expandafter\endgroup
    \expandafter\def\expandafter\@currentlabel
    \expandafter{\daniel@currentlabel}}%
  \x
  \orgdescriptionlabel{#1}%
}
\makeatother

\begin{document}

\section{Definitions}
\begin{description}
    \item [Vehicle\label{itm:vehicle}] Something
    \item [Bus,\label{itm:bus}] A type of \ref{itm:vehicle}
    \item [Car\label{itm:car}] A type of \ref{itm:vehicle} smaller than a \ref{itm:bus}
\end{description}

References: `\ref{itm:bus}' and `\ref{itm:car}'.
\end{document}
  • Sounds very good. But for some reason I get only references to section numbers. – Daniel Apr 26 '16 at 16:03
  • @Daniel What did you expect? No number is generated in a description. – egreg Apr 26 '16 at 16:05
  • I was seeing only section numbers instead of the labels. But that was due to some other setting I had in my file. Works perfect! – Daniel Apr 26 '16 at 17:10
  • 1
    @Daniel Added the change – egreg May 27 '16 at 9:41
  • 1
    @Daniel An “arbitrary” prefix that emulates a namespace, so not to inadvertently clobber existing commands. – egreg May 27 '16 at 9:58

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.