16

I'ld like to make -> a shortcut for \rightarrow. Is that possible with Plain TeX or has it to do with the loaded font?

Mico
  • 506,678
lvaneesbeeck
  • 3,095
  • 2
    Short answer: This is not possible. Longer answer: You can do it, but either you break everything relying on the use of - (including dimension computation) or you re-create the font tables, which is quite complicated and makes your document not portable. But I'm not an expert on fonts in LaTeX. And very likely this is easily doable in LuaLaTeX. – yo' Oct 30 '13 at 13:53
  • 2
    Another question is what do you wan to use it for? I've used -> as a separator in macros, that is quite useful. As in \def\test #1 -> #2\par{#1 $\rightarrow$ #2\par} or similar. – daleif Oct 30 '13 at 13:55
  • @daleif the aim is to lighten source writing, especially when \Leftrightarrow are common (and write sources with the obvious <=>). Also I haven't seen anywhere how ligatures are defined so I'm curious of the user is allowed to.

    But maybe the most convenient way is to redefine the arrows with shorter names (for example \to and \ot for left/right arrows, \tto and \ott for the double arrows)

    – lvaneesbeeck Oct 30 '13 at 16:05
  • 2
    Well, \to actually exists. Defining \ot doesn't sound sooo bad. Then \implies and \iff exist, and you can define \impliedby. IMHO that makes the code quite readable ;) – yo' Oct 30 '13 at 17:15
  • 1
    Apart from amsmath and other LaTeX packages for the symbols themselves, this is already done in How could LaTeX replace the tokens <= by the command \leq?, isn’t it? – Qrrbrbirlbel Oct 30 '13 at 20:08
  • The simplest LaTeX solution is to use the semantic package. The ligature you cite it's even provided as a default(although you can turn this off) so you don't even have to use \mathlig{->}{\rightarrow} explicitly. – Bakuriu Oct 30 '13 at 20:47
  • @Qrrbrbirlbel - The OP has asked for a PlainTeX, not a LaTeX, solution. – Mico Oct 30 '13 at 20:59
  • The plain tex file mathlig.tex can be input and then the command \mathlig{->}{\rightarrow} works. But my recommendation would be to define short macros to save typing. – Dan Oct 30 '13 at 22:21

2 Answers2

15

Make - math active and assign it a suitable definition.

\edef\minus{\mathchar\the\mathcode`-\space}
\def\lookaheadminus{\futurelet\next\whatsafter}
\def\whatsafter{%
  \ifx\next>%
    \expandafter\togr
  \else
    \expandafter\minus
  \fi}
\def\togr>{\to}
\begingroup\lccode`~=`-
\lowercase{\endgroup\let~}\lookaheadminus
\mathcode`-="8000

$A->B$ and $a-b$

\bye

enter image description here

Then avoid doing it.

You might enjoy using UTF-8 characters for this:

% <E2> is one of the prefixes for three byte UTF-8 sequences
\catcode"E2=\active
\protected\def^^e2#1#2{\csname\detokenize{^^e2#1#2}\endcsname}
%% similar definition to the above should be made for
%% other prefixes

% the following defines a single Unicode character
\def\defineutfchar#1{%
  \protected\expandafter\def\csname\detokenize{#1}\endcsname}

\defineutfchar{→}{\to}

$A→B$

\bye
egreg
  • 1,121,712
  • 1
    To this correct answer, I would add that it is very easy to type the → character if you can configure a compose key on your keyboard. (Otherwise you would probably have to rely on some kind of palette character to type the →.) – Michaël Le Barbier Oct 30 '13 at 22:26
  • You define the macro \togr, which I think is a typo of \toarg. – Charles Stewart Nov 01 '13 at 10:12
14

A humble, faulty attempt, inspired by tohecz, and mainly based on my answer to Censoring Curse Words with Grawlixes:

The converter.lua file:

symbols = { "%-%>", "%=%>", "%<%-", "%<%=" }
replacements = { ["%-%>"] = "\\rightarrow ", ["%=%>"] = "\\Rightarrow ", ["%<%-"] = "\\leftarrow ", ["%<%="] = "\\Leftarrow " }

function replace(line)
    for _, element in pairs(symbols) do
        if string.find(line, element) then
           return string.gsub(line, element, replacements[element])
        end
    end
    return line, 0
end

function converter(line)
    occurrences = 0
    repeat
        line, occurrences = replace(line)
    until occurrences == 0
    return line
end

callback.register('process_input_buffer', converter)

Then, in the main TeX file:

\directlua{dofile('converter.lua')}

Hello world, $A -> B$, $A => B$, $B <- A$, $B <= A$.

\bye

Of course, this solution requires LuaTeX. :) The output:

Image

I can point at least one problem:

  • Naive replacement algorithm, it's just based on pattern replacement. There might be parts of the text where things will break terribly.

An improvement of this code might be a good exercise for the reader. :)

Paulo Cereda
  • 44,220