24

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.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 3
    \text{max-color}, assuming you are using \usepackage{amsmath}. – Peter Grill Feb 14 '13 at 04:58
  • That appears to change the font, I am the variable name no longer looks like other math symbols. – Shamim Hafiz - MSFT Feb 14 '13 at 05: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 05: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 - MSFT Feb 14 '13 at 05:08
  • My recommendation would be to define \newcommand{\MaxColor}{\text{max-color}} and then use \MaxColor instead. 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:10
  • Am I missing something!!!? max-color in math mode means m*a*x-c*o*l*o*r. – hpekristiansen Feb 14 '13 at 05:16
  • @Hans-PeterE.Kristiansen: I agree. That was why I recommended not to use max\text{-}color. – Peter Grill Feb 14 '13 at 05:32
  • 1
    \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
  • Btw in unicode there are seperate characters for minus (−, U+2212) and hyphen (-, U+002D). But then there is also the En-Dash, which differs from the minus only by its vertical position (–, U+2013). Compare: -–−+. – bodo Feb 14 '13 at 06:52

2 Answers2

14

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.

Andrew Swann
  • 95,762
  • Just curious... Why not simply \textit the whole thing? – T. Verron Feb 14 '13 at 09:27
  • 2
    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 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 \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
  • @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
12

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

egreg
  • 1,121,712