自定义巧妙引用和简短的标题给出错误

自定义巧妙引用和简短的标题给出错误

我一直\Cref在我的文档中使用以下修改来为定理和引理提供名称(如果给出的话)(因为我认为它读起来更好)。就像在 MWE 中一样(基于上一个问题的答案)。

但是,我刚刚注意到,当我在短标题字段中使用自定义命令时,我收到以下错误:Incomplete \iffalse; all text was ignored after line 68。我已注释掉此行,因此现在可以编译,但我在许多短标题中分散了几个这样的错误。有解决办法吗?

平均能量损失

\caption[\oliver's short caption]{\oliver's figure}导致问题。)

\documentclass[a4paper,10pt]{extarticle}


\usepackage{amsmath,mathtools,graphicx,etoolbox,nameref,amsthm,cleveref,xspace}
% Have theorems and lemmas be referenced by name if applicable.
\makeatletter
\renewrobustcmd{\cref}{\@osmcref{cref}}
\renewrobustcmd{\Cref}{\@osmcref{Cref}}
\def\@osmcref#1#2{%
    \begingroup
    \ifcsundef{r@#2}
    {}
    {\edef\@osmcref@name{%
            \expandafter\expandafter\expandafter
            \@thirdoffive\csname r@#2\endcsname}}%
    \ifcsundef{r@#2@cref}
    {}
    {\cref@gettype{#2}{\@osmcref@type}}%
    \ifboolexpr{not test {\ifdefvoid{\@osmcref@name}}
        and (test {\ifdefstring{\@osmcref@type}{theorem}}
        or test {\ifdefstring{\@osmcref@type}{lemma}})}
    {\nameref{#2} (\@cref{#1}{#2})}
    {\@cref{#1}{#2}}%
    \endgroup
}
\makeatother
% Give bold names to definitions and similar environments. 
\makeatletter
\def\th@plain{%
    \thm@notefont{}% same as heading font
    \itshape % body font
}
\def\th@definition{%
    \thm@notefont{}% same as heading font
    \normalfont % body font
}
\makeatother
% Giving correct theorem and lemma environments. 
\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}[theorem]{Lemma}
% Naming these nicely.
\crefname{lemma}{lemma}{lemmas}
\Crefname{lemma}{Lemma}{Lemmas}
\crefname{theorem}{theorem}{theorems}
\Crefname{theorem}{Theorem}{Theorems}


\newcommand{\oliver}{Oliver\xspace}


\begin{document}
    \section{Commands and short captions}
    \label{sec}

    \begin{lemma}\label{noname}
        Extremely important lemma that has no name.
    \end{lemma}

    \begin{lemma}[Fatou's lemma]\label{boringLemma}
        This is some boring statement.
    \end{lemma}

    \begin{theorem}[Fubini's Theorem]\label{awesomeTheorem}
        This is a really awesome statement.
    \end{theorem}

    \Cref{fig} Doesn't work when I have \verb|\oliver| in the short caption. 

    \begin{figure}[htb]
        \centering
        Something interesting.
        \caption[Oliver's short caption]{\oliver's figure}
%        \caption[\oliver's short caption]{\oliver's figure} % The troublesome line.
        \label{fig}
    \end{figure}

    We have \Cref{boringLemma} and \Cref{awesomeTheorem}. Whereas \Cref{noname} doesn't have anything interesting.


\end{document}

在此处输入图片描述

答案1

未受保护的人\edef有点太急切了。尝试

\makeatletter
\newcommand\expandthrice{%
  \unexpanded\expandafter\expandafter\expandafter
  \expandafter\expandafter\expandafter\expandafter}
\renewrobustcmd{\cref}{\@osmcref{cref}}
\renewrobustcmd{\Cref}{\@osmcref{Cref}}
\def\@osmcref#1#2{%
  \begingroup
    \ifcsundef{r@#2}
      {}
      {\edef\@osmcref@name{\expandthrice{%
         \expandafter\expandafter\expandafter
         \@thirdoffive\csname r@#2\endcsname}}}%
    \ifcsundef{r@#2@cref}
      {}
      {\cref@gettype{#2}{\@osmcref@type}}%
    \ifboolexpr{not test {\ifdefvoid{\@osmcref@name}}
                and (test {\ifdefstring{\@osmcref@type}{thm}}
                     or test {\ifdefstring{\@osmcref@type}{lemma}})}
      {\nameref{#2} (\@cref{#1}{#2})}
      {\@cref{#1}{#2}}%
    \endgroup
}
\makeatother

或者这种美丽

\makeatletter
\renewrobustcmd{\cref}{\@osmcref{cref}}
\renewrobustcmd{\Cref}{\@osmcref{Cref}}
\def\@osmcref#1#2{%
  \begingroup
    \ifcsundef{r@#2}
      {}
      {\expandafter\expandafter\expandafter\expandafter\expandafter
         \expandafter\expandafter\def
       \expandafter\expandafter\expandafter\expandafter\expandafter
         \expandafter\expandafter\@osmcref@name
       \expandafter\expandafter\expandafter\expandafter\expandafter
         \expandafter\expandafter{%
         \expandafter\expandafter\expandafter
         \@thirdoffive\csname r@#2\endcsname}}%
    \ifcsundef{r@#2@cref}
      {}
      {\cref@gettype{#2}{\@osmcref@type}}%
    \ifboolexpr{not test {\ifdefvoid{\@osmcref@name}}
                and (test {\ifdefstring{\@osmcref@type}{thm}}
                     or test {\ifdefstring{\@osmcref@type}{lemma}})}
      {\nameref{#2} (\@cref{#1}{#2})}
      {\@cref{#1}{#2}}%
    \endgroup
}
\makeatother

反而。

我已经更新了我的答案Clever Ref 仅给出定理名称。与七个\expandafter怪物。

第一个解决方案是 通过将 的内容放在 中(通常会尝试尽可能地扩展)来\expandthrice扩展 的内容三倍,但在三步之后停止扩展(本质上这是,但有三个扩展步骤)。第二个解决方案是七个 ,在保存之前立即仅扩展 的内容三倍。不知何故,我觉得第二个解决方案更好一些,因为它没有定义新命令,并且它明确执行了必要的扩展步骤,而第一个解决方案在正确的位置停止了显式的完整扩展。\@osmcref@name\edef\expandthriceetoolbox\expandonce\expandafter\@osmcref@name

相关内容