获取标签目标类型

获取标签目标类型

我正在寻找上一个问题的解决方案“相关章节引用”,并发现自己很难从标签本身获取标签的目标“类型”。

请允许我澄清一下:我想确定给定的标签是否指向章节、节、小节、图表、表格等。

有什么想法吗?(因为我很直白)

PS:解决方案应与hyperref包裹至少。

答案1

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{hyperref}

\makeatletter

% Helper macro to extract the type (section,subsection...) or the type name
% out of the label reference. Works with hyperref only.
% Argument #1 is a macro of form \def\...#1...\@nil{...}
% Argument #2 is the label reference, e.g. "sect:test"
\newcommand*\@myautoref[2]{% \HyPsd@@@autoref from hyperref, modified
  \expandafter\ifx\csname r@#2\endcsname\relax
    ??%
  \else
    \expandafter\expandafter\expandafter\@@myautoref
        \csname r@#2\endcsname{}{}{}{}\@nil#1\@nil
  \fi
}
\def\@@myautoref#1#2#3#4#5\@nil#6\@nil{% \HyPsd@autorefname, modified
  #6#4.\@nil}% Argument #4 = type and number, e.g. "section.1" or "subsection.1.2"

% \myreftype results in the type name, e.g. "section" or "figure".
% The starred variant will remove a star, if existent, i.e. "section*" will become "section"
\newcommand\myreftype{%
  \@ifstar
    {\@myautoref\@@myreftype}%
    {\@myautoref\@myreftype}}
\def\@myreftype#1.#2\@nil{#1}
\def\@@myreftype#1.#2\@nil{\@@@myreftype#1*\@nil}
\def\@@@myreftype#1*#2\@nil{#1}

% \myautorefname results in the type prose name (plus space character),
% e.g. "section" in English or "Abschnitt" in German
% (like \autoref, but without number).
% \HyPsd@@autorefname is defined in the hyperref package.
\newcommand*\myautorefname[1]{\@myautoref\HyPsd@@autorefname{#1}}

% An alternative version of \myautorefname without space at the end.
% Since the \space is hard coded inside \HyPsd@@autorefname we use our
% own version called \@myautorefname instead.
% Furthermore we offer a starred variant which will work with labels to
% \section* etc., too.
\renewcommand*\myautorefname{%
  \@ifstar
    {\@myautoref\@@myautorefname}%
    {\@myautoref\@myautorefname}}
\def\@myautorefname#1.#2\@nil{% = \HyPsd@@autorefname without \space
  \ltx@IfUndefined{#1autorefname}%
    {\ltx@IfUndefined{#1name}%
      {}%
      {\csname#1name\endcsname}}%
    {\csname#1autorefname\endcsname}}
\def\@@myautorefname#1.#2\@nil{%
  \expandafter\@myautorefname\@@@myreftype#1*\@nil.\@nil}

\makeatother

\begin{document}

\myreftype{section:one}
\myreftype{subsection:one}
\myreftype{figure:one}
\myreftype{section:two}

\myreftype*{section:one}
\myreftype*{subsection:one}
\myreftype*{figure:one}
\myreftype*{section:two}

\myautorefname{section:one}
\myautorefname{subsection:one}
\myautorefname{figure:one}
\myautorefname{section:two}

\myautorefname*{section:one}
\myautorefname*{subsection:one}
\myautorefname*{figure:one}
\myautorefname*{section:two}

\section{Section One}
\label{section:one}

\subsection{Subsection One}
\label{subsection:one}

\begin{figure}
\caption{Figure One}
\label{figure:one}
\end{figure}

\section*{Section Two}
\label{section:two}

\end{document}

上面的代码是我曾经为德国 LaTeX 论坛编写的解决方案的摘录,请参阅:http://www.mrunix.de/forums/showthread.php?t=71566

请注意,这个仅使用了 的功能hyperref。我不熟悉 或 之类的软件包variorefcleverref因此使用其中一个软件包可能会有更好的解决方案。

相关内容