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.

Is there a way to define a macro that does not start with \? for example, I'd like to have -> expand to \rightarrow since I like reading raw LaTeX and $\eps -> 0$ reads much nicer (to me...) than $\eps\rightarrow0$ or even (the solution I have now) $\eps\goto0$.

I guess one could define - as an escape character and then use it to read the next character, if it is a > expand the whole shebang to \rightarrow and if not expand the - to a - and let the next character to its thing...I have no idea if what I just wrote makes any sense, nor how one would do it nor the possible pitfalls and reasons to avoid such behavior....

Ideas? Comments?

share|improve this question
3  
I'd say that ε → 0 looks even nicer ;-) –  Stéphane Gimenez Oct 4 '11 at 21:00
    
@Stéphane: I notice the wink, but seriously, I'll say that I prefer ascii... :-o –  Yossi Farjoun Oct 4 '11 at 21:05
2  
This might be a duplicate: look up "mathcode". –  Bruno Le Floch Oct 4 '11 at 21:52
    
yes...very similar to this –  Yossi Farjoun Oct 4 '11 at 22:17
1  
@StéphaneGimenez: This is why I love unicode-math. –  Caramdir Oct 4 '11 at 22:43
add comment

6 Answers

up vote 13 down vote accepted

You can decide to make - "active" only within math mode, and look ahead for a >.

\documentclass{article}
\mathchardef\minuschar=\mathcode`-\relax
\mathcode`-="8000\relax
\begingroup\makeatletter\lccode`~=`-\lowercase{\endgroup
  \def~{\@ifnextchar{>}{\rightarrow\@gobble}{\minuschar}}}
\begin{document}
We can try $-\int$, $-\sum$, $x->0$, $x- >0$ etc.
\end{document}

When TeX encounters a character with catcode 11 or 12 (letter or other) in math mode, it looks up the character's mathcode. That's a number between "0000 and "8000 (hexadecimal). The value "8000 is treated specially to mean "use the corresponding active character instead". I then defined - to look ahead for a >, and either put a \rightarrow and \@gobble the >, or put the minus character.

share|improve this answer
    
Small bug - lccode is reversed. Unforutunately comment markdown won't let me write it correctly, but it should be lccode -= ~. –  Justin Bailey Oct 4 '11 at 22:01
3  
I imagine @Bruno will fix it soon. In any case, in the following replace single quotes (') with a grave quote (```). @Bruno's macro has the lccode definition reversed. It needs to be: \lccode'~='-. Remember, '=` in the preceding. –  Justin Bailey Oct 4 '11 at 22:17
2  
Don't use \everymath for setting the mathcode of - to "8000. Just define \minuschar before setting \mathcode`-="8000 globally (the \relax is at least redundant anyway, a space will do better, as it is ignored, while \relax isn't). Also \minuschar can be more efficiently defined as \mathchardef\minuschar=\mathcode`-. –  egreg Oct 4 '11 at 23:53
    
Justin, egreg: you are both right. Edited to fix that. –  Bruno Le Floch Oct 5 '11 at 0:02
    
The two \relax are still redundant. :) The end of line is all that's needed. –  egreg Oct 5 '11 at 0:03
show 5 more comments

There's a package which provides exactly this service, called semantic. It's designed for typsetting programming language semantics, so it provides five things: math ligatures; inference rules (with horizontal lines); T-diagrams (for representing compositions of compilers and interpreters); typesetting for reserved words; and default shorthands. Obviously, we care about the two in bold.

Math ligatures let you do precisely the following:

\documentclass{minimal}

\usepackage[ligature]{semantic}

\mathlig{->}{\rightarrow}

\begin{document}
  \[ a_{>} -> b_{-} \]
\end{document}

This will typeset just the same as writing \[ a_{>} \rightarrow b_{-} \].

You can define ligatures to be arbitrary sequences of characters (for precise details, check the documentation), and if you specify the [shorthand] option to the package instead, it will define a default set (again, the documentation has the list).

As for limitations, there's only one that I've run into: having b_- alone doesn't work (whether or not it's at the end of math mode), because - is an initial character of a ligature (for instance, a_> works fine). I have no clue why this is. I would also guess that redefining - as an active character would break, but I've never tried. Honestly, I've never run into it before, but now that someone mentioned similar things I thought I'd check.

share|improve this answer
    
very cool package...thanks! –  Yossi Farjoun Oct 5 '11 at 5:21
add comment

The easiest solution, as was observed in one of the comments is to directly type . Both ConTeXt MkIV and LaTeX + unicode-math give the right output when using the correct unicode input.

If you are using LuaTeX, it is also possible to parse the input and convert -> to . Here is an example in ConTeXt that uses the translate module

\usemodule[translate]

\translateinput[->][→]
\translateinput[=>][⇒]

\enableinputtranslation

\starttext
$x - y = 0 => x = y$
\stoptext    
share|improve this answer
    
I assume that it is possible to specify as \to for those of us who like ascii characters? –  Bruno Le Floch Oct 5 '11 at 2:41
    
@BrunoLeFloch: Yes, using \to also works fine. –  Aditya Oct 5 '11 at 2:47
add comment

Just for the record,

\usepackage{newunicodechar}
\newunicodechar{→}{\to}

allows for

$x→0$

with pdflatex (the file must be coded in UTF-8 and \usepackage[utf8]{inputenc} must be used, not utf8x). It also works with xelatex or lualatex (without unicode-math, of course, otherwise it would be nonsense).

share|improve this answer
add comment

It's certainly possible to assign a character other than \ (backslash) to function as the start a control word (macro) in TeX. But, what would you gain? Even if all macros started with a ~ (tilde) character instead of a \, it wouldn't affect the length of the strings used to denote various commands, would it?

By the way, \to is a synonym for \rightarrow. Is \to short enough (and readable enough) for you? :-)

Sort-of Lengthy Addendum To make the specific character combination -> into a "command" that typesets the equivalent of \to in math mode, you'd have to make the first character, -, "active" in TeX jargon and then define a macro that reads ahead to the next character; if that character is a >, you'd tell TeX to typeset \rightarrow, and if it's not, you'd instruct TeX to place the - character and the following character back into the input token stream.

Now, back to the question of how to make the "instruction" -> execute the instruction \rightarrow. Study the code in the MWE below; the \catcode command makes the - active, and the following line provides the definition of the command. Note that we can't just place - back into the token stream, as TeX would find itself in an ever and deeper loop trying to resolve what to do with the command -. Instead, I use the command \char command to place a dash back into the token stream.

\documentclass{article}
\catcode`\-=\active
\def-#1{\ifx>#1 \ensuremath{\rightarrow} \else \char`-#1 \fi}

