# Can someone make my command for formatting variable names work better?

So, I've got this command that I use to format variable names in my homework:

\newcommand*{\var}[1]{\ensuremath{\mathord{\mathtt{#1}}}}


The problem is that it doesn't handle subscripts smoothly. I end up typing this frequently:

\var{Something}_{\var{SomethingElese}}


Which is a bit cumbersome. Can I make it work with:

\var{Something_SomethingElse}


or at least:

\var{Something}_\var{SomethingElse}

-

## migrated from stackoverflow.comNov 30 '12 at 11:55

This question came from our site for professional and enthusiast programmers.

Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Nov 30 '12 at 15:03

You must enclose the subscript in a brace/group to indicate the scope it. Therefore, using

\newcommand*{\var}[1]{$\mathtt{#1}$}


is sufficient:

\documentclass{article}
\newcommand*{\var}[1]{$\mathtt{#1}$}
\begin{document}
\var{Something_{SomethingElse}}
\end{document}

-

-