使用条件宏时出现神秘的水平空间

使用条件宏时出现神秘的水平空间

为了同时处理繁琐的表达式$K$-vector space$K$-vector space $E$并且同时在文本和数学模式下处理,我定义了一个精细的工作快捷方式,它接受两个参数,其中第二个参数是可选的,如下所示:

\makeatletter
\newcommand{\vs}[2]{%
    \relax\ifmmode
        \ifx\relax#2\relax
            #1\text{-vector space}%
        \else
            #1\text{-vector space }#2%
        \fi
    \else
        \ifx\relax#2\relax
            $#1$-vector space%
        \else
            $#1$-vector space $#2$%
        \fi
    \fi}
\makeatother

感谢in\ifemptyarg创建的出色宏egreg另一篇帖子,我可以\vs用更节俭的方式重新定义:

\documentclass{article}

\makeatletter
\def\ifemptyarg#1{%
    \if\relax\detokenize{#1}\relax    % H. Oberdiek
        \expandafter\@firstoftwo%
    \else
        \expandafter\@secondoftwo%
    \fi}
\makeatother

\newcommand{\vs}[2]{%
    \relax\ifmmode
        \ifemptyarg{#2}
            {#1\text{-vector space}}
            {#1\text{-vector space }#2}
    \else
    \ifemptyarg{#2}
        {$#1$-vector space}
        {$#1$-vector space $#2$}
    \fi}


\begin{document}

For any field $K$, a consequence of the Axiom of Choice
is that there is a basis for every \vs{K}{E}, but less known
is the fact that if there is a basis for every \vs{K}{},
and this for any $K$, then the Axiom of Choice holds!

\end{document}

但奇怪的是,这个版本的\vs结果[...] \vs{K}{E},[...] \vs{K}{},分别写成[...] $K$-vector space\ ,和的结果是一样的[...] $K$-vector space $E$\ ,,也就是说,每个逗号后面都有一个多余的空格。这是为什么呢?

答案1

您通过换行符插入了多个空格......

\newcommand{\vs}[2]{%
    \relax\ifmmode
        \ifemptyarg{#2}%
            {#1\text{-vector space}}%
            {#1\text{-vector space }#2}%
    \else
    \ifemptyarg{#2}%
        {$#1$-vector space}%
        {$#1$-vector space $#2$}%
\fi}

有效。请注意,后面的“%”\ifemptyarg{#2}不是必需的,但是……

相关内容