Set math mode as default for each item in list
Here's a enumitem
way and a patch that starts \(
after item
and ends \)
before \item
or at the end of the environment, checking with \ifmmode
whether we are in math mode or not.
Note: \item[...]
is not catched here! -- Since the patch is inside the environment group, all other \item
definitions in other environments are not changed.
It's possible to jump out of math mode with \item $ Non math content
, but perhaps a \item \text{Non math stuff}
would be better then!
The automatic math mode works with any math content, that expects the math mode, i.e. a \begin{pmatrix}...\end{pmatrix}
would also be possible.
\documentclass{article}
\usepackage{enumitem}
\newlist{mathlist}{enumerate}{1}
\setlist[mathlist,1]{label={\arabic*.}}
\usepackage{xpatch}
\AtBeginEnvironment{mathlist}{%
\xpretocmd{\item}{\ifmmode\)\fi}{}{}
\xapptocmd{\item}{\ifmmode\else\(\fi}{}{}
}
\AtEndEnvironment{mathlist}{%
\ifmmode \)\fi% Close math mode
}
\begin{document}
\begin{mathlist}
\item x + y = z
\item e^{i\pi} + 1 = 0
\item E=mc^2
\item \)Non math - mode % Jumping out of math-mode
\end{mathlist}
\begin{enumerate}
\item Foo % It's regular and not in math mode!
\end{enumerate}
\end{document}
Here's a LuaLaTeX-based solution, which works with both enumerate
and itemize
environments. It does not actually modify the enumerate
and itemize
environments, it doesn't modify the \item
macro, and it doesn't require the use of a new macro called, say, \mathitem
. How does the solution work, then? It takes a preprocessor approach: It sets up a Lua function which, by being assigned to the process_input_buffer
callback, acts as a preprocessor, scans each line of input, and -- if (and only if!) it's inside an enumerate
or itemize
environment -- adds $
symbols as needed at the start and end of the scope of each \item
. All this happens before TeX starts its normal work. Put differently, from the point of view of TeX's "eyes" (let alone "mouth" and "stomach"), it's as if all the required $
symbols had been entered by hand all along.
What if you have one or more \item
s whose content should not be treated as needing to be in math mode? For such \item
s, you could write either
\item $ An item without math. $
or, if the amsmath
package is loaded,
\item \text{An item without math.}
Which assumptions does this approach make regarding the formatting of the contents of the tex file? The only restrictive assumption is: The directives \begin{enumerate}
, \begin{itemize}
, \end{enumerate}
, \end{itemize}
, and \item
must occur on separate lines. Hopefully, this assumption isn't all that restrictive.
If you need to suspend the operation of the Lua function at some point in the document, just insert the instruction \addmathOff
. Conversely, to restart the Lua function later on in the document, insert the instruction \addmathOn
.
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath} % for "bmatrix" environment and "\text" macro
%% Lua-side code
\usepackage{luacode}
\begin{luacode}
in_list_env = false
at_first_item = false
function add_dollar_symbols ( buff )
if string.find ( buff, "\\begin{enumerate}" )
or string.find ( buff, "\\begin{itemize}" ) then
in_list_env = true
at_first_item = true
elseif string.find ( buff , "\\end{enumerate}" )
or string.find ( buff , "\\end{itemize}" ) then
in_list_env = false
buff = "$"..buff
elseif in_list_env == true then
if at_first_item == true then
at_first_item = false
buff = string.gsub ( buff , "\\item", "%0".."$ " )
else
buff = string.gsub ( buff , "\\item", " $".."%0".."$ ")
end
end
return buff
end
\end{luacode}
%% TeX-side code
\newcommand\addmathOn{\directlua{luatexbase.add_to_callback (
"process_input_buffer", add_dollar_symbols , "add_dollar_symbols" )}}
\newcommand\addmathOff{\directlua{luatexbase.remove_from_callback (
"process_input_buffer", "add_dollar_symbols" )}}
\addmathOn % enable operation of the Lua function
\begin{document}
\begin{enumerate}
\item 1 + 1
=
2
\item e^{i\pi} + 1
= 0 \quad \text{Euler's Identity}
\item $A non-mathy item$
\item \begin{bmatrix}
a & b & c \\
d & e & f
\end{bmatrix}
\end{enumerate}
\end{document}
You can create a command like \mathitem
that you would use in place of \item
when you want that the current item contains only math:
\newcommand\mathitem[1]{\item $#1$}
Your lists will look like this:
\begin{itemize}
\mathitem{1+1=2}
\mathitem{e^{i\pi}+1=0}
\end{itemize}