
我正在使用 KOMAscript 的類別編寫文檔scrreprt
,並且正在編寫自訂巨集 ( \xref
) 來格式化用於交叉引用的文字。
該巨集將標籤名稱作為參數:\xref{mylabel}
我想根據標籤是指章/節/小節(預設編號)還是子小節/段落(預設未編號)來產生不同的文字。
我怎樣才能檢查這個?以下兩個中的任何一個都可以:
- 檢查標籤是否引用章節/節/小節(或相反:檢查標籤是否引用子小節/段落)
- 檢查標籤是否涉及編號項目
答案1
unnumbered/numbered
請參閱最後引用結構單元和檢查的更新。
在不使用任何額外套件的情況下,僅\renewcommand, \let, \pdfstrcmp
使用 etc. 並將 a 寫入\@namedef
檔案.aux
。
它需要兩次運行(無論如何都是需要的,因為我們正在處理標籤!)
\extractlabeltype{labelname}
將and\checklabeltype
與 true/false 分支一起使用。
它假設沒有其他包涉及\label
and \ref
,即不支持hyperref
or 。cleveref
\documentclass{book}
\makeatletter
\let\latex@@refstepcounter\refstepcounter
\let\latex@@label\label%
\renewcommand{\refstepcounter}[1]{%
\gdef\lastrefsteppedcounter{#1}%
\latex@@refstepcounter{#1}%
}
\renewcommand{\label}[1]{%
\immediate\write\@auxout{\string\global\string\@namedef{label#1}{\lastrefsteppedcounter}}
\latex@@label{#1}%
}
\newcommand{\extractlabeltype}[1]{%
\@nameuse{label#1}%
}
\makeatother
\newcommand{\checklabeltype}[4]{%
\ifnum0=\pdfstrcmp{\extractlabeltype{#1}}{#2}
#3%
\else
#4%
\fi
}
\begin{document}
\chapter{Foo} \label{foo}
\section{Foosection}\label{foosection}
\checklabeltype{foosection}{section}{Yes, it is section}{No, it is something different}
\checklabeltype{foo}{section}{Yes, it is section}{No, it is something different}
\end{document}
更新檢查未編號/編號的問題。
請注意,這故意依賴未編號結構單元的錨名稱中包含 a 的假設*
,因此使用\theH...
巨集並不是一個好主意;-)
\documentclass{book}
\usepackage{xparse}
\usepackage[hyperref,counter]{zref}% Using the counter mechanism behind `nameref`
\usepackage{hyperref}
\makeatletter
\AtBeginDocument{%
\let\latex@@label\label%
\renewcommand{\label}[1]{%
\zref@label{#1}%
\latex@@label{#1}%
}
% Get the underlying counter type
\newcommand{\extractlabelcounter}[1]{%
\zref@ifrefundefined{#1}{%
???????}{%
\zref@extract{#1}{counter}%
}%
}
% Get the anchor name for hyperref or nameref -> has a `*` inside if it is unnumbered
\newcommand{\extractlabelanchor}[1]{%
\zref@ifrefundefined{#1}{%
???????}{%
\zref@extract{#1}{anchor}%
}%
}
}
% Check if there's a `*` inside of the anchor name
\ExplSyntaxOn
\cs_new:Npn \checkifnumbered#1#2#3{%
\tl_set:Nx \l_tmpa_tl {\extractlabelanchor{#1}}
\tl_if_in:NnTF \l_tmpa_tl {*} {#2} {#3}
}
\ExplSyntaxOff
\makeatother
\newcommand{\checklabeltype}[4]{%
\ifnum0=\pdfstrcmp{\extractlabelcounter{#1}}{#2}
#3%
\else
#4%
\fi
}
\begin{document}
\chapter{Foo} \label{foo}
\section*{An unnumbered section} \label{unnumbered}
\section{Foosection}\label{foosection}
\checklabeltype{foosection}{section}{Yes, it is section}{No, it is something different}
\checklabeltype{foo}{section}{Yes, it is section}{No, it is something different}
\begin{enumerate}
\item First \label{enumfirst}
\item Second \label{enumsecond}
\end{enumerate}
\checklabeltype{enumsecond}{enumi}{It is a numbered item and has the value \ref{enumsecond}}{}
In \nameref{unnumbered} we have an \checkifnumbered{unnumbered}{unnumbered}{numbered} \extractlabelcounter{unnumbered}
In \nameref{foo} we have an \checkifnumbered{foo}{unnumbered}{numbered} \extractlabelcounter{foo} whereas
\nameref{foosection} is a \checkifnumbered{foosection}{unnumbered}{numbered} \extractlabelcounter{foosection}.
\end{document}