特定の\refが参照するセクションの種類を確認する方法

特定の\refが参照するセクションの種類を確認する方法

私は KOMAscript のクラスを使用してドキュメントを作成しており、相互参照に使用されるテキストをフォーマットするためのscrreprtカスタム マクロ ( ) を作成しています。\xref

このマクロはラベル名を引数として受け取ります。\xref{mylabel}

ラベルが章/セクション/サブセクション (デフォルトでは番号付き) を参照しているか、サブサブセクション/段落 (デフォルトでは番号なし) を参照しているかに応じて、異なるテキストを生成したいと思います。

これをどうやって確認すればいいでしょうか? 次の 2 つのいずれかで確認できます。

  1. ラベルが章/節/サブセクションを参照しているかどうかを確認します(またはその逆:ラベルがサブサブセクション/段落を参照しているかどうかを確認します)
  2. ラベルが番号付き項目を参照しているかどうかを確認する

答え1

unnumbered/numbered構造単位の参照と最後の確認についてはアップデートを参照してください。

追加のパッケージを使用せずに、などのみを使用して、ファイルに\renewcommand, \let, \pdfstrcmp書き込みます。\@namedef.aux

2 回の実行が必要です (ラベルを扱っているので、いずれにしても必要です)。

true/false ブランチで\extractlabeltype{labelname}and を使用します。\checklabeltype

および に関連する他のパッケージがない、つまり または がサポートされていないことを前提とし\label\refhyperrefます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}

ここに画像の説明を入力してください

アップデート番号なし/番号付きの問題をチェックします。

これは、番号なし構造単位のアンカー名に が含まれているという前提に意図的に依存しているため*、マクロをいじるのは\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}

ここに画像の説明を入力してください

関連情報