檢查 \list 環境的 \item 的內容是否完全處於內聯數學模式

檢查 \list 環境的 \item 的內容是否完全處於內聯數學模式

我有一個清單

\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}

我只想將以下命令應用於 (4):

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

這裡的條件是,(4) 發生的唯一事情是它包含內聯數學材料——除指令之外沒有其他內容\item。相反,(1) 不包含數學; (2) 同時包含內聯數學和顯示數學,而「真正的」內聯數學材料位於句子的中間; (3) 與 (2) 的情況類似,在句子中間有「真正的」內聯數學。

我不能只是將 (2) 代入,\[1 + 1 = 2\]因為這會產生不需要的格式(例如方程式周圍有更多的空白)。

答案1

前面的評論:我建議您加載包並鍵入,而不是使用非常低級別的list環境,然後必須手動編輯每個標籤的外觀- 不再手動編輯每個標籤的輸出每一項指令。\itemenumitem\begin{enumerate}[label=(\arabic*)] ... \end{enumerate}\item

雖然 OP 表示不想使用 LuaLaTeX,但這篇文章的其他讀者可能會發現看到基於 LuaLaTeX 的解決方案很有啟發性。以下輸入格式要求希望不具約束力:

  • \begin{enumerate}, \item, 並且\end{enumerate}永遠不會發生一起在輸入行中;
  • \item每個輸入行至多有一個指令;
  • 輸入行中至多有一個內聯數學材料的實例;和
  • \(\)用於啟動和終止內聯數學模式。

此解決方案的工作原理如下:一個名為 的 Lua 函數switch2displaystyle對每一行輸入進行操作;如果它在enumerate環境中,它會檢查輸入行是否包含\item(可選)和純內聯數學材料(即沒有文字)。如果滿足此條件,則\displaystyle在 後插入指令\(。此switch2displaystyle函數使用 Lua 的findsubgsubstring 函數。

還有兩個實用的 LaTeX 宏,稱為\DisplayOn\DisplayOff,可以啟動和停用該switch2displaystyle功能。我所說的「激活」是指分配switch2displaystyle給 LuaTeX 的process_input_buffer回調,其中該函數作為輸入流上的預處理器運行。


在此輸入影像描述

% !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}

相關內容