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?

  • 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
  • 1
    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
14

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
  • 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 →.) – Michael Le Barbier Grünewald Oct 30 '13 at 22:26
  • You define the macro \togr, which I think is a typo of \toarg. – Charles Stewart Nov 1 '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. :)

  • 1
    Well, but that's not your mistake. Of course, how can LuaTeX know what $n<-1$ is supposed to mean? :) – yo' Oct 30 '13 at 17:13
  • @tohecz: awwww ♥ – Paulo Cereda Oct 30 '13 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.