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 '19 at 12:38\ifemptyarg
command (good craftwork, though ;-), you could use\ifstrempty
or\ifblank
from the etoolbox package. – frougon Jul 15 '19 at 12:57$K$-vector space\ .
. Hope that my new wording is clearer. – Fitzcarraldo Jul 15 '19 at 14:27