\begin{document}
-> \quad -+ \quad -, \quad $-a$ \quad 5--9 \quad $x->0$
\end{document}

enter image description here (Well, I'm sure I've left out some eventuality that'll make this code break down in some circumstances; I'll let our fellow TeX-SX hounds point out the mistakes...)

share|improve this answer
    
the main issue is not the length, but the readability. I can ofcourse define \t to be \rightarrow, but I want -> to be \rightarrow...or rather I'm interested in finding out how it could be done....I certainly do not want to change anything else...I do not want all macros to start with a tilde or any other character. –  Yossi Farjoun Oct 4 '11 at 21:16
    
Please see the addendum to my answer. –  Mico Oct 4 '11 at 22:46
1  
Active characters are a mess. For example, the above will fail with $x_{-}$. Such things are more easy to handle using luatex where you can parse the input. –  Aditya Oct 4 '11 at 23:37
    
@Aditya, could you post an example in luatex? It would be very interesting! Your comment is one reason for using \@ifnextchar to test for the presence of >. Also, making - active only in math mode helps with the fact that otherwise - cannot be used in lengths etc. –  Bruno Le Floch Oct 5 '11 at 0:00
1  
Setting \catcode`~=0 doesn't make \to and ~to different. Moreover, activating - would break many things: with your definition, any space after - would be eaten up and, as Aditya observes, x_{-} will give an error. –  egreg Oct 5 '11 at 0:21
show 2 more comments

You can always make - an active character and assign a macro to it. Below I use a "delimited" macro to make -> produce the "\rightarrow" token. The problem is - is redefined everywhere. You normally need to have some sort of scoping macro that turns active characters on and off:

\documentclass{article}
\begin{document}
\catcode `-=\active\def->{\ensuremath{\rightarrow}}
hello ->.
\end{document}

Search for "catcode" and "active characters" to learn more.

share|improve this answer
    
Right, so this does the first part...but leave a mess...since now - is active and I cannot use it anywhere except in a ->....so, while this is a step in the right direction...its definitely the easier step... :-) –  Yossi Farjoun Oct 4 '11 at 21:44
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.