\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환경을 사용하고 각 레이블의 모양을 직접 편집하는 대신 패키지를 로드하고 유형을 입력하는 \item것이 좋습니다. 모든 지시문.enumitem\begin{enumerate}[label=(\arabic*)] ... \end{enumerate}\item

OP는 LuaLaTeX를 사용하고 싶지 않다고 밝혔지만, 이 게시물의 다른 독자들은 LuaLaTeX 기반 솔루션이 작동하는 것을 보는 것이 유익할 수 있습니다. 다음 입력 형식 요구 사항은 구속력이 없기를 바랍니다.

  • \begin{enumerate}, \item\end{enumerate}발생하지 않음함께입력 라인에서;
  • \item입력 줄당 최대 하나의 지시문이 있습니다 .
  • 입력 라인에는 최대 하나의 인라인 수학 자료 인스턴스가 있습니다. 그리고
  • \(인라인 수학 모드를 시작하고 종료하는 데 \)사용됩니다.

솔루션은 다음과 같이 작동합니다. 라는 Lua 함수는 switch2displaystyle각 입력 줄에서 작동합니다. 환경 에 있는 경우 enumerate입력 행이 \item(선택 사항) 순수 인라인 수학 자료(즉, 텍스트 없음)로 구성되어 있는지 확인합니다. 이 조건이 만족되면 \displaystyle뒤에 지시문이 삽입됩니다 \(. 이 switch2displaystyle함수는 Lua의 find, sub, gsub문자열 함수를 사용합니다.

기능 을 활성화하고 비활성화하는 \DisplayOn및 라는 두 가지 유틸리티 LaTeX 매크로도 있습니다 . "활성화"란 함수가 입력 스트림에서 전처리기로 작동하는 LuaTeX의 콜백 에 대한 할당을 의미합니다 .\DisplayOffswitch2displaystyleswitch2displaystyleprocess_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}

관련 정보