It's possible, although dubious, to define a macro that behaves differently in text or in math mode. There are LaTeX macros that do:
\strut \verb \phantom \smash \ensuremath \underline
\, \$ \{ \} \P \S \dag \ddag \_ \copyright \pounds \dots
\@inmatherror \@inmathwarn <...other internal macros...>
The first group consists of macros that must do different calls in one mode or the other, for technical reasons; for instance, \strut
calls \unhcopy\strutbox
in text mode, while it calls \uncopy\strutbox
in horizontal mode. Of course \ensuremath
has to do different things in the two modes.
The second group consists of commands that print the same symbol in both modes, but need different technical realizations. The third group is formed by internal macros that can signal some command can't or shouldn't be used in math mode, or do important bookkeeping jobs.
It's important to notice that the semantics of the commands in the first two groups doesn't change: for the final user they do “the same thing”. It's similar to \\
that has several different meanings depending on the context, but for the final user it always means “terminate a line”.
Defining a macro that has different semantics in text and math mode is not good practice, because it hinders clarity in the input and makes it difficult to remember what it means.
As a general rule, commands belonging to the LaTeX Internal Character Representation (LICR) shouldn't be redefined, as they can pop out in unexpected places. Once I received a frantic message by a user who couldn't print his coauthor's name, who happened to be Turkish and his name started with “Ş”; my colleague had, in his preamble,
\def\c{\gamma}
and of course “Ş” must be typed \c{S}
or Ş
(which inputenc
translates into \c{S}
anyway).
After this long premise, aimed at discouraging you to redefine \L
, here's how you can do
\let\polishL\L % save the old meaning
\DeclareRobustCommand{\L}{\ifmmode\mathbf{L}\else\polishL\fi}
We don't want that \L
is untimely expanded when in moving arguments, say
\section{\L o\'s theorem}
so we need to declare it as a robust command. However, this is not really a good definition, because commands such as \L
are defined in an “encoding dependent” way. So saving the meaning of \L
in a place can give it a meaning that could be different from the meaning it would have had in the document. Say that a package loads, at begin document, the T1
output encoding (some font packages may do it). If you don't load the T1
encoding beforehand, your \polishL
would mean
\OT1-cmd \L \OT1\L
instead of the correct
\T1-cmd \L \T1\L
and this can cause obscure font related error messages or wrong output.
Final words
Don't. Use a meaningful macro based on the meaning of your boldface mathematical L, not \L
.
\bL
for bold L?\L
may seem a good shortcut, it's better avoiding different meanings of the same macro depending on the context.\L
is too short a macro; use, say,\LosThm
.