1

On this example, I use \cref{first,second} and cleveref correctly labels them: enter image description here

\documentclass[10pt,a5paper]{memoir}
\usepackage[brazil]{babel}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}

\namecref{first,second}

\chapter{First}
\label{first}

\chapter{Second}
\label{second}

\end{document}

But, if I use \nameref{first,second}, then, it gives me only ?? instead of First and Second.

How can I use \nameref with multiple labels as \cref?

  • the cleveref manual states "Note that [namecref] can only be passed a single reference name; they do not accept multi-references like \cref. (Passing multiple references to these commands would make little sense if the references had different types, and is redundant if they have the same type.) – ebcontrol Aug 23 at 13:07
2

You can use the following code. Note that user is your current display name; you should probably replace the user prefix in the code with something more likely to be unique in order to be sure not to conflict with other unrelated code that might exist using the same prefix.

% The code below automatically adapts to the selected language.
\documentclass[english]{article}
\usepackage{babel}
\usepackage{xparse}
\usepackage{hyperref}
\usepackage{cleveref}

\ExplSyntaxOn

% #1: variant (cref, Cref, crefs or Crefs)
% #2: reference name (label)
\cs_new_protected:Npn \user_name_cref:nn #1#2
  { \use:c { name #1 } {#2} }

\cs_generate_variant:Nn \user_name_cref:nn { xV }

% #1: boolean expression (true: disable hyperlink)
% #2: reference name (label)
\cs_new_protected:Npn \user_name_ref:nn #1#2
  { \bool_if:nTF {#1} { \nameref* } { \nameref } {#2} }

\cs_generate_variant:Nn \user_name_ref:nn { nV }

\seq_new:N \l__user_name_refs_tmpa_seq
\seq_new:N \l__user_name_refs_tmpb_seq
\int_new:N \l__user_name_refs_nbrefs_int
\tl_new:N \l__user_name_refs_firstref_tl

% #1: boolean expression (true: start with capitalized letter, as in \Cref)
% #2: boolean expression (true: disable hyperlinks)
% #3: comma list of refs
\cs_new_protected:Npn \user_name_refs:nnn #1#2#3
  {
    \seq_set_from_clist:Nn \l__user_name_refs_tmpa_seq {#3}
    \int_set:Nn \l__user_name_refs_nbrefs_int
                { \seq_count:N \l__user_name_refs_tmpa_seq }
    \seq_get_left:NN \l__user_name_refs_tmpa_seq \l__user_name_refs_firstref_tl

    % (section, Section, sections or Sections) or (theorem, Theorem, ...) or...
    \user_name_cref:xV
      { \bool_if:nTF {#1} { C } { c }
        ref
       \int_compare:nNnTF { \l__user_name_refs_nbrefs_int } > { 1 } { s } { } }
     \l__user_name_refs_firstref_tl
    \nobreakspace

    % Now print the references.
    \seq_clear:N \l__user_name_refs_tmpb_seq
    \seq_map_inline:Nn \l__user_name_refs_tmpa_seq
      {
        \seq_put_right:Nn \l__user_name_refs_tmpb_seq
                          { \user_name_ref:nn {#2} {##1} }
      }
    \seq_use:Nnnn \l__user_name_refs_tmpb_seq { \crefpairconjunction }
                  { \crefmiddleconjunction } { \creflastconjunction }
  }

\cs_generate_variant:Nn \user_name_refs:nnn { nx }

\cs_new_protected:Npn \__user_name_refs:Nnn #1#2#3
  {
    \user_name_refs:nxn {#1}
      { \IfBooleanTF {#2} { \c_true_bool } { \c_false_bool } }
      {#3}
  }

% “Start in lower case” variant. With star: disable hyperlinks.
% #2: comma list of refs
\NewDocumentCommand \nameRefs { s m }
  {
    \__user_name_refs:Nnn \c_false_bool {#1} {#2}
  }

% “Start in upper case” variant. With star: disable hyperlinks.
% #2: comma list of refs
\NewDocumentCommand \NameRefs { s m }
  {
    \__user_name_refs:Nnn \c_true_bool {#1} {#2}
  }

\ExplSyntaxOff

\begin{document}

References:
\begin{itemize}
\item With one reference and hyperlink: \nameRefs{first} (we'll disable
  hyperlinks from now on, because their default appearance is hideous and they
  don't behave very well across line breaks);
\item With one reference: \nameRefs*{first};
\item With two references: \nameRefs*{first, second};
\item With three references: \nameRefs*{first, second, third};
\item With four references: \nameRefs*{first, second, third, fourth};
\item Capitalized variant: \NameRefs*{first, second, third, fourth};
\item etc.
\end{itemize}

% This is a cref command; beware that spaces are not ignored after the commas!
For comparison, the \verb|\cref| command: \cref{first,second,third,fourth}.

\section{First section}
\label{first}

\section{Second section}
\label{second}

\section{Third section}
\label{third}

\section{Fourth section}
\label{fourth}

\end{document}

Screenshot

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.