In LaTeX2e, Is it possible to redefine a command, yet have the scope of the redefinition apply itself only outside math mode? For instance, I'd like to redefine \sec to \section, but unfortunately \sec has a special meaning inside math mode (one that doesn't look exceptionally useful, yet still exists), which I would like to preserve when entering said mode.

  • Doen't stick a screwdriver in your knee. Use a decent editor assisting you in your daily work.
    – Johannes_B
    Oct 28, 2015 at 7:31
  • 1
    Sorry, I'm confused. How would a different editor choice help with this?
    – Jason
    Oct 28, 2015 at 7:37
  • You wanna be efficient and save time, right? Redefining LaTeX macros isn't the way to go. Getting a powerful editor is.
    – Johannes_B
    Oct 28, 2015 at 7:44
  • 1
    Now I understand, it's all about allowing auto-completion features of editors like TexMaker. Yes, I completely agree that such simple redefinitions are agnostic towards the presence of such features. Yet it is also the case that I tend to collaborate on .tex sources with people that use various editors on different OSs. I can therefore not safely rely on assumptions about editor functionality, and would rather incorporate cross-platform capacity by virtue of command definitions and redefinitions.
    – Jason
    Oct 28, 2015 at 8:19
  • 2
    If you collaborate with various peoples it is not a good idea to use new names for standard commands like section. You force them to learn two names for the same thing. Also if they use decent editors loose highlighting and so the source is less readable for them. Oct 28, 2015 at 8:26

1 Answer 1


Can it be done?

What you ask can technically be done, with some care.

First you have to define a macro equivalent to the current \sec; in this particular case, \let\latexsec=\sec is good (provided it is issued after amsmath is loaded).

Second, you can redefine \sec. The first attempt,


fails as soon as \sec appears in a sectional title, be it \chapter, \section, \subsection and so on, say

\section{Definition of $\sec$}

This is because LaTeX tries to write out the title in the .aux file, but when the write operation occurs, TeX is in no mode, so \ifmmode is false and \sec then expands to \section, which is not something one would like to try.

Second attempt:


this seems to work, because \sec in the title is not expanded during the write operation, having been made robust. However, we soon find another cause for failure, when hyperref is loaded. This happens because the package tries to use the title for the bookmarks.

This can be cured by adding

\pdfstringdefDisableCommands{\def\sec{sec }}

so this local redefinition overrides the standard one when the bookmarks are being produced.

Final code (to be added in the preamble after all packages have been loaded):

  {\pdfstringdefDisableCommands{\def\sec{sec }}}

Why shouldn't it be done?

There are several reason against doing this. One is general: a consistent user interface doesn't use the same command name for doing very different things.

Actually even Knuth has sinned in this respect, because the primitive \span means different things in different contexts (there are reasons for doing this, and the primitive is to be used with care anyway, usually hidden in other macros).

Another reason is for the particular case. Abbreviating \section into \sec has many disadvantages for no apparent advantage, other than saving a few keystrokes.

Smart editors know about \section and most colorize it with special colors for better readability of the source code. But some of them also have code folding features: one can press a button and all text pertaining to sections different from the one we're working on is hidden, leaving only the section title with some marker denoting the folded text.

If you use \sec, you're giving up with these useful features.

Another reason: if you work in cooperation with other people, they probably won't use \sec as they're used to \section. The result will be an inconsistent document and email exchanges about “what the h*** is \sec doing?”

Keep it simple and don't try saving on keystrokes. Good text editors will have code completion: type \sec and you'll be presented with possible completions, including the braces and the cursor between them if you choose \section.

You must log in to answer this question.

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