3

In order to deal with both burdensome expressions $K$-vector space and $K$-vector space $E$ at once, and this in both text and math mode alike, I defined a fine working shortcut taking two arguments, the second of them optional, as follows:

\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

Thanks to the nice macro \ifemptyarg created by egreg in another post, I could redefine \vs in a thriftier way:

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

But mysteriously, with this version of \vs the result of both [...] \vs{K}{E}, and [...] \vs{K}{}, is the same as if instead I was respectively writing [...] $K$-vector space\ , and [...] $K$-vector space $E$\ ,, namely, with an undesired extra space after each comma. Why?

  • $K$-vector space\ . is obviously wrong, the space should go away. – egreg Jul 15 at 12:38
  • 1
    @Fitzcarraldo Welcome to TeX.SE! As an alernative to the artisanal \ifemptyarg command (good craftwork, though ;-), you could use \ifstrempty or \ifblank from the etoolbox package. – frougon Jul 15 at 12:57
  • @egreg I couldn't agree more with you, but that's what my code was doing, that is, as if I had exactly written $K$-vector space\ .. Hope that my new wording is clearer. – Fitzcarraldo Jul 15 at 14:27
3

You have several spaces inserted by newlines...

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

works. Notice that the "%" after the \ifemptyarg{#2} is not necessary, but well...

  • 4
    Technically you only need the ending % in \ifemptyarg{.}{..}{...}. – Werner Jul 15 at 15:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.