0

I am attempting to write macro that extracts and formats information from a label.

I get the messages

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

From

\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}

Second attempt

\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}

gets

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

Running these tests with the packages crossreftools and 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

I get

\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} = ??

So it appears cleveref isn't fully supported by either package. The output from \crtrefcounter is good enough for my immediate purposes, but I still haven't found a working equivalent to \refname in either crossreftools or zref.

  • the ref commands are not expandable, so can't be used in \edef (or an e argument) – David Carlisle Feb 27 at 22:42
  • If you want expandable ref commands, use the zref package. – Ulrike Fischer Feb 27 at 22:45
  • 2
    @Sebastiano please fix your edit you have changed the format of the error message from a correct code block to an incorrect quote so losing all the line breaks. – David Carlisle Feb 27 at 22:45
  • @DavidCarlisle I didn't know about this! I'll try to remedy the mistake. – Sebastiano Feb 27 at 22:47
  • unrelated to the error but you have spurious space in \newcommand \Pagecref [1] { \Fullcref{#1} on \cpageref{#1} } which would mean that you get double space before the reference in uses such as refers to \Fullcref – David Carlisle Feb 27 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.