在此示例中,我使用\cref{first,second}
并cleveref
正确标记了它们:
\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}
但是,如果我使用\nameref{first,second}
,那么它只会给我??
而不是First and Second
。
如何使用\nameref
多个标签\cref
?
答案1
您可以使用以下代码。请注意,这user
是您当前的显示名称;您可能应该将user
代码中的前缀替换为更可能唯一的前缀,以确保不与可能使用相同前缀的其他不相关代码发生冲突。
% 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}