
En este ejemplo, los uso \cref{first,second}
y cleveref
los etiqueto correctamente:
\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}
Pero, si uso \nameref{first,second}
, entonces solo me da ??
en lugar de First and Second
.
¿Cómo puedo usarlo \nameref
con varias etiquetas como \cref
?
Respuesta1
Puede utilizar el siguiente código. Tenga en cuenta que ese user
es su nombre para mostrar actual; probablemente debería reemplazar el user
prefijo en el código con algo que sea más probable que sea único para asegurarse de no entrar en conflicto con otro código no relacionado que pueda existir usando el mismo prefijo.
% 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}