交叉引用章节标题中的计数器

交叉引用章节标题中的计数器

我正在尝试使用 Latex 中的自定义计数器来自动对章节标题中的用例进行编号(例如\section{UC <counter>: use case title})。我还想要一个\usecaseref命令来用完整的用例名称替换引用:UCx:用例标题

这是我目前得到的:

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage{hyperref}

\newcounter{UCcounter}

\newcommand{\usecaseprint}[1]{UC \arabic{UCcounter}: #1}

\newcommand{\usecaseincrease}{\refstepcounter{UCcounter}}

\newcommand{\usecaseref}[1]{\emph{\nameref{#1}}}

\begin{document}

\usecaseincrease
\section{\usecaseprint{First Use Case Name}\label{sec:first}}

    This is the forwards reference: \usecaseref{sec:second}.

\usecaseincrease
\section{\usecaseprint{Second Use Case Name} \label{sec:second} }

    This is the backwards reference: \usecaseref{sec:first}

\end{document}

但这会产生以下结果:

在此处输入图片描述

如您所见,在调用命令时\nameref,它使用的是当前值,UCcounter而不是部分标题中的计数器的值。

我已经尝试用 替换\newcommand出现的情况\DeclareRobustCommand,但这并没有改变任何事情。

答案1

这是一个‘绕过’的方法,通过\newlabel向文件写入直接命令来使用黑客技术.aux

这是经过测试的 hyperref 仅用于便携目的!

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage{hyperref}

\newcounter{UCcounter}
\setcounter{UCcounter}{0}

% Change to arabic counter format 
\renewcommand{\theUCcounter}{\arabic{UCcounter}}


\newcommand{\usecaseref}[1]{\emph{\nameref{#1}}}

% Change behaviour (except calling of \theUCcounter) etc. at will 
\newcommand{\usecaseprint}[1]{%
UC \theUCcounter: #1}%


\let\LaTeXStandardSection\section


\renewcommand{\section}[1]{%
\refstepcounter{UCcounter}%
\LaTeXStandardSection{\usecaseprint{#1}}%    
% Bypass the normal label command with hyperrefs label style -- this uses the counter value at the
% moment the section is begun, not afterwards!
\immediate\write1{%  Directly write to aux - file -- I hope, this is portable
\string\newlabel{sec::uc::\theUCcounter}{{\thesection}{\thepage}{\usecaseprint{#1}}{section.\thesection}{}}}%
}%

\begin{document}



\section{First Use Case Name}

    This is the forwards reference: \usecaseref{sec::uc::5}

\section{Second Use Case Name}
    This is the backwards reference: \usecaseref{sec::uc::1}


\section{Third Use Case Name}
    This is the backwards reference: \usecaseref{sec::uc::2}

\newpage

\section{Fourth Use Case Name}%\label{sec:second}
    This is  yet another forward reference: \usecaseref{sec::uc::6}


\section{Fifth Use Case Name}%\label{sec:second}
    This is  yet another backwards reference: \usecaseref{sec::uc::3}

\section{Sixth Use Case Name}%\label{sec:second}
    This is  yet another backwards reference: \usecaseref{sec::uc::1}


\end{document}

我重新定义了该\section命令,但目前它只能使用其默认参数!

为了方便起见,我还改为使用自动标签。 在此处输入图片描述

答案2

谢谢!

我改进了 Christian H. 的解决方案,以便能够使用普通部分。因此,我创建了一个新命令,而不是重新定义该\section命令。此命令还将标签名称作为第二个参数,因此我可以通过逻辑名称而不是数字来引用。这是最终的解决方案:

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage{hyperref}

\newcounter{UCcounter}
\setcounter{UCcounter}{0}

% Change to arabic counter format 
\renewcommand{\theUCcounter}{\arabic{UCcounter}}

\newcommand{\usecaseref}[1]{\emph{\nameref{#1}}}

% Change behaviour (except calling of \theUCcounter) etc. at will 
\newcommand{\usecaseprint}[1]{%
UC \theUCcounter: #1}%

% Define a new command for section titles with Use Cases
% First argument = Use Case Name; second argument = label
\newcommand{\UCsection}[2]{%
\refstepcounter{UCcounter}%
\section{\usecaseprint{#1}}%    
% Bypass the normal label command with hyperrefs label style -- this uses the counter value at the
% moment the section is begun, not afterwards!
\immediate\write1{%  Directly write to aux - file -- I hope, this is portable
\string\newlabel{uc:#2}{{\thesection}{\thepage}{\usecaseprint{#1}}{section.\thesection}{}}}%
}%

\begin{document}



\UCsection{First Use Case Name}{first}

    This is the forwards reference: \usecaseref{uc:fifth}

\UCsection{Second Use Case Name}{sec}
    This is the backwards reference: \usecaseref{uc:first}


\section{Normal Section}

\UCsection{Third Use Case Name}{third}
    This is the backwards reference: \usecaseref{uc:sec}

\newpage

\section{Another Normal Section}

\UCsection{Fourth Use Case Name}{fourth}
    This is  yet another forward reference: \usecaseref{uc:six}


\UCsection{Fifth Use Case Name}{fifth}
    This is  yet another backwards reference: \usecaseref{uc:third}

\section{Yet another normal section}

\UCsection{Sixth Use Case Name}{six}
    This is  yet another backwards reference: \usecaseref{uc:first}


\end{document}

得出的结果为:

在此处输入图片描述

相关内容