Math & ScienceIs there any way to define -> as rightarrow?

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
Incantator
Posts: 12
Joined: Wed Feb 18, 2015 7:55 am

Is there any way to define -> as rightarrow?

Post by Incantator »

Hello, I'm recently learning LaTeX. What I want is, when I write \-> in math mod, it will turn into \rightarrow. I first tried this:

Code: Select all

\newcommand{\->}{\rightarrow}
I didn't expect it to work anyway; it seems like -> cannot be recognized as a group for a new command. And I'm also not sure if this will work INSIDE math mod.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Is there any way to define -> as rightarrow?

Post by Johannes_B »

LaTeX knows letters, and other stuff. Commands like \section or \tableofcontents consist of the escape character, which is a backslash by default, and a series of letters. For the sake of remembering (mnemonity, this word should exist) those are english words.

On the other hand, we have commands that take one non letter. For example \& or \,.

You could do some magic stuff, make the - active and use -> to get the symbol, but please, please don't.

You want to save typing, you are lazy. I am lazy too. Well, some would call it efficient. The day will come that you are giving your file away. The person reviewing your code will be most confused.
The best way is to get a text editor, that helps you to save time. A good editor can map your -> input and directly convert it, but ... There is a pretty high chance, that you will want -> (exactly those two charackter) sooner or later. For example in TikZ pictures. TikZ is a nice thing, please have a look at some examples.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10323
Joined: Mon Mar 10, 2008 9:44 pm

Is there any way to define -> as rightarrow?

Post by Stefan Kottwitz »

Yes, such shortcuts can break other things and it doesn't improve readability. For example, lengthy TikZ code is nice to read but condensed xypic drawing code is extremely hard to understand.

The next logical requirement would be supporting <- too. Which means, making - an active character (command) would not suffice. I would stick with classic commands. Though you could define shortcuts \ra and \la. But it's hardly memorable over time, especially if you would do it with many things.

You could use editor keyboard shortcuts, or auto-completion. Or a preprocessor such as with arara, which does the replacement automatically before the LaTeX run. If you just started with LaTeX, there are many other useful things to learn than such advanced things. Even I don't use arara or preprocessing yet, after about 20 years of using LaTeX, though I'm open for new things.

Stefan
LaTeX.org admin
Incantator
Posts: 12
Joined: Wed Feb 18, 2015 7:55 am

Re: Is there any way to define -> as rightarrow?

Post by Incantator »

Thanks for your explanations!
Post Reply