1

Is it possible to replace the name of cross-references in place of their numbers in multi-referencing with \cref ? I want to define a \fullref command for referencing the name of sections and chapters:

Code:

\section{First Section}
label{sec:first}
\section{Second Section}
\label{sec:second}
\section{fullref command}
As discussed in \fullref{sec:first,sec:second} ...

Text: As discussed in "sections First Section and Second Section" ...

The \nameref code would not type the word "Sections" before the reference name and also can't be used for multiple references.

Also, for referencing to just one label with its name instead of number including its type, there is a good answer here: How to display label name in autoref in place of its number

  • You should link the originating question as well ;-) – user31729 May 8 '15 at 13:29
  • I link the main question ;) – Mehran Ebrahimian May 8 '15 at 13:46
  • I've changed my solution here – user31729 May 8 '15 at 14:51
1

A hackish version, combining lists and counters. The most complicated stuff is to get the and in between, as well as the expansion.

Short explanation:

The comma separated value list of labels is stored to an etoolbox list, then split using into individual references, using a wrapper command to \nameref (due to expansion issues). An external counter labelcount is used for counting the number of references, to track for the issues of just one or multiple reference(s) (singular/plural) and the and conjunction. All is inside a group, so this should not have any side effects.

The starred version of \fullref uses \nameref* in turn. (It's the basic idea from my answer to this question: How to display label name in autoref in place of its number

\documentclass{book}

\usepackage{blindtext}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{hyperref}

\usepackage{cleveref}



\newcommand{\andconjunctionname}{%
  and
}

\newcommand{\checkforlastitem}[2]{%
  \ifnumequal{#1}{#2}{%
    \ignorespaces
  }{%
  }%    
}

\NewDocumentCommand{\namerefwrap}{sm}{%
  \stepcounter{myrefscounter}%
  \IfBooleanTF{#1}{%
    \nameref*{#2}\checkforlastitem{\value{myrefscounter}}{\value{labelcount}}
  }{%
    \typeout{Link reference #2}%
    \nameref{#2}\checkforlastitem{\value{myrefscounter}}{\value{labelcount}}
  }%
  \ifnumgreater{\value{labelcount}}{1}{% Only add an `and` if there is more than one label
    \ifnumequal{\value{myrefscounter}}{\value{labelcount}}{%
    }{% Only add an `add`, if it's not the last label
      \andconjunctionname%
    }%
  }{%
  }%
}



\newcounter{labelcount}%

\newcommand{\addlabelstolist}[2]{%
  \stepcounter{labelcount}% Count the labels
  \listadd{#1}{#2}%
}

\newcounter{myrefscounter}%

\newcommand{\getrefelement}[2]{%
  \setcounter{myrefscounter}{0}%
  \renewcommand*{\do}[1]{%
    \ifnumequal{\value{myrefscounter}}{#1}{%
      \gdef\firstref{##1}\listbreak% Brute force  ;-)
    }{%
      \stepcounter{myrefscounter}%
    }%
  }%
  \dolistloop{#2}%
}


\NewDocumentCommand{\fullref}{s+m}{%
  \begingroup
  \setcounter{labelcount}{0}%
  \listadd{\myrefs}{}%
  \forcsvlist{\addlabelstolist{\myrefs}}{#2}%
  % Get the first element for the \namecref command
  \getrefelement{0}{\myrefs}%
  \ifnumgreater{\value{labelcount}}{1}{%
    \namecrefs{\firstref}
  }{%   
    \namecref{\firstref}
  }%
  % Expand the label list
  \setcounter{myrefscounter}{0}%
  \IfBooleanTF{#1}{%
    \forlistloop{\namerefwrap*}{\myrefs}%
  }{%
    \forlistloop{\namerefwrap}{\myrefs}%
  }%
  \endgroup
}

\begin{document}
\chapter{First} \label{firstchapter}
\section{First section} \label{firstsection}
\section{Second section} \label{secondsection}


\chapter{Second}
In \fullref*{firstchapter,thirdchapter} we saw ... whereas in \fullref{firstsection,secondsection} there was some 

 \blindtext

\chapter{Third} \label{thirdchapter}


\end{document}

enter image description here

  • Thanks. It works properly; however, there is no possibility to replace the second "and" with "\crefpairconjunction" to let the clever package options control your conjuction name – Mehran Ebrahimian May 8 '15 at 15:45
  • @MehranEbrahimian: Replacing? What do you mean? – user31729 May 8 '15 at 15:47
  • Just change and to \crefpairconjunction, this should work... – user31729 May 8 '15 at 15:50
  • Yes, it works; thanks. Also, I think your answer needs slight modifications to use \crefmiddleconjunction and \creflastconjunction commands (from cleveref package) between names of references instead of the same word "and" or "\crefpairconjunction". I don't know the answer :( – Mehran Ebrahimian May 8 '15 at 16:15
  • @MehranEbrahimian: Your initial question did not mention that \crefmiddleconjunction etc. is important at all. Now the question is changed and I have to edit my answer (again). Your questions tend to be changed and being unclear, in my point of view, leaving all efforts by me and possible other users here effectively useless – user31729 May 8 '15 at 18:32

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.