hyperref--xkeyval 交互?\reserved@a ->\@nil 错误

hyperref--xkeyval 交互?\reserved@a ->\@nil 错误

代码来自\reserved@a ->\@nil 答案在加载时也产生错误 hyperref

% !TEX TS-program = xelatexmk
\documentclass{report}
\usepackage{xkeyval}
\usepackage{hyperref}

\makeatletter
\define@boolkey{fam}[myenv@]{title}[true]{}
\define@boolkey{fam}[myenv@]{notitle}[true]{\ifmyenv@notitle\myenv@titlefalse\else\myenv@titletrue\fi}
\define@cmdkey{fam}[myenv@]{label}[\relax]{}
\define@cmdkey{fam}[myenv@]{repeat}[\relax]{}

\newcounter{mycnt}[chapter]%
\renewcommand{\themycnt}{\thechapter.\arabic{mycnt}}
\newenvironment{myenv}[1][]{%
  \setkeys{fam}{title,label,repeat,#1}%
  \ifmyenv@title
    \expandafter\ifx\myenv@repeat\relax
      \refstepcounter{mycnt}%
      \expandafter\ifx\myenv@label\relax\else\label{\myenv@label}\fi
    \else
      \renewcommand{\themycnt}{\ref{\myenv@repeat}}%
    \fi
    \noindent\textbf{Test\ \themycnt}%
    \par\nobreak\addvspace{\medskipamount}%
  \else
    \par\addvspace{\medskipamount}%
  \fi
  \edef\@currentlabel{\themycnt}%
  \noindent\ignorespaces
}{%
  \par\addvspace{\medskipamount}\ignorespacesafterend
}
\makeatother


\begin{document}

\chapter{Introduction} 
\section{A Section} 
Some text
\begin{myenv}[label={test:one}]
A: Some text within environment
\end{myenv}
As Test~\ref{test:one} demonstrates ...

\begin{myenv}[notitle]
B: Some text within environment with no title
\end{myenv}

\begin{myenv}[label={test:two}]
C: Some text within environment 
\end{myenv}
As Test~\ref{test:two} demonstrates ...

\section{Another Section}
Now let us look again at Test~\ref{test:one} already shown in the previous section:

\begin{myenv}[repeat={test:one}]
D: Some text within environment
\end{myenv}

\end{document}

有任何想法吗?

答案1

@currentlevel 的定义应该使用\protected@edef

\documentclass{report}
\usepackage{xkeyval}
\usepackage{hyperref}

\makeatletter
\define@boolkey{fam}[myenv@]{title}[true]{}
\define@boolkey{fam}[myenv@]{notitle}[true]{\ifmyenv@notitle\myenv@titlefalse\else\myenv@titletrue\fi}
\define@cmdkey{fam}[myenv@]{label}[\relax]{}
\define@cmdkey{fam}[myenv@]{repeat}[\relax]{}

\newcounter{mycnt}[chapter]%
\renewcommand{\themycnt}{\thechapter.\arabic{mycnt}}
\newenvironment{myenv}[1][]{%
  \setkeys{fam}{title,label,repeat,#1}%
  \ifmyenv@title
    \expandafter\ifx\myenv@repeat\relax
      \refstepcounter{mycnt}%
      \expandafter\ifx\myenv@label\relax\else\label{\myenv@label}\fi
    \else
      \renewcommand{\themycnt}{\ref{\myenv@repeat}}%
    \fi
    \noindent\textbf{Test\ \themycnt}%
    \par\nobreak\addvspace{\medskipamount}%
  \else
    \par\addvspace{\medskipamount}%
  \fi
  \protected@edef\@currentlabel{\themycnt}%
  \noindent\ignorespaces
}{%
  \par\addvspace{\medskipamount}\ignorespacesafterend
}
\makeatother


\begin{document}

\chapter{Introduction}
\section{A Section}
Some text
\begin{myenv}[label={test:one}]
A: Some text within environment
\end{myenv}
As Test~\ref{test:one} demonstrates ...

\begin{myenv}[notitle]
B: Some text within environment with no title
\end{myenv}

\begin{myenv}[label={test:two}]
C: Some text within environment
\end{myenv}
As Test~\ref{test:two} demonstrates ...

\section{Another Section}
Now let us look again at Test~\ref{test:one} already shown in the previous section:

\begin{myenv}[repeat={test:one}]
D: Some text within environment
\end{myenv}

\end{document}

相关内容