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.

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!

share|improve this question
    
Welcome to TeX.SX! A tip: You can use backticks ` to mark your inline code as I did in my edit. –  Adam Liter Nov 26 '13 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 Nov 26 '13 at 8:13
add comment

2 Answers

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

share|improve this answer
    
Thank you very much! –  Halfdan Nov 26 '13 at 3:00
6  
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 Nov 26 '13 at 8:57
    
@egreg You remind me of my school days :-) Really here no advantage. I will add it to the answer. Thanks. –  Harish Kumar Nov 26 '13 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. –  tohecz Nov 26 '13 at 14:05
1  
@daleif you mean across ~\\~\\? Well, you don't wanna know what one comes across as a typesetter... –  tohecz Nov 26 '13 at 21:45
show 3 more comments

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.
share|improve this answer
add 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.