如何在沒有節號的情況下引用小節號?

如何在沒有節號的情況下引用小節號?

我想只包含小節的小節編號,而不包含小節編號。

我認為這個 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》(第二版)一書中學到的技術的解決方案。它執行\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}

相關內容