Here's a LuaLaTeX-based solution. The solution provides a Lua function called l2ell
that does most of the actual work, along with two utility LaTeX macros, \lellOff
and \lellOn
, which deactivate and reactivate the l2ell
function.
This approach will admittedly go astray on $\l$
. However, as \l
is a text-mode command (for the Polish "soft ell", to be specific), it shouldn't occur in math-y circumstances, right?
\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function l2ell ( s )
s = s:gsub ( "^l(%A)" , "\\ell %1" )
s = s:gsub ( "(%A)l$" , "%1\\ell" )
s = s:gsub ( "(%A)l(%A)" , "%1\\ell %2" )
return s
end
\end{luacode}
% LaTeX macros to activate and deactivate the Lua function:
\newcommand\lellOn{\directlua{luatexbase.add_to_callback (
"process_input_buffer" , l2ell , "l2ell" )}}
\newcommand\lellOff{\directlua{luatexbase.remove_from_callback (
"process_input_buffer" , "l2ell" )}}
\AtBeginDocument{\lellOn} % activate the Lua function by default
\begin{document}
$\log l +\ln l= lb + l^2$, $l$, $ll$, $l + l$, $ l ^ l$
\lellOff % deactivate the Lua function
$\log l +\ln l= lb + l^2$, $l$, $ll$, $l + l$, $ l ^ l$
\end{document}
lb
represents the multiplication ofl
andb
, not the variablelb
. For multi-letter mathematical symbols, one might use, for example,\mathrm{lb}
. In this sense, your question might be considered ambiguous.