10

I'm trying to figure out how best to define an alias. I have some moderately complicated variable names, such as $p^{i,j}_{t}$, and having to type them over and over again is a pain. More importantly, if I decide later to change my naming convention, I'd have to go back and search replace in my whole document, which seems ridiculous. I've tried to use this method:

\newcommand{\varn}{$p^{i,j}_{t}$}

but this method is not powerful enough. First of all, I cannot use it in Math mode, and would have to define another command

\newcommand{\mathvarn}{p^{i,j}_{t}}

which seems redundant.

Second of all, latex doesn't know that it's supposed to insert a whitespace after I use the command in a text environment. If I force a whitespace in the command, I cannot use the command just before punctuation like full stop, or comma.

Any suggestions? Thank you!

2
  • Welcome to TeX.SX! A tip: You can use backticks ` to mark your inline code as I did in my edit.
    – Adam Liter
    Commented Nov 26, 2013 at 2:36
  • 3
    It is a math expression, so it should always be written with $-signs, embedding the $ will make the code harder to read. Define it without $'s and use $\varn$.
    – daleif
    Commented Nov 26, 2013 at 8:13

2 Answers 2

12

You can use \ensuremath Running for cover, egreg will come soon.

\documentclass{article}
\newcommand*{\varn}{\ensuremath{p^{i,j}_{t}}}

\begin{document}
\varn  \qquad    $\varn$

Here is how \varn{} is used in text again \varn.

\end{document}

Fir the second part, you may insert an empty atom {} after \varn like \varn{} when you need a space. This is the usual behaviour. Thre is also xspace package, but it may fail some times.

\documentclass{article}
\usepackage{xspace}
\newcommand*{\varn}{\ensuremath{p^{i,j}_{t}}\xspace}
\begin{document}
\varn  \qquad $\varn$

Here is how \varn is used in text again \varn.

\end{document}

enter image description here

As noted by egreg, it won't take much work to write $\varn$ over \varn. It is also more symatec for you identify math code. So better define \newcommand*{\varn}{p^{i,j}_{t}} and use $\varn$.

7
  • 10
    Write 100 times on the blackboard "I'll not mention \ensuremath for two months" and send a photo on chat when you're done. Seriously: what's the advantage of ...how \varn is... over ...how $\varn$ is? I see none.
    – egreg
    Commented Nov 26, 2013 at 8:57
  • @egreg You remind me of my school days :-) Really here no advantage. I will add it to the answer. Thanks.
    – user11232
    Commented Nov 26, 2013 at 9:24
  • 1
    I will not downvote, but the two concepts you just used --- \ensuremath{...}\xspace --- are of the ugliest things you can ever do in LaTeX, pretty much fighting usage of `~\\~\` for vertical white-space.
    – yo'
    Commented Nov 26, 2013 at 14:05
  • @tohecz you've come across those as well?
    – daleif
    Commented Nov 26, 2013 at 14:15
  • 1
    @daleif you mean across ~\\~\\? Well, you don't wanna know what one comes across as a typesetter...
    – yo'
    Commented Nov 26, 2013 at 21:45
6

Please, never try to "get rid of the 'additional dollars'." First, you easily mis-interpret your math as text. Second, this then leads to doing things like \varn=0, which, in text mode, becomes $p^{i,j}_{t}$=0, and this get completely wrong spacing. So, you should define

\newcommand{\varn}{p^{i,j}_{t}}

and then use it as in:

We have that $\varn=0$ if and only if the guy $\varn$ is really zero.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .