交叉引用未编号的子部分

交叉引用未编号的子部分

在我的memoir文档中,所有章节没有编号,但列在目录中。是否有某种方法可以利用cleveref(和hyperref)进行交叉引用还有比以下内容更直接的部分吗?

\documentclass[12pt]{memoir}

\setsecnumdepth{subsection}
\settocdepth{subsection}
\setlength{\cftsubsectionindent}{5.25em}
\usepackage{lipsum}
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[noabbrev,capitalize]{cleveref}
\crefname{subsection}{subsection}{subsection}    % not used?
\Crefname{subsection}{Subsection}{Subsection}
\crefname{page}{page}{page}

\begin{document}

\tableofcontents

\chapter{The Chapter}

\subsection*{A particular situation}%
\label{subsec:particular}
\addcontentsline{toc}{subsection}{\nameref{subsec:particular}}

\lipsum[1]

\newpage

\subsection*{Another subsection}
\label{subsec:another}
\addcontentsline{toc}{subsection}{\nameref{subsec:another}}

See subsection~``\nameref*{subsec:particular}'' (\cpageref{subsec:particular}).

\noindent See subsection~``\nameref{subsec:particular}'' (\cpageref{subsec:particular}).

\end{document}

从输出来看:

目录页

包含对小节引用的页面

当然,我可以定义一个宏来进行创建\subsection*{...}\label{...}\addcontentsline...,并定义另一个宏来进行交叉引用:

\documentclass[12pt]{memoir}

\setsecnumdepth{subsection}
\settocdepth{subsection}
\setlength{\cftsubsectionindent}{5.25em}
\usepackage{lipsum}
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[noabbrev,capitalize]{cleveref}
\crefname{subsection}{subsection}{subsection}    % not used?
\Crefname{subsection}{Subsection}{Subsection}
\crefname{page}{page}{page}

\newcommand{\subsec}[2]{%
\subsection*{#1}\label{subsec:#2}%
\addcontentsline{toc}{subsection}{\nameref{subsec:#2}}%
}

\usepackage{suffix}

\newcommand{\subsecref}[1]{%
subsection~``\nameref{subsec:#1}'' (\cpageref{subsec:#1})%
}
\WithSuffix\newcommand\subsecref*[1]{%
subsection~``\nameref*{subsec:#1}'' (\cpageref{subsec:#1})%
}

\begin{document}

\tableofcontents

\chapter{The Chapter}

\section{This stuff}\label{sec:stuff}

\subsec{A particular situation}{particular}

\lipsum[1]

\newpage

\subsec{Another subsection}{another}

See \subsecref{another}.

\noindent See \subsecref*{another}.

\end{document}

(输出与之前相同。)

有没有办法使用cleveref,或者以其他方式自动在交叉引用中包含“小节”(就像\cref对节所做的那样)?

答案1

定义您自己的命令:

\documentclass[12pt]{memoir}

\setsecnumdepth{subsection}
\settocdepth{subsection}
\setlength{\cftsubsectionindent}{5.25em}
\usepackage{lipsum}
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[noabbrev,capitalize]{cleveref}

\crefname{page}{page}{page}
\crefformat{specialsubsection}{#2subsection~``#1''#3}

\newcounter{specialsubsection}
\makeatletter
\newcommand{\specialsubsection}[1]{%
  \subsection*{#1}%
  \addcontentsline{toc}{subsection}{#1}%
  \refstepcounter{specialsubsection}%
  \def\cref@currentlabel{[specialsubsection][\arabic{specialsubsection}][]#1}%
  \def\@currentlabelname{#1}%
}
\makeatother

\begin{document}

\tableofcontents

\chapter{The Chapter}

\section{Whatever}

\specialsubsection{A particular situation}%
\label{subsec:particular}

\lipsum[1]

\newpage

\specialsubsection{Another subsection}
\label{subsec:another}

See \cref*{subsec:particular} (\cpageref{subsec:particular}).

\noindent See \cref{subsec:particular} (\cpageref{subsec:particular}).

\noindent See \cref{subsec:another} (\cpageref{subsec:another}).

\end{document}

内容

在此处输入图片描述

文本

在此处输入图片描述

答案2

与 egreg 的解决方案类似,使用针对 nameref 的特殊命令和“稍微”修改的\subsection命令。

\documentclass[12pt]{memoir}


\setsecnumdepth{section}
\settocdepth{subsection}
\setlength{\cftsubsectionindent}{5.25em}
\usepackage{lipsum}
\usepackage{xparse}%
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[noabbrev,capitalize]{cleveref}

\crefname{subsection}{subsection}{subsection}    % not used?
\Crefname{subsection}{Subsection}{Subsection}
\crefname{page}{page}{page}

\newcommand{\cnameref}[1]{%
  \namecref{#1}\ "\nameref{#1}" (\cpageref{#1})%
}

\newcommand{\Cnameref}[1]{%
  \nameCref{#1}\ "\nameref{#1}" (\cpageref{#1})%
}

\usepackage{letltxmacro}

\makeatletter
\LetLtxMacro\@@memoir@subsection\subsection

\RenewDocumentCommand{\subsection}{somo}{%
  \IfBooleanTF{#1}{%
    \@@memoir@subsection*{#3}%
  }{%
    \edef\@toctitle{#3}
    \IfValueT{#2}{%
      \edef\@toctitle{#2}
    }
    \refstepcounter{subsection}% Since it is unnumbered anyway ;-)
    \IfValueTF{#4}{%
      \@@memoir@subsection[\@toctitle]{#3}[#4]%
    }{%
      \@@memoir@subsection[\@toctitle]{#3}%
    }
    \edef\@currentlabelname{#3}% Set the \nameref information to #3
  }%
}

\makeatother

\setlength{\parindent}{0em}
\begin{document}

\tableofcontents

\chapter{The Chapter}

\section{This stuff}\label{sec:stuff}


\subsection{A particular situation}\label{subsec:particular}

\lipsum[1]

\newpage

\subsection{Another subsection}\label{subsec:another}

See \cnameref{subsec:particular} 

or 

\cnameref{subsec:another}

or

\Cnameref{subsec:another}

See \cnameref{sec:stuff}

\end{document}

在此处输入图片描述

相关内容