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.
- 459
1 Answers
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,
\renewcommand{\sec}{\relax\ifmmode\latexsec\else\expandafter\section\fi}
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:
\DeclareRobustCommand{\sec}{%
\ifmmode\latexsec\else\expandafter\section\fi
}
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):
\let\latexsec\sec
\DeclareRobustCommand{\sec}{%
\ifmmode\latexsec\else\expandafter\section\fi
}
\makeatletter
\@ifpackageloaded{hyperref}
{\pdfstringdefDisableCommands{\def\sec{sec }}}
{}
\makeatother
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.
- 1,121,712
\sectioncommand and are able to give prominence to it or use it for code folding. Going the\secroute means giving up with these features. – egreg Oct 28 '15 at 08:09