If I am in Math mode, and I want to name a variable, say max-color, here max and color are treated as two different variables and two spaces are placed around the minus sign. Now, what I really meant was a single variable called max-color. How to achieve this inside Math mode.
2 Answers
In order to match the font of the other variables, it is probably best to use \mathit. However, this will still set the dash as a minus. To change that you can use \text, or for better consistency \textnormal around the dash, since \text adopts the font style of the text before the equation. The command \text requires amsmath, whereas \textnormal is a standard LaTeX command but it is improved by the amsmath package, so that it scales appropriately in subscripts, superscripts and fractions. Thus I recommend the following:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\( \mathit{max\textnormal{-}color} \)
\end{document}
You can conveniently package this as a command via
\newcommand{\maxcolor}{\mathit{max\textnormal{-}color}}
EDIT I originally thought an extra level of braces around the definition would be needed to make this directly usable in a sub-/superscripts, but T.Verron pointed out they are not necessary in this case.
- 95,762
-
-
2You could use
\textnormal{\itshape ...}, but (a)\textiton its own will change in a bold context, (b) there is no guarantee that the textitshapefont is the same as themathitfont – Andrew Swann Feb 14 '13 at 09:39 -
Thank you a lot, that's indeed good reason. On a side note, I just tested it, the extra level of braces you mention in your last command definition seems superfluous. Latex seems to perform smarter command expansion than Tex.
:)– T. Verron Feb 14 '13 at 09:49 -
@T.Verron You are right about the brackets! It seems to be some special property of these font changing commands. – Andrew Swann Feb 14 '13 at 12:54
-
@AndrewSwann The expansion of
\mathitstarts, after some work, with\bgroup, followed by the font selection internal command; TeX accepts_\bgroup...\egroupas equivalent to_{...}– egreg Feb 14 '13 at 14:12 -
@egreg Many thanks for the explanation. I started digging through the code, but didn't get to the bottom. – Andrew Swann Feb 14 '13 at 14:13
You can take benefit of \operatorname which set things up so that in its argument hyphens give real hyphens and not minus signs:
\usepackage{amsmath} % only amsopn would suffice, actually
\newcommand{\var}[1]{{\operatorname{#1}}}
The additional pair of braces will keep the object from being considered an operator as far as spacing is concerned. If you want the variable name to appear in italics, change the definition into
\newcommand{\var}[1]{{\operatorname{\mathit{#1}}}}
In the following minimal example I use \varA for the first realization and \varB for the second one, take your pick.
\documentclass{article}
\usepackage{amsmath}
\newcommand{\varA}[1]{{\operatorname{#1}}}
\newcommand{\varB}[1]{{\operatorname{\mathit{#1}}}}
\begin{document}
$\varA{max-color}=3$
$\varB{max-color}=2$
\end{document}

- 1,121,712
\text{max-color}, assuming you are using\usepackage{amsmath}. – Peter Grill Feb 14 '13 at 04:58max\text{-}colorwill put just the dash into text mode. However, I would not recommend that you do that. – Peter Grill Feb 14 '13 at 05:03\newcommand{\MaxColor}{\text{max-color}}and then use\MaxColorinstead. Then when you decide to change the formatting you just need to change the definition of it. However, that is assuming you are using this for math. If you are using it for algorithm you should look at the appropriate packages. See for instance Print programs with its proper syntax. – Peter Grill Feb 14 '13 at 05:10max-colorin math mode meansm*a*x-c*o*l*o*r. – hpekristiansen Feb 14 '13 at 05:16max\text{-}color. – Peter Grill Feb 14 '13 at 05:32\newcommand{\MaxColor}{\textnormal{max-color}}would be more suitable, see the answer to this question : http://tex.stackexchange.com/questions/98052/umlauts-in-math-mode . – T. Verron Feb 14 '13 at 06:20-–−+. – bodo Feb 14 '13 at 06:52