3

I have a list

\begin{list}{}{}
    \item[(1)] First things first.
    \item[(2)] There are \(1 + 1 = 2\) types of "normal" math
        \[e^{2\pi i} + 1 = 2\]
    \item[(3)] Third time's \(\frac{1}{3}\) of a charm.
    \item[(4)] \(f(x) = 4\)
\end{list}

I would like to apply the following command to only (4):

\newcommand{\displayit}[1]{\bgroup\allmath{\displaystyle}#1\egroup}

The condition here is that the only thing happening with (4) is that it contains inline math material -- and nothing else (other than the \item directive). In contrast, (1) contains no math; (2) contains both inline and display math, and the "true" inline math material is in the middle of a sentence; and (3) is in a similar situation as (2) with "true" inline math in the middle of a sentence.

I can't just make (2) into \[1 + 1 = 2\] because that creates undesired formatting (such as a lot more white space around the equation).

3
  • Are you willing and able to use LuaLaTeX to compile your document? Please advise.
    – Mico
    Apr 20 at 4:50
  • @Mico no, sorry
    – grepgrok
    Apr 20 at 6:43
  • 1
    what does \allmath do? it's not a standard command? why not simply use \(\displaystyle f(x)=4\) ? Apr 20 at 8:25

1 Answer 1

2

A comment up front: Instead of employing a very-low-level list environment and then having to hand-edit the appearance of each and every \item label, I suggest you load the enumitem package and type \begin{enumerate}[label=(\arabic*)] ... \end{enumerate} -- no more hand-editing the output of each and every \item directive.

While the OP has indicated not to want to use LuaLaTeX, other readers of this posting may find it instructive to see a LuaLaTeX-based solution at work. The following input format requirements are hopefully not binding:

  • \begin{enumerate}, \item, and \end{enumerate} never occur together in an input line;
  • there's at most one \item directive per input line;
  • there's at most one instance of inline math material in an input line; and
  • \( and \) are used to initiate and terminate inline math mode.

The solution works as follows: A Lua function, called switch2displaystyle, operates on each line of input; if it's in an enumerate environment, it checks if the input line consists of \item (optional) and pure inline math material (i.e., no text). If this condition is satisfied, a \displaystyle directive is inserted after \(. The switch2displaystyle function employs Lua's find, sub, and gsub string functions.

There are also two utility LaTeX macros, called \DisplayOn and \DisplayOff, that activate and deactivate the switch2displaystyle function. By "activation", I mean assignment of switch2displaystyle to LuaTeX's process_input_buffer callback, where the function operates as a pre-processor on the input stream.


enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{enumitem}
\usepackage{luacode} % for 'luacode' envenvironment

\begin{luacode}
in_list_env = false -- initiate a Boolean flag
function switch2displaystyle ( s ) 
  if s:find ( "\\begin{enumerate}" ) then
    in_list_env = true
  elseif s:find ( "\\end{enumerate}" ) then
    in_list_env = false
  elseif in_list_env == true then -- there's something to do
    if s:find ( "\\item" ) then
      s = s:gsub ( "^%s-\\item%s-(\\%(.-\\%))$" , 
                   function ( x )
                      return "\\item \\( \\displaystyle " .. x:sub(3) 
                   end ) 
    else
      s = s:gsub ( "^%s-(\\%(.-\\%))$" , 
                   function ( x )
                      return "\\( \\displaystyle " .. x:sub(3)
                   end )
    end
    return s
  end
end
\end{luacode}

% Define 2 LaTeX utility macros
\newcommand\DisplayOn{\directlua{%
   luatexbase.add_to_callback(
     "process_input_buffer" , switch2displaystyle , 
     "switchtodisplaystyle" )}}
\newcommand\DisplayOff{\directlua{%
   luatexbase.remove_from_callback(
     "process_input_buffer" , 
     "switchtodisplaystyle" )}}

\begin{document}

\DisplayOn % activate the Lua function

% Verify that nothing happens if we're not in an 'enumerate' env.
\( \frac{1}{2}+\frac{1}{2}=1 \)

\begin{enumerate}[label=(\arabic*)]
\item First things first.
\item There are \( \frac{2}{2}+\frac{3}{3}=2 \) types of ``normal'' math
      \[ \textstyle \frac{1}{2}+\frac{1}{2}=1 \]
\item Third time's \( \frac{1}{2}+\frac{1}{2}=1 \) not a charm.
\item \( \frac{1}{2}+\frac{1}{2}=1 \)
     
      \( \frac{1}{4}+\frac{1}{4}=\frac{1}{2} \)
     
\DisplayOff % deactivate the Lua function

      \(\frac{1}{2}+\frac{1}{2}=1\)
\end{enumerate}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .