我想创建一个命令小节应被引用并可索引。该命令有两个参数:代码和名称。
该命令创建子部分和姓名(第二个参数)在目录中绘制。
但当我尝试使用代码作为参考,它写下了不同的(第一个)小节的名称!!
这是我的不是工作代码:
\documentclass{article}
\usepackage{hyperref}
\hypersetup{
colorlinks=true,
linkcolor=blue,
filecolor=magenta,
urlcolor=blue,
}
\newcommand{\virtualSubsec}[3]{
\phantomsection
\par\nopagebreak
\refstepcounter{subsection}
\def\@currentlabelname{#1}
\addcontentsline{toc}{subsection}{\protect\numberline{\thesubsection}#2}
\label{#1}
\textbf{#1 - #2}
\par
\textit{#3}
\par
}
\begin{document}
\tableofcontents
\section{This is a Section}
\subsection{This is a Subsection}
A line of text...
\virtualSubsec{VS01}{Virtual Subsec 1}{
this is the content of the first virtual subsection
}
\virtualSubsec{VS02}{Virtual Subsec 2}{
this is the content of the second virtual subsection
}
\section{References}
autoref VS02 is working: \par
\autoref{VS02} \par %true => is subsection 1.2
nameref VS02 is NOT working: \par
\nameref{VS02} \par
it is painting "This is a Subsection" => false => it should be "Virtual Subsec 1"
\end{document}
问题是什么??
答案1
您必须先将 @ 设为字母。没有它,您就是在定义\@
而不是\@currentlabelname
。此外,您使用了错误的参数编号:
\makeatletter %<-- make @ a letter
\newcommand{\virtualSubsec}[3]{
\phantomsection
\par\nopagebreak
\refstepcounter{subsection}
\def\@currentlabelname{#2}%<------ #2
\addcontentsline{toc}{subsection}{\protect\numberline{\thesubsection}#2}
\label{#1}
\textbf{#1 - #2}
\par
\textit{#3}
\par
}
\makeatother