# Problem with macro definition spaces of index and superscript

I have defined a command FZ for defining mathematical symbols:

\newcommand{\FZ}[1]{\textcolor{blue}{\ensuremath{#1}}\xspace}


I want to define a Symbol CV first and then create some variants with different indices like CV_D, CV_{D,\,\mathrm{Gr}}...

To make that consistently, I tried to define it "stepwise", but then I get problems with the spacing. See minimal example:

• how can I get the D closer to the CV like in the black version which is "directly" typeset?
• how can I get the "Gr" on the same height as the index "D"?

Minimal example:

\documentclass{scrbook}
\usepackage{color, xspace}

\newcommand{\FZ}[1]{\textcolor{blue}{\ensuremath{#1}}\xspace}

\newcommand{\CV}{\FZ{CV}}
\newcommand{\CvD}{\FZ{\CV{}^{*}_{D}}}
\newcommand{\CvDGr}{\FZ{\CvD_{,\,\mathrm{Gr}}}}

\begin{document}

\CV

\CvD

wish: $CV_D$

\CvDGr

wish: $CV^{*}_{D,\,\mathrm{Gr}}$

\end{document}

-

In math mode { } generate a new math object (it's actually called differently AFAIK, but I can't recall the correct name) which influences the placement of sub- and superscripts. {{...}_{D}}_{\,Gr} is simply different than {...}_{D\,Gr}.

Also placing it in a color macro has also some influence. Note that you use \FZ multiple times int \CvDGr. I would simply flatten out the macro definitions. If you really want "consistency" then IMHO you need some macros which do not hold the \FZ and also accept more material in form of an argument:

\documentclass{scrbook}
\usepackage{color, xspace}

\newcommand{\FZ}[1]{\textcolor{blue}{\ensuremath{#1}}\xspace}

\newcommand{\cv}{CV}
\newcommand{\CV}{\FZ{\cv}}
\newcommand{\cvd}[1]{\cv^{*}_{D#1}}
\newcommand{\CvD}{\FZ{\cvd{}}}
\newcommand{\CvDGr}{\FZ{\cvd{,\,\mathrm{Gr}}}}

\begin{document}

\CV

\CvD

wish: $CV_D$

\CvDGr

wish: $CV^{*}_{D,\,\mathrm{Gr}}$

\end{document}


-
thank you! I'll try that approach. The color is only used to easily distinguish already consistently defined symbols from those typed "by hand" and will be removed for the final version, when all non-blue math symbols have been checked and changed into blue ones. – MostlyHarmless Jul 27 '11 at 9:50

My position is to never use \ensuremath for commands that produce math symbols, which also frees from the \xspace problem.

\newcommand{\FZ}{\textcolor{blue}}
\newcommand{\CV}{\FZ{CV}}
\newcommand{\CvD}{\FZ{CV^{*}_{D}}}
\newcommand{\CvDGr}{\FZ{CV^{*}_{D,\,\mathrm{Gr}}}}


The "D" in your \CvD is far because of the empty subformula you inserted with {}; the "Gr" was not in the same subscript, so it was treated as a subscript of an empty formula, hence it was a bit raised because the "D" was pushed down by the presence of the superscript.

Now $\CV$, $\CvD$ and $\CvDGr$ work without problems. If you really want \ensuremath, put it outside the color command:

\newcommand{\FZ}[1]{\ensuremath{\textcolor{blue}{#1}}\xspace}


Indeed \textcolor{blue}{x} works also in math mode, producing an ordinary symbol; since your objects are ordinary symbols, there's no problem.

-
thanks! So you mean, it is better to explicitely write $...$ around all math commands? What happens then, if I use one already defined command "in" another one? E. g. I define \D as $D$ and later use it in \CVD which shall look like $CV_{\D}$? (The hint about the color command is extremely helpful as well!) – MostlyHarmless Jul 27 '11 at 9:54
Keep math as math and non math as non math. Where's the gain from $D$ to \D? – egreg Jul 27 '11 at 10:13
$D$ is a variable describing a product property, I'm using very often. Should I decide to call it $d$ later, I'd like to be able to change that easily. And same for e. g. the coefficient of variation of the property \D which originally was called CV_{D}, but might have to change his name. I'm also using a lot of indices which shall be easily toggled between english and german, for example. Defining those commands proved very helpful for me, as I (a) am not very good in regular expressions, so (b) the risk would be too high to destroy something with simple find and replace... – MostlyHarmless Jul 27 '11 at 10:33
p.s. it does not end with $CV_D$, but there are the D's and CV_D's of different product states during the manufacturing process. I've made up a quite complicated system, but I prefer defining it once and using it consistently to reduce the risk of errors. – MostlyHarmless Jul 27 '11 at 10:35
So define it by \newcommand{\D}{D}. I really don't see any advantage in being able to write \D in text instead of $\D$, when this adds complications to the definitions; $\D$ is straigthforward, well visible in the input file, and has no "spaces problems". – egreg Jul 27 '11 at 10:39