4

I want to change the function of some commands consisting of only one letter, such as \e or \i, and make them abide by MY rules. (Mouhahaha!) I have done this before with other commands like \in or \=, but it seems that one-letter commands are special for some mysterious reason.

In particular, I'd like to make a new command \i to use it in math-mode, but this produces an error.

Here is just a part of my document:

\documentclass[12pt]{article}

%   % symbols
\let\i\undefined
\newcommand{\i}{\mathrm{i}}


% begin languages
\usepackage[LGR,T1]{fontenc}


\begin{document}

$\i$ $i$ i

\end{document}

I get the error

Command \i invalid in math mode

The .pdf file is, nevertheless, produced and \i appears as the greek letter beta. I tried other ways (which now I cannot recall) that had \i to appear as a dotless i instead of beta.

And why don't I get the same error with \e that also has a predefined meaning?

David Carlisle
  • 757,742
Jim
  • 535
  • 3
    Didn't the two answers and several comments stating Don't do that in your other question make you a bit suspicious thinking about Why shouldn't i do it? instead of Now, ireally want to do that and mess up everything. – Johannes_B Dec 12 '15 at 18:03
  • 3
  • Hahahahahahhahahaha! Well...! I was discovered! ^_^

    My problem is that I am not generally satisfied with the solution if I don't have completely understood the problem itself. By posting this question, I hoped to get an explanation about how one-letter commands really work and why it is so difficult to redefine them. The truth is I was not persuaded that I should not mess with them. So, here what I ask is "Why?".

    – Jim Dec 12 '15 at 18:16
  • 1
    Diacritics are done using one letter comands (at least some), so that functionality will be gone. – Johannes_B Dec 12 '15 at 18:19
  • And why can't I redefine them?! (Yes, I tried! A lot! It failed!) My initial problem at Error when trying to change imaginary unit's style has been already solved. I'm just curious now. – Jim Dec 12 '15 at 18:29
  • Have you tried \renewcommand{\i}{\iota} (say)? I don't have a problem with this... – Werner Dec 12 '15 at 18:39
  • 1
    I think you took too much meaning in the comment someone made about one letter commands. the intent of the comment was "don't redefine latex's internal workings" it just happens that the example you used was a one letter command and many other one letter commands are similarly already used. – David Carlisle Dec 12 '15 at 18:41
  • It was not only because of that. I found another strange case with them, but I cannot track it back now... – Jim Dec 12 '15 at 18:44
  • 3
    I usually recall the (true) case of a colleague who asked my why the name of his coauthor was not accepted by LaTeX. Well, the coauthor was Turk and his name contained “ç”, but my colleague had redefined \c. – egreg Dec 12 '15 at 19:00
  • you have asked two questions about this now but have still shown no code that produces the error that you are asking about (which would not occur by default) – David Carlisle Dec 12 '15 at 21:58
  • @DavidCarlisle In my other question I have given a primitive code I had been trying at the time. The present question has been updated with relative details. – Jim Dec 12 '15 at 22:45
  • 3
    no an example would start \documentclass and have the minimal number of packages needed and then would have code that caused tex to give the error message. Without that you are asking us to debug unseen code. – David Carlisle Dec 12 '15 at 22:55
  • @DavidCarlisle The preamble was chaotic. (That's why I didn't posted an explicit code from the beginning. Sorry for the inconvenience.) I reduced it to only the packages and some other definitions I have. – Jim Dec 13 '15 at 00:17
  • 1
    It isn't hard to delete packages until the error goes away, if you had done that you'd get something more like the edited example above, where you will find that if you remove fontenc you would not get the error that you show, since you never said before you were using fontenc it isn't surprising no one told you the exact cause of the error. – David Carlisle Dec 13 '15 at 00:24
  • 1
    as I said in a comment to the original question \i is an encoding-specific command. So it gets defined (again) when you define an encoding (T1 and LGR in your case) the fact that it is single letter is not relevant. – David Carlisle Dec 13 '15 at 00:30
  • Ok, sorry to bother you with that. However:

    Why doesn't \e give that same error?

    It seems to matter whether the package is before or after the new command! Is that how it is supposed to work?

    Regardless, there is no i nor \i in greek. What is exactly the problem?

    – Jim Dec 13 '15 at 01:02
  • If you really want to redefine one-letter commands, there is a safe way to do it. Run LaTeX from a script that converts them into longer commands using sed before feeding the file to LaTeX. Presto! Note that you will have to go through included files recursively for multi-file documents. – Zorgoth Aug 15 '17 at 17:31
  • 1
  • @user202729 Thanks! This looks interesting! – Jim Jun 27 '22 at 15:42

2 Answers2

12

There is nothing special about one letter commands you can (re)define them the same as multiletter commands. The point is that most of them are already defined and unless you are certain that you are not breaking latex, redefining latex's internals is not normally recommended.

You did not show any reproducible code to show the error that you showed in your previous question. Your actual redefinition redefined \i and it would have worked in math mode at that point. However \i is (normally) an encoding-specific command and probably some package you loaded reset those commands at \begin{document} so over-writing your definition.

But the solution is not to ask how to redefine latex's inner workings without breaking it, just use a different name.

David Carlisle
  • 757,742
10

Consider the following document:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}

\renewcommand{\i}{\mathrm{i}}

\begin{document}

El mínimo de la función $f(x)=1+x^{2}$ es $1$.

\end{document}

Run LaTeX over it to get

! LaTeX Error: \mathrm allowed only in math mode.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.10 El mí
           nimo de la función $f(x)=1+x^{2}$ es $1$.

This is just an example of why you don't want to redefine \i. There are many other situations where redefining an internal gives seemingly mysterious errors.

Another example (similar to one presented to me by a colleague)

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}

\renewcommand{\c}{\mathrm{c}} % a constant

\begin{document}

\title{Mínimo de una función}
\author{Leonardo Castro and Şaban Atatürk}
\maketitle

El mínimo de la función $f(x)=\c+x^{2}$ es $\c$.

\end{document}
egreg
  • 1,121,712
  • This is indeed enlightening! However, in your (first) example there is no \i in math mode. But if you do put \i into the dollars LaTeX gives the error Command \i invalid in math mode and ignores the new definition you have assigned to it. How can you make LaTeX understand you don't care about its previews meaning? – Jim Dec 12 '15 at 20:00
  • 3
    @Jim In the first case, \i in math mode does not raise an error. The examples want to show that one doesn't want to redefine these commands. – egreg Dec 12 '15 at 20:20