如何在没有章节编号的情况下引用小节编号?

如何在没有章节编号的情况下引用小节编号?

我想仅包含小节的小节编号,而不包含节编号。

我认为这个 MWE 总结了我的追求。 还有其他选择吗\ref

\documentclass{article}
\begin{document}
\section{Section A}
\section{Section B}
\section{Section C}
\subsection{Subsection 1}
\subsection{Subsection 2}
\label{subsec:Subsection}
I would like "\ref{subsec:Subsection}" to just return "2".
\end{document}

答案1

以下是基于我从《The LaTeX Companion》(第 2 版)一书中学到的技术的解决方案。它执行\renewcommand\thesubsection{\arabic{subsection}}重置计数器的表示subsection,并使用低级 LaTeX 命令\subsection@cntformat,该命令控制subsection计数器在子节级条目中的显示方式。

hyperref该解决方案在设计上与和cleveref包及其交叉引用宏兼容,例如\autoref\cref\labelcref

附录 2019/08/08:我在代码中添加了两行,以使解决方案subsubsection也能处理级标题。

在此处输入图片描述

\documentclass{article}
\usepackage{geometry} % optional

\renewcommand\thesubsection{\arabic{subsection}}
\renewcommand\thesubsubsection{\arabic{subsubsection}}
% Method proposed in "The LaTeX Companion", 2nd ed.:
\makeatletter
    \def\@seccntformat#1{\@ifundefined{#1@cntformat}%
       {\csname the#1\endcsname\space}%    default
       {\csname #1@cntformat\endcsname}}%  enable individual control
    \def\subsection@cntformat{\thesection.\thesubsection\space} 
    \def\subsubsection@cntformat{\thesection.\thesubsection.\thesubsubsection\space}
\makeatother

%Optional:
\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[noabbrev,nameinlink]{cleveref}
\crefname{subsection}{subsection}{subsections} 
\crefname{subsubsection}{subsubsection}{subsubsections} 

\begin{document}
\setlength\parindent{0pt}  % just for this example
The instructions \verb+\ref{sec:C2}+  and \verb+\labelcref{sec:C2}+ return ``\ref{sec:C2}'' and ``\labelcref{sec:C2}''.

The instructions \verb+\ref{sec:C11}+ and \verb+\labelcref{sec:C11}+ return ``\ref{sec:C11}'' and ``\labelcref{sec:C11}''.

\verb+\autoref+: \autoref{sec:C2} and \autoref{sec:C11}

And \verb+\cref+: \cref{sec:C2,sec:C11}

\addtocounter{section}{2} % just for this example
\section{Section C}
\subsection{Subsection 1}
\subsubsection{Subsubsection 1} \label{sec:C11}
\subsection{Subsection 2} \label{sec:C2}

\end{document}

相关内容