相同的代码适用于其他类型的显示方程式,但不适用于“多行”

相同的代码适用于其他类型的显示方程式,但不适用于“多行”

我正在测试cleveref-usedon显示方程的能力。以下是我的测试文档:

\documentclass{article}
\usepackage{mathtools}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}

\usepackage[UsedByAndOn]{cleveref-usedon}

\usepackage{todonotes}
\SetUsedByAndOnText{Referenced by #1 on #2.}
\SetUsedByAndOnMessage
  {%
    \todo[%
      inline,
      size=\scriptsize,
      color=blue!40,
      backgroundcolor=white,
      textcolor=darkgray,
      bordercolor=darkgray,
      prepend]{%
      \textup{\UsedByAndOnText{#1}{#2}}%
    }%
  }
\SetUsedByAndOnMessageOutsideMath
  {%
    \todo[%
      inline,
      size=\scriptsize,
      color=blue!40,
      backgroundcolor=white,
      textcolor=blue!50!cyan,
      bordercolor=blue!50!cyan,
      prepend]{%
      \textup{\UsedByAndOnText{#1}{#2}}%
    }%
  }

\begin{document}

\begin{theorem}\label{thm}
  \cref{eq},
  \cref{multline:test}
\end{theorem}

\begin{lemma}
  \cref{thm}, \cref{eq}
  \cref{multline:test}
\end{lemma}

\begin{equation}
  \label{eq}
  \int x
\end{equation}

\begin{multline}\label{multline:test}
  1+1+1+1+1+1+1+1+1+1+1+1+1+1+1\\
  +1+1+1+1+1+1+1+1+1+1+1+1+1+1+1
\end{multline}

\end{document}

因为equation我得到了正确的输出(蓝色框):

在此处输入图片描述

我也尝试了dmath“breqn”、、alignflalign,这些功能全部或至少部分起作用,但什么也没发生。请问为什么 的行为不同,有什么方法可以解决这个问题吗alignatgathermultlinemultline


下面是我修改的版本cleveref-usedon,使其适用于(编号)显示的方程式。

下面的代码与已经发布的代码略有修改这个 GitHub 问题,我正在尝试将其扩展以用于其他类型的显示方程。

\def\UsedOnPackageVersion{0.4.0x}
\def\UsedOnPackageDate{2023-10-19}
\NeedsTeXFormat{LaTeX2e}[2021-06-01]
\ProvidesExplPackage{cleveref-usedon}
    {\UsedOnPackageDate}
    {\UsedOnPackageVersion}
    {Patches the cleveref package and adds forward-referencing functionality}
%%
\providecommand\IfFormatAtLeastTF{\@ifl@t@r\fmtversion}
\IfFormatAtLeastTF{2021-06-01}{%
    % LaTeX2e version new enough
}{%
    \PackageError{cleveref-usedon}{%
        Mismatched~LaTeX~support~files~detected.\MessageBreak
        Your~LaTeX~format~is~dated~\fmtversion,\MessageBreak
        but~the~package~cleveref-usedon\MessageBreak
        requires~at~least~2021-06-01.\MessageBreak
        Update~your~TeX~distribution.\MessageBreak
        \MessageBreak
        Loading~cleveref-usedon~will~abort!}%
        {Update~your~TeX~distribution~using~your~TeX~package~manager.}%
}
\providecommand\IfExplAtLeastTF{\@ifl@t@r\ExplLoaderFileDate}
\RequirePackage{expl3}[2021-05-16]
\IfExplAtLeastTF{2021-05-16}{%
    % expl3 version new enough
}{%
    \PackageError{cleveref-usedon}{%
        Support~package~expl3~too~old.\MessageBreak
        The~L3~programming~layer~in~the~LaTeX~format\MessageBreak
        is~dated~\ExplLoaderFileDate,\MessageBreak
        but~the~package~cleveref-usedon\MessageBreak
        requires~at~least~2021-05-16.\MessageBreak
        Update~your~TeX~distribution.\MessageBreak
        \MessageBreak
        Loading~cleveref-usedon~will~abort!}%
        {Update~your~TeX~distribution~using~your~TeX~package~manager.}%
}

\keys_define:nn { cleveref-usedon }
  {
    , Default     .tl_set:N   = \l__UsedOn_default_tl
    , Default     .initial:n  = { }
    , Default     .value_required:n = true
    , UsedOn      .meta:n     = { Default = UsedOn }
    , usedon      .meta:n     = { Default = UsedOn }
    , uo          .meta:n     = { Default = UsedOn }
    , UsedBy      .meta:n     = { Default = UsedBy }
    , usedby      .meta:n     = { Default = UsedBy }
    , ub          .meta:n     = { Default = UsedBy }
    , UsedByAndOn .meta:n     = { Default = UsedByAndOn }
    , usedbyandon .meta:n     = { Default = UsedByAndOn }
    , ubao        .meta:n     = { Default = UsedByAndOn }

    , unknown    .code:n     =
        { \PassOptionsToPackage { \CurrentOption } { cleveref } }
  }
\ProcessKeyOptions [ cleveref-usedon ]

\RequirePackage{cleveref}[2018/03/27]
\newcommand*{\@setcpagerefrange}[3]{%
    \@@setcpagerefrange{#1}{#2}{cref}{#3}}
\newcommand*{\@setCpagerefrange}[3]{%
    \@@setcpagerefrange{#1}{#2}{Cref}{#3}}
\newcommand*{\@setlabelcpagerefrange}[3]{%
    \@@setcpagerefrange{#1}{#2}{labelcref}{#3}}
\prg_generate_conditional_variant:Nnn \str_case:nn { x } { T, TF }
\cs_generate_variant:Nn \str_case:nn { x }
\seq_new:N \g__UsedOn_k_seq
\prop_new:N \g__UsedOn_kv_prop
\clist_new:N \g__UsedOn_Options_clist
\clist_set:Nn \g__UsedOn_Options_clist { UsedOn, UsedBy, UsedByAndOn }
\cs_new:Nn \__UsedOn_UsedOnMessage:n { }
\cs_new:Nn \__UsedOn_UsedByMessage:n { }
\cs_new:Nn \__UsedOn_UsedByAndOnMessage:nn { }
\NewDocumentCommand \UsedOnText { m } { \__UsedOn_UsedOnMessage:n { #1 } }
\NewDocumentCommand \UsedByText { m } { \__UsedOn_UsedByMessage:n { #1 } }
\NewDocumentCommand \UsedByAndOnText { m m } { \__UsedOn_UsedByAndOnMessage:nn { #1 } { #2 } }
\NewDocumentCommand \SetUsedOnText { m }
  {
    \cs_set:Nn \__UsedOn_UsedOnMessage:n { #1 }
  }
\NewDocumentCommand \SetUsedByText { m }
  {
    \cs_set:Nn \__UsedOn_UsedByMessage:n { #1 }
  }
\NewDocumentCommand \SetUsedByAndOnText { m }
  {
    \cs_set:Nn \__UsedOn_UsedByAndOnMessage:nn { #1 }
  }

\RequirePackage{iflang}

\SetUsedOnText
  {
    \IfLanguageName{english}
        { (Used~on~#1.) } {}
    \IfLanguageName{french}
        { (Apparaît~en~#1.) } {}
    \IfLanguageName{ngerman}
        { (Wird~auf~#1.) } {}
    \IfLanguageName{spanish}
        { (Aparece~en~#1.) } {}
  }
\SetUsedByText
  {
    \IfLanguageName{english}
        { (Used~by~#1.) } {}
    \IfLanguageName{french}
        { (Apparaît~dans~#1.) } {}
    \IfLanguageName{ngerman}
        { (Wird~in~#1.) } {}
    \IfLanguageName{spanish}
        { (Aparece~en~#1.) } {}
  }
\SetUsedByAndOnText
  {
    \IfLanguageName{english}
        { (Used~by~#1~on~#2.) } {}
    \IfLanguageName{french}
        { (Apparaît~dans~#1~en~#2.) } {}
    \IfLanguageName{ngerman}
        { (Wird~in~#1~auf~#2.) } {}
    \IfLanguageName{spanish}
        { (Aparece~en~#1~en~#2.) } {}
  }

\NewDocumentCommand \UsedOnMessage { m }
  {
    \emph{ \__UsedOn_UsedOnMessage:n { #1 } } \\[.3\baselineskip]
  }
\NewDocumentCommand \UsedByMessage { m }
  {
    \emph{ \__UsedOn_UsedByMessage:n { #1 } } \\[.3\baselineskip]
  }
\NewDocumentCommand \UsedByAndOnMessage { m m }
  {
    \emph{ \__UsedOn_UsedByAndOnMessage:nn { #1 } { #2 } } \\[.3\baselineskip]
  }

\def\UsedOnMessageMath{}
\def\UsedByMessageMath{}
\def\UsedByAndOnMessageMath{}
\NewDocumentCommand \UsedOnMessageInsideMath { m }
  {
    % By default does nothing
  }
\NewDocumentCommand \UsedByMessageInsideMath { m }
  {
    % By default does nothing
  }
\NewDocumentCommand \UsedByAndOnMessageInsideMath { m m }
  {
    % By default does nothing
  }
\NewDocumentCommand \UsedOnMessageOutsideMath { m }
  {
    % By default does nothing
  }
\NewDocumentCommand \UsedByMessageOutsideMath { m }
  {
    % By default does nothing
  }
\NewDocumentCommand \UsedByAndOnMessageOutsideMath { m m }
  {
    % By default does nothing
  }

\tl_new:N \g__UsedOn_tmpa_tl
\NewDocumentCommand \SetUsedOnMessage { m }
  {
    \RenewDocumentCommand \UsedOnMessage { m } { #1 }
  }
\NewDocumentCommand \SetUsedByMessage { m }
  {
    \RenewDocumentCommand \UsedByMessage { m } { #1 }
  }
\NewDocumentCommand \SetUsedByAndOnMessage { m }
  {
    \RenewDocumentCommand \UsedByAndOnMessage { m m } { #1 }
  }
\bool_new:N \g__UsedOn_inside_math_bool
\NewDocumentCommand \SetUsedOnMessageInsideMath { m }
  {
    \bool_set_true:N \g__UsedOn_inside_math_bool
    \RenewDocumentCommand \UsedOnMessageInsideMath { m } { #1 }
  }
\NewDocumentCommand \SetUsedByMessageInsideMath { m }
  {
    \bool_set_true:N \g__UsedOn_inside_math_bool
    \RenewDocumentCommand \UsedByMessageInsideMath { m } { #1 }
  }
\NewDocumentCommand \SetUsedByAndOnMessageInsideMath { m }
  {
    \bool_set_true:N \g__UsedOn_inside_math_bool
    \RenewDocumentCommand \UsedByAndOnMessageInsideMath { m m } { #1 }
  }

\clist_new:N \l__UsedOn_supported_equation_env_clist
\clist_set:Nn \l__UsedOn_supported_equation_env_clist
  {
    equation,
    align, flalign, alignat,
    gather,
    multline,
    dmath,
  }
\cs_new:Nn \__UsedOn_message_outsidemath_hook_gremove_code:nn
  {
    \clist_map_inline:Nn \l__UsedOn_supported_equation_env_clist
      {
        \hook_gremove_code:nn { env/##1/#1 } { cleveref-usedon- #2 -message-outsidemath }
      }
  }
\cs_new:Nn \__UsedOn_message_outsidemath_hook_gput_code:nnn
  {
    \clist_map_inline:Nn \l__UsedOn_supported_equation_env_clist
      {
        \hook_gput_code:nnn { env/##1/#1 } { cleveref-usedon- #2 -message-outsidemath } { #3 }
      }
  }
\cs_new:Nn \__UsedOn_message_outsidemath_hook_gset_rule:nnnn
  {
    \clist_map_inline:Nn \l__UsedOn_supported_equation_env_clist
      {
        \hook_gset_rule:nnnn { env/##1/#1 } { cleveref-usedon- #2 -message-outsidemath } { #3 } { cleveref-usedon- #4 -message-outsidemath }
      }
  }
\__UsedOn_message_outsidemath_hook_gput_code:nnn { before } { UsedOn }      { }
\__UsedOn_message_outsidemath_hook_gput_code:nnn { before } { UsedBy }      { }
\__UsedOn_message_outsidemath_hook_gput_code:nnn { before } { UsedByAndOn } { }
\__UsedOn_message_outsidemath_hook_gput_code:nnn { after }  { UsedOn }      { }
\__UsedOn_message_outsidemath_hook_gput_code:nnn { after }  { UsedBy }      { }
\__UsedOn_message_outsidemath_hook_gput_code:nnn { after }  { UsedByAndOn } { }
\cs_new:Nn \__UsedOnMessageOutsideMath:n {}
\NewDocumentCommand \SetUsedOnMessageOutsideMath { m }
  {
    \bool_set_false:N \g__UsedOn_inside_math_bool
    \RenewDocumentCommand \UsedOnMessageOutsideMath { m }
      {
        \__UsedOn_message_outsidemath_hook_gset_rule:nnnn { after } { UsedOn } { voids } { UsedBy }
        \__UsedOn_message_outsidemath_hook_gset_rule:nnnn { after } { UsedOn } { voids } { UsedByAndOn }
        \tl_gset:Ne \g__UsedOn_tmpa_tl { ##1 }
      }
    \cs_set:Nn \__UsedOnMessageOutsideMath:n
      {
        \bool_if:NF \g__UsedOn_no_use_bool
          {
            \tl_if_empty:NF \g__UsedOn_tmpa_tl
              {
                #1
              }
          }
      }
    \__UsedOn_message_outsidemath_hook_gremove_code:nn { before } { UsedOn }
    \__UsedOn_message_outsidemath_hook_gput_code:nnn { before } { UsedOn }
      {
        \tl_gclear:N \g__UsedOn_tmpa_tl
      }
    \__UsedOn_message_outsidemath_hook_gremove_code:nn { after } { UsedOn }
    \__UsedOn_message_outsidemath_hook_gput_code:nnn { after } { UsedOn }
      {
        \__UsedOnMessageOutsideMath:n { \tl_use:N \g__UsedOn_tmpa_tl }
      }
  }
\cs_new:Nn \__UsedByMessageOutsideMath:n {}
\NewDocumentCommand \SetUsedByMessageOutsideMath { m }
  {
    \bool_set_false:N \g__UsedOn_inside_math_bool
    \RenewDocumentCommand \UsedByMessageOutsideMath { m }
      {
        \__UsedOn_message_outsidemath_hook_gset_rule:nnnn { after } { UsedBy } { voids } { UsedOn }
        \__UsedOn_message_outsidemath_hook_gset_rule:nnnn { after } { UsedBy } { voids } { UsedByAndOn }
        \tl_gset:Ne \g__UsedOn_tmpa_tl { ##1 }
      }
    \cs_set:Nn \__UsedByMessageOutsideMath:n
      {
        \bool_if:NF \g__UsedOn_no_use_bool
          {
            \tl_if_empty:NF \g__UsedOn_tmpa_tl
              {
                #1
              }
          }
      }
    \__UsedOn_message_outsidemath_hook_gremove_code:nn { before } { UsedBy }
    \__UsedOn_message_outsidemath_hook_gput_code:nnn { before } { UsedBy }
      {
        \tl_gclear:N \g__UsedOn_tmpa_tl
      }
    \__UsedOn_message_outsidemath_hook_gremove_code:nn { after } { UsedBy }
    \__UsedOn_message_outsidemath_hook_gput_code:nnn { after } { UsedBy }
      {
        \__UsedByMessageOutsideMath:n { \tl_use:N \g__UsedOn_tmpa_tl }
      }
  }
\cs_new:Nn \__UsedByAndOnMessageOutsideMath:nn {}
\NewDocumentCommand \SetUsedByAndOnMessageOutsideMath { m }
  {
    \bool_set_false:N \g__UsedOn_inside_math_bool
    \RenewDocumentCommand \UsedByAndOnMessageOutsideMath { m m }
      {
        \__UsedOn_message_outsidemath_hook_gset_rule:nnnn { after } { UsedByAndOn } { voids } { UsedOn }
        \__UsedOn_message_outsidemath_hook_gset_rule:nnnn { after } { UsedByAndOn } { voids } { UsedBy }
        \tl_gset:Ne \g__UsedOn_tmpa_tl { ##1 }
        \tl_gset:Ne \g__UsedOn_tmpb_tl { ##2 }
      }
    \cs_set:Nn \__UsedByAndOnMessageOutsideMath:nn
      {
        \bool_if:NF \g__UsedOn_no_use_bool
          {
            \tl_if_empty:NF \g__UsedOn_tmpa_tl
              {
                #1
              }
          }
      }
    \__UsedOn_message_outsidemath_hook_gremove_code:nn { before } { UsedByAndOn }
    \__UsedOn_message_outsidemath_hook_gput_code:nnn { before } { UsedByAndOn }
      {
        \tl_gclear:N \g__UsedOn_tmpa_tl
        \tl_gclear:N \g__UsedOn_tmpb_tl
      }
    \__UsedOn_message_outsidemath_hook_gremove_code:nn { after } { UsedByAndOn }
    \__UsedOn_message_outsidemath_hook_gput_code:nnn { after } { UsedByAndOn }
      {
        \__UsedByAndOnMessageOutsideMath:nn { \tl_use:N \g__UsedOn_tmpa_tl } { \tl_use:N \g__UsedOn_tmpb_tl }
      }
  }

\cs_new:Nn \__UsedOn_Printer_generic:nnn
  {
%%    % Check if the reference #1@<LabelName>@1 exists
%%    % Here the @1 means that <LabelName> has been referenced
%%    % with option #1 at least once where #1 is
%%    % 'UsedOn', 'UsedBy' or 'UsedByAndOn'
    \cs_if_exist:cT {r@#1@#2@1}
      {
%%        % In a tmp clist we store all the references of the form
%%        %    `<Option>@<LabelName>@<Number>`
%%        % where Number between 1 and \value{LastRun@UsedOn@<LabelName>}
%%        % if the latter exists, otherwise until 1
%%        % Should/will normally need two consecutive runs of pdflatex
        \cs_if_free:cTF {c@LastRun@#1@#2}
          { \int_set:Nn \l_tmpa_int { 1 } }
          { \int_set:Nn \l_tmpa_int { \value{LastRun@#1@#2} } }
        \int_set:Nn \l_tmpb_int { 1 }
        \int_while_do:nn { \l_tmpb_int <= \l_tmpa_int }
          {
            \clist_put_right:Nx \l_tmpa_clist { #1@#2@\int_use:N \l_tmpb_int }
            \int_incr:N \l_tmpb_int
          }
        #3
    }
  }
\NewDocumentCommand \__UsedOn_Printer { m m }
  {
    \__UsedOn_Printer_generic:nnn { #1 } { #2 }
      {
%%        % Print `UsedOn` message by calling \cpageref with the parameter clist above
%%        % Uncomment the next two lines for debugging to see the contents of \l_tmpa_clist
%%        %%   Arguments~of~cpageref/cref~are:
%%        %%   \par\clist_use:Nn \l_tmpa_clist {\par}\par
        \str_case:xn { \str_foldcase:n { #1 } }
          {
            {usedon}
              { \UsedOnMessage { \cpageref{\l_tmpa_clist} } }
            {usedby}
              { \UsedByMessage { \cref{\l_tmpa_clist} } }
            {usedbyandon}
              { \UsedByAndOnMessage { \cref{\l_tmpa_clist} } { \cpageref{\l_tmpa_clist} } }
          }
      }
  }
\NewDocumentCommand \__UsedOn_Printer_Math { m m }
  {
    \__UsedOn_Printer_generic:nnn { #1 } { #2 }
      {
%%        % Print `UsedOn` message by calling \cpageref with the parameter clist above
%%        % Uncomment the next two lines for debugging to see the contents of \l_tmpa_clist
%%        %%   Arguments~of~cpageref/cref~are:
%%        %%   \par\clist_use:Nn \l_tmpa_clist {\par}\par
        \bool_if:NTF \g__UsedOn_inside_math_bool
          {
            \RenewCommandCopy \UsedOnMessageMath \UsedOnMessageInsideMath
            \RenewCommandCopy \UsedByMessageMath \UsedByMessageInsideMath
            \RenewCommandCopy \UsedByAndOnMessageMath \UsedByAndOnMessageInsideMath
          }
          {
            \RenewCommandCopy \UsedOnMessageMath \UsedOnMessageOutsideMath
            \RenewCommandCopy \UsedByMessageMath \UsedByMessageOutsideMath
            \RenewCommandCopy \UsedByAndOnMessageMath \UsedByAndOnMessageOutsideMath
          }
        \str_case:xn { \str_foldcase:n { #1 } }
          {
            {usedon}
              { \UsedOnMessageMath { \exp_not:V \cpageref{\l_tmpa_clist} } }
            {usedby}
              { \UsedByMessageMath { \cref{\l_tmpa_clist} } }
            {usedbyandon}
              { \UsedByAndOnMessageMath { \cref{\l_tmpa_clist} } { \exp_not:V \cpageref{\l_tmpa_clist} } }
          }
      }
  }
\NewDocumentCommand \__UsedOn_PrintMessage { m }
  {
    \clist_map_inline:Nn \g__UsedOn_Options_clist
      {
        \__UsedOn_Printer{##1}{#1}
      }
  }
\NewDocumentCommand \__UsedOn_PrintMessageMath { m }
  {
    \clist_map_inline:Nn \g__UsedOn_Options_clist
      {
        \__UsedOn_Printer_Math{##1}{#1}
      }
  }
\str_new:N \l__UsedOn_Option_str
\NewDocumentCommand \__UsedOn_Processor { o m }
  {
    \IfValueT{#1}{
      \str_case:xnTF { \str_foldcase:n { #1 } }
        {
%%            % check if options 'UsedOn', 'UsedBy' or 'UsedByAndOn'
%%            % (case-insensitive) were used in one of the following forms
            {usedon}      {\str_set:Nn \l__UsedOn_Option_str {UsedOn}}
            {uo}          {\str_set:Nn \l__UsedOn_Option_str {UsedOn}}
            {usedby}      {\str_set:Nn \l__UsedOn_Option_str {UsedBy}}
            {ub}          {\str_set:Nn \l__UsedOn_Option_str {UsedBy}}
            {usedbyandon} {\str_set:Nn \l__UsedOn_Option_str {UsedByAndOn}}
            {ubao}        {\str_set:Nn \l__UsedOn_Option_str {UsedByAndOn}}
        }
        {
            {
%%              % Loop through (potential) label list in arg of \cref (or \Cref)
              \seq_set_from_clist:Nn \l_tmpa_seq {#2}
              \seq_map_inline:Nn \l_tmpa_seq
                {
%%                  % if the label has not been referenced yet,
%%                  % create a counter for the current and last run and save the label in the
%%                  % global container \g_@@_k_seq
                  \seq_if_in:NxF \g__UsedOn_k_seq { \l__UsedOn_Option_str @##1 }
                    {
                        \newcounter{ ThisRun@ \l__UsedOn_Option_str @##1 }
                        \cs_if_free:cT {c@LastRun@ \l__UsedOn_Option_str @##1}
                          {
                            \newcounter{LastRun@ \l__UsedOn_Option_str @##1}
                          }
                        \seq_gput_right:Nx \g__UsedOn_k_seq
                          { \l__UsedOn_Option_str @##1}
                    }
%%                  % increase the counters and compare with max counter
                  \stepcounter{ThisRun@ \l__UsedOn_Option_str @##1}
                  \setcounter{LastRun@ \l__UsedOn_Option_str @##1}
                    {
                      \fp_eval:n
                        {
                          max(
                            \value{ThisRun@ \l__UsedOn_Option_str @##1} ,
                            \value{LastRun@ \l__UsedOn_Option_str @##1}
                          )
                        }
                    }
%%                    % store the value in global key-value property list
                    \prop_gput:Nxx \g__UsedOn_kv_prop
                      { \l__UsedOn_Option_str @##1}
                      {\arabic{LastRun@ \l__UsedOn_Option_str @##1}}
%%                    % create a label for the UsedOn reference and number this label
                    \__UsedOn_origlabel
                      {
                        \l__UsedOn_Option_str @##1@
                        \arabic{ThisRun@ \l__UsedOn_Option_str @##1}
                      }
                }
            }
        }
        {
%%            % Throw an error, if an unrecognised option was used
%%            % for the optional argument to this macro.
            \msg_new:nnn {cleveref-usedon} { OptionSpellingError }
              {
                \MessageBreak
                Spelling~error~\msg_line_context:
                \MessageBreak
                Did~you~mean~to~pass~option\MessageBreak
                'UsedOn'~to~cref~or~Cref?
              }
            \msg_fatal:nn { cleveref-usedon } { OptionSpellingError }
        }
    }
  }
\bool_new:N \l__UsedOn_already_specified_bool
\clist_new:N \l__UsedOn_args_clist
\cs_new:Nn \__UsedOn_define_from_orig:NN
  {
    \NewDocumentCommand #1 { s o m }
      {
        \IfValueTF { ##2 }
          {
            \bool_set_false:N \l__UsedOn_already_specified_bool
            \clist_set:Nn \l__UsedOn_args_clist { ##2 }
            \clist_put_right:No \l__UsedOn_args_clist { \l__UsedOn_default_tl }
            \clist_map_inline:Nn \l__UsedOn_args_clist
              {
                \str_case:xnT { \str_foldcase:n { ####1 } }
                  {
                    {usedon} {}
                    {uo}     {}
                    {usedby} {}
                    {ub}     {}
                    {usedbyandon} {}
                    {ubao}        {}
                  }
                  {
                    \clist_remove_all:Nn \l__UsedOn_args_clist { ####1 }
                    \bool_if:NF \l__UsedOn_already_specified_bool
                      {
                        \__UsedOn_Processor[####1]{##3}
                      }
                    \bool_set_true:N \l__UsedOn_already_specified_bool
                  }
              }
            \clist_if_empty:NTF \l__UsedOn_args_clist
              {
                \IfBooleanTF{##1}{ #2*{##3} }{ #2{##3} }
              }
              {
                \IfBooleanTF{##1}{ #2*[\l__UsedOn_args_clist]{##3} }{ #2[\l__UsedOn_args_clist]{##3} }
              }
          }
          {
            \tl_if_blank:VF \l__UsedOn_default_tl
              {
                \clist_clear:N \l__UsedOn_args_clist
                \clist_put_right:No \l__UsedOn_args_clist { \l__UsedOn_default_tl }
                \clist_map_inline:Nn \l__UsedOn_args_clist
                  {
                    \__UsedOn_Processor[####1]{##3}
                  }
              }
            \IfBooleanTF{##1}{ #2*{##3} }{ #2{##3} }
          }
      }
  }
\__UsedOn_define_from_orig:NN \__UsedOn_cref \__UsedOn_origcref
\__UsedOn_define_from_orig:NN \__UsedOn_Cref \__UsedOn_origCref
\__UsedOn_define_from_orig:NN \__UsedOn_labelcref \__UsedOn_origlabelcref
\NewDocumentCommand \__UsedOn_ReadFromAux { }
  {
    \prop_map_inline:Nn \g__UsedOn_kv_prop
      {
          \newcounter{LastRun@##1}
          \setcounter{LastRun@##1}{##2}
      }
  }
\NewDocumentCommand \__UsedOn_WriteToAux { }
  {
%%    % First, we clear the global key-value prop list \cs{g_@@_kv_prop} and
%%    % then we rebuild it with the information from the current run.
    \prop_clear:N \g__UsedOn_kv_prop
    \seq_map_inline:Nn \g__UsedOn_k_seq
      { \prop_gput:Nxx \g__UsedOn_kv_prop {##1}{\arabic{ThisRun@##1}} }
%%    % Turn on |expl3| functionality in .aux file.
    \iow_now:cx { @auxout }
      { \token_to_str:N \ExplSyntaxOn }
%%    % Loop through the key-val |proplist| and write contents to .aux file.
    \prop_map_inline:Nn \g__UsedOn_kv_prop
      {
        \tl_set:Nn \l_tmpa_tl { ##1 }
        \tl_replace_all:Nnn \l_tmpa_tl { ~ } { \c_tilde_str }
        \iow_now:cx { @auxout }
          {
            \prop_gput_from_keyval:Nn \token_to_str:N \g__UsedOn_kv_prop
              { {\l_tmpa_tl} = ##2 }
          }
      }
%%    % Turn off |expl3| functionality in .aux file.
    \iow_now:cx { @auxout }
      { \token_to_str:N \ExplSyntaxOff }
}%
\hook_gput_code:nnn { begindocument/end } { cleveref-usedon }
  {
    \__UsedOn_ReadFromAux
    \cs_if_exist:NF \label@in@display { \NewCommandCopy \label@in@display \label }
    \NewCommandCopy \__UsedOn_origlabel@in@display \label@in@display
    \NewCommandCopy \__UsedOn_origlabel \label
    \NewCommandCopy \__UsedOn_origcref  \cref
    \NewCommandCopy \__UsedOn_origCref  \Cref
    \NewCommandCopy \__UsedOn_origlabelcref \labelcref
    \bool_new:N \g__UsedOn_no_use_bool
    \RenewDocumentCommand \label { o m }
      {
        \bool_gset_false:N \g__UsedOn_no_use_bool
        \clist_clear:N \l__UsedOn_args_clist
        \IfValueT { #1 }
          {
            \clist_set:Nn \l__UsedOn_args_clist { #1 }
          }
        \clist_map_inline:Nn \l__UsedOn_args_clist
          {
            \str_case:xnT { \str_foldcase:n { ##1 } }
              {
                {nouse} {}
              }
              {
                \clist_remove_all:Nn \l__UsedOn_args_clist { ##1 }
                \bool_gset_true:N \g__UsedOn_no_use_bool
              }
          }
        \if_mode_math: % this is for environments that do not use \label@in@display
          \clist_if_empty:NTF \l__UsedOn_args_clist
            {
              \__UsedOn_origlabel@in@display{#2}
            }
            {
              \__UsedOn_origlabel@in@display[\l__UsedOn_args_clist]{#2}
            }
          \bool_if:NF \g__UsedOn_no_use_bool
            {
              \__UsedOn_PrintMessageMath{#2}
            }
        \else:
          \clist_if_empty:NTF \l__UsedOn_args_clist
            {
              \__UsedOn_origlabel{#2}
            }
            {
              \__UsedOn_origlabel[\l__UsedOn_args_clist]{#2}
            }
          \bool_if:NF \g__UsedOn_no_use_bool
            {
              \__UsedOn_PrintMessage{#2}
            }
        \fi:
      }
    \RenewDocumentCommand \label@in@display { o m }
      {
        \bool_gset_false:N \g__UsedOn_no_use_bool
        \clist_clear:N \l__UsedOn_args_clist
        \IfValueT { #1 }
          {
            \clist_set:Nn \l__UsedOn_args_clist { #1 }
          }
        \clist_map_inline:Nn \l__UsedOn_args_clist
          {
            \str_case:xnT { \str_foldcase:n { ##1 } }
              {
                {nouse} {}
              }
              {
                \clist_remove_all:Nn \l__UsedOn_args_clist { ##1 }
                \bool_gset_true:N \g__UsedOn_no_use_bool
              }
          }
        \clist_if_empty:NTF \l__UsedOn_args_clist
          {
            \__UsedOn_origlabel@in@display{#2}
          }
          {
            \__UsedOn_origlabel@in@display[\l__UsedOn_args_clist]{#2}
          }
        \bool_if:NF \g__UsedOn_no_use_bool
          {
            \__UsedOn_PrintMessageMath{#2}
          }
      }
    \RenewCommandCopy \cref \__UsedOn_cref
    \RenewCommandCopy \Cref \__UsedOn_Cref
    \RenewCommandCopy \labelcref \__UsedOn_labelcref
  }
\hook_gput_code:nnn { enddocument } { cleveref-usedon }
  {
    \__UsedOn_WriteToAux
  }
\endinput
%%
%% End of file `cleveref-usedon.sty'.

答案1

事实证明,经过cleveref特殊处理multline。它将内部宏重新定义为:

  \def\multline@#1{%
    \Let@%
    \@display@init{\global\advance\row@\@ne\relax\global\dspbrk@lvl\m@ne}%
    \chardef\dspbrk@context\z@%
    \restore@math@cr%
    \let\tag\tag@in@align%
    \global\tag@false \global\let\raise@tag\@empty%
    \mmeasure@{#1}%
    \let\tag\gobble@tag \let\label\cref@gobble@optarg%  <<< cleveref modification
    \tabskip \if@fleqn \@mathmargin \else \z@skip \fi%
    \totwidth@\displaywidth%
    \if@fleqn%
        \advance\totwidth@-\@mathmargin\relax%
    \fi%
    \halign\bgroup%
        \hbox to\totwidth@{%
            \if@fleqn%
                \hskip \@centering \relax%
            \else%
                \hfil%
            \fi%
            \strut@%
            $\m@th\displaystyle{}##\endmultline@math%
            \hfil%
        }% $
        \crcr%
        \if@fleqn%
            \hskip-\@mathmargin%
            \def\multline@indent{\hskip\@mathmargin}%
        \else%
            \hfilneg%
            \def\multline@indent{\hskip\multlinegap}%
        \fi%
        \iftagsleft@%
            \iftag@%
                \begingroup%
                    \ifshifttag@%
                        \rlap{\vbox{%
                                \normalbaselines%
                                \hbox{%
                                    \strut@%
                                    \make@display@tag%
                                }%
                                \vbox to\lineht@{}%
                                \raise@tag%
                        }}%
                        \multline@indent%
                    \else%
                        \setbox\z@\hbox{\make@display@tag}%
                        \dimen@\@mathmargin \advance\dimen@-\wd\z@\relax%
                        \ifdim\dimen@<\multlinetaggap%
                          \dimen@\multlinetaggap%
                        \fi%
                        \box\z@ \hskip\dimen@\relax%
                    \fi%
                \endgroup%
            \else%
                \multline@indent%
            \fi%
        \else%
            \multline@indent%
        \fi%
    #1%
  }%

为了使代码正常工作,必须修补此修改后的行,例如通过:

% \RequirePackage{regexpatch}
\xpatchcmd{\multline@}{\let\label\cref@gobble@optarg}{\let\label\label@gobble}{}{}

其中的定义与中的\label@gobble修改类似,只是没有应用原始的标签命令。\labelcleveref-usedon

相关内容