17
\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?

  • May be for that tasks package is better suited. In any case, it would be great to have in tasks also what you propose of math mode by default. – Manuel May 1 '16 at 20:38
  • 2
    @Manuel github.com/cgnieder/tasks/issues/8 – clemens May 1 '16 at 21:50
  • 1
    @Manuel this is now implemented in v0.11 of tasks (available on CTAN): \begin{tasks}[item-format=\ensuremath] – clemens May 4 '16 at 8:35
  • @clemens You could add an answer to this question. – Manuel May 4 '16 at 9:05
  • @Manuel done :) – clemens May 4 '16 at 9:10
15

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

  • 2
    It's you again with your xpatch wizardry! Thanks for an answer. – Synergist May 1 '16 at 21:52
  • @Synergist: Patching is easier here than redefinition of \item -- but I should provide another (better) version to catch \item[...] as well. – user31729 May 1 '16 at 21:54
8

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}
  • This is my best option so far. It's still slightly inconvenient as my editor autocompletes \item when I hit Enter while in the list environment. I'm probably spending more time nitpicking than I would ever save, but it'd be nice to have to have a wrapper around the environment that sets math mode as the default for each entry rather than doing as you said. Still, thanks for your suggestion. – Synergist May 1 '16 at 20:45
  • 4
    You simply replace typing a pair of dollars with typing a pair of braces… – Bernard May 1 '16 at 20:48
  • 2
    I'd like to be able to do \begin{mathItemize} { \item 1, \tem 2, ... } \end{mathItemize} rather than having to do \begin{itemize} { \mathitem 1, \mathitem 2, ...} \end{itemize}, or, even worse, having to do\begin{itemize} {\item $1$, \item $2$, ... } \end{itemize}. Does that make sense? – Synergist May 1 '16 at 21:03
8

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 \items whose content should not be treated as needing to be in math mode? For such \items, 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.

enter image description here

% !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}
  • I don’t know Lua nor LuaTeX, but since you say that your Lua code parses each line, is it possible to write multi-line math (like matrix environments), I mean that spans across several lines in the source code? – Zoxume May 2 '16 at 7:03
  • @Zoxume - It's certainly possible. :-) In the code above, observe that the contents of the third \item are spread out over several lines. The code doesn't modify the contents of any \items; all it does is to insert $ symbols, where needed, to initiate and terminate math mode. – Mico May 2 '16 at 11:45
  • @Zoxume - I've modified the code to provide an \item that features a bmatrix environment. – Mico May 2 '16 at 16:46
6

With version 0.11 of tasks it is possible to say item-format=\ensuremath:

\documentclass{article}
\usepackage{tasks}[2016/05/03] % v0.11
\begin{document}

This is some text.
\begin{tasks}[item-format=\ensuremath]
  \task x + y = z
  \task e^{i\pi} + 1 = 0
\end{tasks}
This is some text.
\begin{tasks}[style=enumerate,item-format=\ensuremath]
  \task x + y = z
  \task e^{i\pi} + 1 = 0
\end{tasks}
This is some text.
\begin{tasks}[counter-format=tsk.,item-format=\ensuremath]
  \task x + y = z
  \task e^{i\pi} + 1 = 0
\end{tasks}
This is some text.

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.