5

I have a very basic question which concerns \renewcommand.

How dangerous it is to use it?
In other words, does the usage of such command can have effects in terms of the stability of the compilation of a file?

Thanks a lot for any feedback.

PS: I am not sure I explained myself.

7
  • 1
    Of course it will cause problems if you introduce errors within your \renewcommand or change the number of arguments of an existing macro, i.e. a former \newcommand{\foo}[1]{...} would become \renewcommand{\foo}[2]{...} -- this will potentially lead to problems as well as \renewcommand{\foo}{...}, leaving \foo{Some stuff} being \foo and then some text Some stuff that at best is typeset, at worst it appears in the preamble, breaking compilation
    – user31729
    Commented Dec 31, 2017 at 14:56
  • 1
    In short: Before doing \renewcommand{\foo} you should know the old meaning of \foo first, then act wisely ;-)
    – user31729
    Commented Dec 31, 2017 at 15:00
  • 1
    Try \usepackage{color}\renewcommand{\box}[1]{\colorbox{red}{#1}}
    – egreg
    Commented Dec 31, 2017 at 15:00
  • 1
    \renewcommand{\box} is a sledge-hammer in order to show the wrong use, isn't it? ;-)
    – user31729
    Commented Dec 31, 2017 at 15:02
  • 2
    Just adding that in any case is good to use \ifdefined or \ifcsname before "renew" the command that could be just a "new" command (if a package haven't be loaded and we didn't thought of that possibility... etc)
    – koleygr
    Commented Dec 31, 2017 at 15:43

4 Answers 4

8

Apart from the funny

\renewcommand{\fi}{Firenze} % a handy abbreviation

that will produce the amazing error message

! LaTeX Error: Missing \begin{document}.

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

l.8 \begin{document}

or something like

\renewcommand{\box}[1]{\colorbox{red}{#1}}

that makes TeX suffer until giving up the ghost with

! TeX capacity exceeded, sorry [grouping levels=255].
\rlap #1->\hb@xt@ \z@ 
                      {#1\hss }

there are many other commands that should not be redefined under any circumstances. Note that \fi and \box are TeX primitives, so problems are expected if one changes their meaning. However also “handy” abbreviations such as

\renewcommand{\a}{\alpha}
\renewcommand{\b}{\beta}
\renewcommand{\c}{\gamma}

a colleague of mine was fond of are likely to produce mysterious effects. In a frantic email he asked me for help because his paper with a Turkish coauthor refused to typeset. Guess what? The coauthor's surname started with “Ş”, which is quite common a letter in Turkish. Typesetting “Ş” relies on \c being unchanged even if direct UTF-8 input is used. By the way, also \a and \b should not be redefined.

In some cases \renewcommand is however mandatory: if you want to change the interline skip in tables or arrays, you should indeed use

\renewcommand{\arraystretch}{<new factor>}

(the default definition assigns the value 1).

There are cases when \renewcommand is safe, I usually mention

\renewcommand{\phi}{\varphi}
\renewcommand{\epsilon}{\varepsilon}

in order to be sure to use the letterforms which are more common in continental Europe.

In other cases one must use the same care an elephant should in a glassware shop: move very carefully.

3
  • 1
    @Kolmin Did you like the examples? :-)
    – egreg
    Commented Dec 31, 2017 at 17:22
  • Actually, I did! :D... and – of course – I did conceive the idea of getting "handy" and "innocuous" abbreviations such as the \a and \b you mentioned.
    – Kolmin
    Commented Dec 31, 2017 at 19:20
  • @Kolmin Been there, done that. And then learned not to. ;-)
    – egreg
    Commented Dec 31, 2017 at 22:24
4

It depends what you use \renewcommand on! In general, LaTeX doesn’t provide any “safety” here; if you redefine a macro or even primitive that LaTeX relies on you could end up with either obvious errors or (probably worse) silent problems in the final outcome.

Having said that, in many cases it is expected that you use \renewcommand to alter behaviour, so I wouldn’t say that one should avoid using it.

0
4

A stupid sample document what could happen if \renewcommand is used without inspection of the previous code:

Usually \addcontentsline does have 3 arguments, so \addcontentsline{toc}{section}{Some text} will leave Some text in the ToC, formatted as section entry.

However, \renewcommand{\addcontentsline}{Oh my!} removes all three arguments of the macro, leaving their content in the input stream.

\documentclass{article}


\begin{document}

\tableofcontents

\section{Foo}

\renewcommand{\addcontentsline}{Oh my!}

\section{Foobar}
\end{document}

So, in the very end it is quite recommendable to look the definition of a macro up before trying to \renewcommand.

This can be done either by looking into class or package codes (or at latex.ltx, for example), reading the documentation or saying

\meaning\foo in the document body oder \show\foo (can be done in the preamble as well

enter image description here

1
  • Thanks a lot for your feedback above and for this answer. So here there is something that always makes me wonder: as you write first of all we should know what is the old meaning of what we want to redefine. The question is: How do we find out? Is there some shortcut to get it explicitly with some command? Thanks a lot (and happy new year).
    – Kolmin
    Commented Dec 31, 2017 at 15:53
2

You can always use \show to see the existing definition of a command (if it is defined). That can be done in Preamble or in the document body. The result will be displayed in your console (or terminal), then the program will halt.

For example, using LuaLaTeX (with utf-8 encoding, \show\a replies that it is a macro, defined as:

 \expandafter \@changed@cmd \csname \string #1\endcsname \relax

So I look at that, and I ask myself, is that something I wish to redefine? Probably not! It looks essential, and I'm not quite sure what it does.

On the other hand, \show\i yields:

TU-cmd \i \TU\i

I happen to understand what that means, and it is something I am not likely to need in my own work. So, I can consider renewing that command, if \i would be a handy macro in my own code.

3
  • It’s the other way around: \a is only used in the tabbing environment, so not really essential. On the other hand, \i is part of LICR (LaTeX internal character representation), so it’s in the same class as \c that I mentioned in my answer.
    – egreg
    Commented Jan 1, 2018 at 9:44
  • @egreg Yes, but tabbing is unfamiliar to me. As for \i, I do not need dotless i, and I don't do math. So, what works for one user may be poison to another.
    – user139954
    Commented Jan 1, 2018 at 16:33
  • 1
    When you'll have to cite some Turkish author, you'll know why it's better not redefining \i.
    – egreg
    Commented Jan 1, 2018 at 16:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .