[Tex/LaTex] Set math mode as default for each item in list

#enumeratelistsmath-mode

\documentclass{article}
\begin{document}
\begin{enumerate}
    \item $x + y = z$
    \item $e^{i\pi} + 1 = 0$
\end{enumerate}
\end{document}

I have a long list in which 95% of the entries are to be written in math mode. I'd rather not have to wrap each entry with $...$ again and again. Is there any way to set the default mode to math mode just for the list entries?

Best Answer

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}

enter image description here