8

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?

Yossi Farjoun
  • 13,274
  • 10
  • 74
  • 96

7 Answers7

15

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.

  • Small bug - lccode is reversed. Unforutunately comment markdown won't let me write it correctly, but it should be lccode -= ~. – Justin Bailey Oct 04 '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 04 '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 04 '11 at 23:53
  • Justin, egreg: you are both right. Edited to fix that. – Bruno Le Floch Oct 05 '11 at 00:02
  • The two \relax are still redundant. :) The end of line is all that's needed. – egreg Oct 05 '11 at 00:03
  • @egreg: true, but I'm used to \catcode\endlinechar=9 in LaTeX3 catcodes, so it is safer that way. The only disadvantage of \relax here is that it slows TeX down just a tiny tiny bit. – Bruno Le Floch Oct 05 '11 at 00:05
  • Bruno and @egreg: I think this code won't typeset the expressions $-\int$ or $-\sum$ properly: the symbols that show up are (minus sign followed by) puny code table symbols rather than the result of the "normal" $\int$ and $\sum$ commands. :-( – Mico Oct 05 '11 at 00:55
  • @Mico: Surprising. I don't get that: see edit. – Bruno Le Floch Oct 05 '11 at 02:35
  • Indeed, with the latest version these issues have disappeared. :-) I may have run, perhaps, an earlier version of your code. – Mico Oct 05 '11 at 02:43
  • @Mico: good. Sorry that it took long to converge to good code. – Bruno Le Floch Oct 05 '11 at 03:44
9

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    
Aditya
  • 62,301
7

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

egreg
  • 1,121,712
7

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.

4

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

Mico
  • 506,678
  • 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 04 '11 at 21:16
  • Please see the addendum to my answer. – Mico Oct 04 '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 04 '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 05 '11 at 00: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 05 '11 at 00:21
  • @Bruno: See my answer that uses the translate module for ConTeXt. The implementation is only a few lines that are part of m-translate.mkiv, but it builds upon some of the ConTeXt infrastructure. – Aditya Oct 05 '11 at 00:55
  • @egreg and others: You guys are absolutely correct! I had a sinking feeling I had not thought through all circumstances... Anyway, for my own papers, I'll happily stick with typing $\to$! – Mico Oct 05 '11 at 00:57
1

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.

  • 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 04 '11 at 21:44
0

Since you mentioned reading sources, I can suggest an alternative: set up your editor to display \rightarrow as .

Emacs 24.5 with AucTeX does this if you enable prettify-symbols-mode:

prettification

(left: without prettify-symbols-mode; right: with prettify-symbols-mode. Same text in both buffers)

Clément
  • 4,004