! \@firstoftwo 的参数有一个额外的 }

! \@firstoftwo 的参数有一个额外的 }

我正在尝试编写从标签中提取和格式化信息的宏。

我收到了消息

! Argument of \@firstoftwo has an extra }.
<inserted text> 
                \par 
l.84 ... is text that refers to \fullcref{sec:foo}
                                                  . \\
? x

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{xparse}

\newcommand \pagecref [1]
   {
     \fullcref{#1} on \cpageref{#1}
   }

\newcommand \Pagecref [1]
   {
     \Fullcref{#1} on \cpageref{#1}
   }

\ExplSyntaxOn

% Remove when added to expl3
\cs_generate_variant:Nn \str_show:n {e}
\cs_generate_variant:Nn \tl_log:n {e}
\cs_generate_variant:Nn \tl_show:n {e}

% Render type # (description)
%       \begin{equation} does not retain optional parameter in \nameref{label}
\NewDocumentCommand \fullcref {m}
    {
      \fullcref:nn {\cref} {#1}
    }

\NewDocumentCommand \Fullcref {m}
    {
      \fullcref:nn {\Cref} {#1}
    }

\cs_new:Npn \fullcref:nn #1 #2
  {
%     \str_show:e {fullcref:~#2~has~\tl_count:n {#2}~tokens}
%     \str_show:e {fullcref:~#2~labels~a~\namecref{#2}}
%     \tl_show:e  {fullcref:~#2~has~environment~type~#1{#2}}
      \bool_if:nTF
        {
%         \str_if_eq_p:ee {\nameref{#2}}                {equation} ||
          \str_if_eq_p:ee {\str_range:nnn {#2} {1} {3}} {eq:} ||
          \str_if_eq_p:ee {\namecref{#2}}               {}
        }
        {
%          \str_show:n {true}
%          #1 {#2} on \cpageref{#2}
          true 
        }
        {
%           \str_show:n {false}
%           #1 {#2} (\nameref{#2}) on \cpageref{#2}
          false 
        }
  }

\ExplSyntaxOff


\newtheorem{theorem}{Theorem}[section]

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\def\definitionutorefname{Definition} % Needed for \autoref
\newtheorem{example}[theorem]{Example}

\usepackage[colorlinks,hidelinks,draft=false]{hyperref}

\usepackage{cleveref}
\usepackage[draft]{showlabels}

\begin{document}
\section{foo bear}
\label{sec:foo}
This is dummy text.

\begin{equation}
\label{eq:foo}
\end{equation}

This is text that refers to \fullcref{sec:foo}. \\

This is text that refers to \fullcref{eq:foo}. \\

This is text that refers to \fullcref{def:foo}. \\

This is text that refers to \fullcref{def:bar}. \\

This is text that refers to \pagecref{sec:foo}. \\

This is text that refers to \pagecref{eq:foo}. \\

This is text that refers to \pagecref{def:foo}. \\

This is text that refers to \pagecref{def:bar}. \\

\begin{definition}
\label{def:foo}
This is a definition without descriptive text.
\end{definition}

\Fullcref{sec:foo}. \\

\Fullcref{eq:foo}. \\

\Fullcref{def:foo}. \\

\Fullcref{def:bar}. \\

\Pagecref{sec:foo}. \\

\Pagecref{eq:foo}. \\

\Pagecref{def:foo}. \\

\Pagecref{def:bar}. \\

\begin{definition}[dummy definition]
\label{def:bar}
This is a definition with descriptive text.
\end{definition}

\end{document}

第二次尝试

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{xparse}

\newcommand \pagecref [1]
   {%
     \fullcref{#1} on \cpageref{#1}
   }

\newcommand \Pagecref [1]
   {%
     \Fullcref{#1} on \cpageref{#1}
   }

\ExplSyntaxOn

% Remove when added to expl3
\cs_generate_variant:Nn \str_if_eq_p:nn {xx}
\cs_generate_variant:Nn \str_show:n {e}
\cs_generate_variant:Nn \tl_log:n {e}
\cs_generate_variant:Nn \tl_show:n {e}

% Render type # (description)
%       \begin{equation} does not retain optional parameter in \nameref{label}
\NewDocumentCommand \fullcref {m}
    {%
      \fullcref:nn {\cref} {#1}
    }

\NewDocumentCommand \Fullcref {m}
    {%
      \fullcref:nn {\Cref} {#1}
    }

\cs_new:Npn \fullcref:nn #1 #2
  {
%      \str_show:e {fullcref:~#2~has~\clist_count:n {#2}~labels}
%     \str_show:e {fullcref:~#2~has~\tl_count:n {#2}~tokens}
%     \str_show:e {fullcref:~#2~labels~a~\namecref{#2}}
%     \tl_show:e  {fullcref:~#2~has~environment~type~#1{#2}}
      \bool_if:nTF
        {
%         \str_if_eq_p:ee {\nameref{#2}}                {equation} ||
          \str_if_eq_p:xx {\str_range:nnn {#2} {1} {3}} {eq:} ||
          \str_if_eq_p:xx {\nameref{#2}}               {}
        }
        {
%          \str_show:n {true}
%          #1 {#2} on \cpageref{#2}
          true
        }
        {
%           \str_show:n {false}
%           #1 {#2} (\nameref{#2}) on \cpageref{#2}
          false
        }
  }

\ExplSyntaxOff


\newtheorem{theorem}{Theorem}[section]

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\def\definitionutorefname{Definition} % Needed for \autoref
\newtheorem{example}[theorem]{Example}

\usepackage[colorlinks,hidelinks,draft=false]{hyperref}

\usepackage{cleveref}
\usepackage[draft]{showlabels}

\begin{document}
\section{foo bear}
\label{sec:foo}
This is dummy text.

\begin{equation}
\label{eq:foo}
\end{equation}

This is text that refers to \fullcref{sec:foo}. \\

This is text that refers to \fullcref{eq:foo}. \\

This is text that refers to \fullcref{def:foo}. \\

This is text that refers to \fullcref{def:bar}. \\

This is text that refers to \pagecref{sec:foo}. \\

This is text that refers to \pagecref{eq:foo}. \\

This is text that refers to \pagecref{def:foo}. \\

This is text that refers to \pagecref{def:bar}. \\

\begin{definition}
\label{def:foo}
This is a definition without descriptive text.
\end{definition}

\Fullcref{sec:foo}. \\

\Fullcref{eq:foo}. \\

\Fullcref{def:foo}. \\

\Fullcref{def:bar}. \\

\Pagecref{sec:foo}. \\

\Pagecref{eq:foo}. \\

\Pagecref{def:foo}. \\

\Pagecref{def:bar}. \\

\begin{definition}[dummy definition]
\label{def:bar}
This is a definition with descriptive text.
\end{definition}

\end{document}

获取

! Missing number, treated as zero.
<to be read again> 
                   \cs_set_nopar:Npx 
l.86 ... is text that refers to \fullcref{sec:foo}
                                                  . \\

使用 crossreftools 和 zref 包运行这些测试

\begin{document}
\section{foo bear}
\label{sec:foo}
This is dummy text.

\begin{equation}
\label{eq:foo}
\end{equation}

\begin{definition}[text for baz]
\label{def:baz}
This is anotehr definition with descriptive text.
\end{definition}

\crtlistoflabels
{\textbackslash}crtcrefcounter\{sec:foo\} = \crtcrefcounter{sec:foo}

{\textbackslash}crtcrefnamebylabel \{sec:foo\} = \crtcrefnamebylabel{sec:foo}

{\textbackslash}crtcrefresult\{sec:foo\} = \crtcrefresult{sec:foo}

{\textbackslash}crtcrefresult\{sec:foo\} = \crtcrefresult{sec:foo}

counter=\zref@extractdefault {sec:foo} {counter} {error}

envname=\zref@extractdefault {sec:foo} {envname} {error}

{\textbackslash}ztitleref \{sec:foo\} = \ztitleref {sec:foo}


{\textbackslash}crtcrefcounter\{def:baz\} = \crtcrefcounter{def:baz}

{\textbackslash}crtcrefnamebylabel \{def:baz\} = \crtcrefnamebylabel{def:bazi}

{\textbackslash}crtcrefresult\{def:baz\} = \crtcrefresult{def:baz}

{\textbackslash}crtcrefresult\{def:baz\} = \crtcrefresult{def:baz}

counter=\zref@extractdefault {def:baz} {counter} {error}

envname=\zref@extractdefault {def:baz} {envname} {error}

{\textbackslash}ztitleref \{def:baz\} = \ztitleref {def:baz}

\makeatother

我明白了

\crtcrefcounter{sec:foo} = section
\crtcrefnamebylabel {sec:foo} = section
\crtcrefresult{sec:foo} =
\crtcrefresult{sec:foo} =
counter=error
envname=error
\ztitleref {sec:foo} = ??
\crtcrefcounter{def:baz} = theorem
\crtcrefnamebylabel {def:baz} = UNDEFINED LABEL
\crtcrefresult{def:baz} = 1
\crtcrefresult{def:baz} = 1
counter=error
envname=error
\ztitleref {def:baz} = ??

因此,似乎 cleveref 并未得到这两个软件包的完全支持。 \crtrefcounter 的输出足以满足我的当前需求,但我仍未在 crossreftools 或 zref 中找到与 \refname 等效的可行方法。

相关内容