Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
\text{max-color}, assuming you are using \usepackage{amsmath}. –  Peter Grill Feb 14 '13 at 4:58
    
That appears to change the font, I am the variable name no longer looks like other math symbols. –  Shamim Hafiz Feb 14 '13 at 5:01
1  
In that case max\text{-}color will put just the dash into text mode. However, I would not recommend that you do that. –  Peter Grill Feb 14 '13 at 5:03
    
Actually, I am trying to type out an algorithm where some variables are named this way. What would be the ideal way to achieve this? –  Shamim Hafiz Feb 14 '13 at 5:08
1  
\newcommand{\MaxColor}{\textnormal{max-color}} would be more suitable, see the answer to this question : tex.stackexchange.com/questions/98052/umlauts-in-math-mode . –  T. Verron Feb 14 '13 at 6:20
show 4 more comments

2 Answers 2

up vote 5 down vote accepted

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}

enter image description here

share|improve this answer
add comment

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:

Sample output

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

share|improve this answer
    
Just curious... Why not simply \textit the whole thing? –  T. Verron Feb 14 '13 at 9:27
    
You could use \textnormal{\itshape ...}, but (a) \textit on its own will change in a bold context, (b) there is no guarantee that the text itshape font is the same as the mathit font –  Andrew Swann Feb 14 '13 at 9: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 9: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 \mathit starts, after some work, with \bgroup, followed by the font selection internal command; TeX accepts _\bgroup...\egroup as equivalent to _{...} –  egreg Feb 14 '13 at 14:12
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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