xparse:带星号的命令和 \IfBooleanTF

xparse:带星号的命令和 \IfBooleanTF

在对现有代码进行较长时间的重新定义时,我使用了类似

\documentclass{book}
\usepackage{xparse}


\makeatletter
\NewDocumentCommand{\mymacro}{smO{}m}{
    \def\blabla{#1}%
    % several lines later in another macro I use
    \IfBooleanTF{\blabla}{}{}
}

\makeatother

\begin{document}
    \mymacro*{}{}
\end{document}

它可以很好地与 texlive 2018(xparse 2018-10-17)和 lualatex 配合使用。但是,当尝试使用 texlive 2019(xparse 2019-03-05)时,我收到以下错误

! Undefined control sequence.
<argument> \LaTeX3 error:
                Invalid use \IfBooleanTF {\blabla }
l.15     \mymacro*{}{}

使用新版本的 xparser 执行此类操作的正确方法是什么?


以下是上述情况发生时我的完整宏定义:基本上,我试图从子标题中删除标题,包括垂直空间。为了实现这一点,我修改了子标题包的部分内容。这适用于如上所述的 TL2018,并产生以下输出:

在此处输入图片描述

请注意,我特别在寻找一种对我拥有的其他代码侵入性最小的解决方案。为此,我认为只需*在 中添加\subcaptionbox就非常方便。

\documentclass{book}

\usepackage{xparse,graphicx,subcaption,mwe,float}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\IfNoValueOrEmptyTF}{mmm}
{
    \IfNoValueTF{#1}{#2}
    {
        \tl_if_empty:nTF {#1} {#2} {#3}
    }
}
\ExplSyntaxOff
% This code makes \subcaptionbox*{}{\includegraphics...} print no label and remove the vertical space

\makeatletter
\let\oldsubcaptionbox\subcaptionbox
\RenewDocumentCommand{\subcaptionbox}{smO{}m}{
    \def\subcaptionbox@skip@subcpation{#1}%
    \IfBooleanTF{#1}{%
        \IfNoValueOrEmptyTF{#3}{%
            \oldsubcaptionbox*{#2}{#4}%
        }{%
            \oldsubcaptionbox*{#2}[#3]{#4}%
        }%
    }{%
        \IfNoValueOrEmptyTF{#3}{%
            \oldsubcaptionbox{#2}{#4}%
        }{%
            \oldsubcaptionbox{#2}[#3]{#4}%
        }%
    }{%
    }%
}

\long\def\caption@iiibox#1#2#3#4[#5]#6{%
    \begingroup
    #1*% set \caption@position
    \caption@iftop{%
        \endgroup
        \parbox[t]{#4}{%
            #1\relax
            \caption@setposition t%
            \vbox{\caption#2{#3}}%
            \captionbox@hrule
            \csname caption@hj@#5\endcsname
            #6}%
    }{%
        \endgroup
        \parbox[b]{#4}{%
            #1\relax
            \caption@setposition b%
            \csname caption@hj@#5\endcsname
            #6%
            \captionbox@hrule%
            \IfBooleanTF{\subcaptionbox@skip@subcpation}{%
                \IfNoValueOrEmptyTF{#3}{}{%
                    \vtop{\caption#2{#3}}%
                }%
            }{%
                \vtop{\caption#2{#3}}%
            }%
        }%
}}
\makeatother

\begin{document}
    \begin{figure}[H]
        \subcaptionbox{}{\includegraphics[width=0.5\linewidth]{example-image-a}}%
        \subcaptionbox{}{\includegraphics[width=0.5\linewidth]{example-image-b}}%
    \end{figure}
    Blabla
    \begin{figure}[H]
        \subcaptionbox*{}{\includegraphics[width=0.5\linewidth]{example-image-a}}%
        \subcaptionbox*{}{\includegraphics[width=0.5\linewidth]{example-image-b}}
    \end{figure}
    BlaBla
\end{document}

答案1

你让你的生活变得困难。如果有*并且参数为空,则只需发出最后一个参数,可能在指定宽度的框内。

\documentclass{book}

\usepackage{xparse,graphicx,subcaption}

\let\oldsubcaptionbox\subcaptionbox

\ExplSyntaxOn
\NewExpandableDocumentCommand{\IfEmptyTF}{mmm}
 {
  \tl_if_empty:nTF { #1 } { #2 } { #3 }
 }
\ExplSyntaxOff

\RenewDocumentCommand{\subcaptionbox}{smom}{%
  \IfBooleanTF{#1}
    {%
     \IfEmptyTF{#2}
      {#4}
      {\IfNoValueTF{#3}{\oldsubcaptionbox*{#2}{#4}}{\oldsubcaptionbox*{#2}[#3]{#4}}}%
    }
    {%
     \IfNoValueTF{#3}{\oldsubcaptionbox{#2}{#4}}{\oldsubcaptionbox{#2}[#3]{#4}}%
    }%
}

\begin{document}

\begin{figure}[htp!]

\subcaptionbox{zzz}{\includegraphics[width=0.5\linewidth]{example-image-a}}%
\subcaptionbox{zzz}{\includegraphics[width=0.5\linewidth]{example-image-b}}

\end{figure}

Blabla

\begin{figure}[htp!]

\subcaptionbox*{zzz}{\includegraphics[width=0.5\linewidth]{example-image-a}}%
\subcaptionbox*{zzz}{\includegraphics[width=0.5\linewidth]{example-image-b}}

\end{figure}

Blabla

\begin{figure}[htp!]

\subcaptionbox*{}{\includegraphics[width=0.5\linewidth]{example-image-a}}%
\subcaptionbox*{}{\includegraphics[width=0.5\linewidth]{example-image-b}}

\end{figure}

BlaBla

\end{document}

在此处输入图片描述

相关内容