使用描述环境标题

使用描述环境标题

这与将描述环境与 cleveref 结合使用。我想知道是否有一个干净的解决方案来重复使用描述中的项目标题。例如,给出以下乳胶片:

\begin{description}
    \item[TEST\label{itm:test}] test
\end{description}

我需要一些宏,descriptionTitle例如\descriptionTitle{itm:test}产生TEST。我不介意TEST成为此描述项的引用,但我不想每次有人想出比 TEST 更好的名称时就替换 TEST 和其他名称。

多谢!

答案1

您可以使用hyperref并定义一个名为的标记\namedlabel

\documentclass{article}

\usepackage{float}
% definition of environment
\floatstyle{plain}
\newfloat{statement}{tbhp}{los}[section]
\floatname{statement}{Statement}

\usepackage{hyperref}
% definition of named label
\makeatletter
\def\namedlabel#1#2{\begingroup
    #2%
    \def\@currentlabel{#2}%
    \phantomsection\label{#1}\endgroup
}
\makeatother


\begin{document}

\section{Statements with float}

\begin{statement}[ht!]

\begin{description}
      \item[\namedlabel{state1}{Statement 1}] This is the first statement.
      \item[\namedlabel{state2}{Statement 2}] This is the second statement. 
      \item[\namedlabel{state3}{Statement 3}] This is the third statement
\end{description}

\caption{Statements}
\label{state:example}
\end{statement}

Reference to whole thing: See Statements \ref{state:example}.

Reference to a single statement: See  \ref{state2}. 

Note that if you refer to the float you get only the number, but if you refer to the description item itself you get the whole label.

\section{Statements without float}

\begin{description}
      \item[\namedlabel{state4}{Statement 4}] This is the fourth statement.
      \item[\namedlabel{state5}{Statement 5}] This is the fifth statement. 
\end{description}

See \ref{state4}. 

\end{document}

我将我创建的浮动环境与此(带有float包)放在一起,但正如您在第二部分看到的那样,即使没有它,它也能正常工作。

相关内容