환경을 열고 닫는 명령 정의

환경을 열고 닫는 명령 정의

\begin{...}나는 그러한 환경에 있지 않으면 환경을 시작하고( 's it) \end{...}그러한 환경에 있다면 닫는( 's it) 명령을 정의하고 싶습니다. 예를 들면 다음과 같습니다.

\myenum
    \item Item A
    \item Item B
\myenum

물론 이는 myenum환경이 중첩될 수 없음을 의미합니다. 그러한 명령을 정의하는 방법을 아는 사람이 있습니까?

답변1

이것이 얼마나 유용한지는 잘 모르겠습니다. 물론 중첩은 불가능합니다.

\documentclass{article}

\usepackage{pdftexcmds}
\makeatletter
\catcode`*=\active
\def*{\@ifnextchar*{\check@enumerate}{\item}}

\def\check@enumerate{%
  \ifnum\pdf@strcmp{\@currenvir}{enumerate}=\z@
    % we're already in enumerate
    \end{enumerate}
  \else
    \begin{enumerate}
  \fi
  \@gobble
}
\makeatother

\begin{document}

Some text which we try to make long enough to reach
the right margin, so that we can see a line break
before the \texttt{enumerate} environment.
**
* First
* Second
**

Some other text which we try to make long enough to reach
the right margin, so that we can see a line break
after the \texttt{enumerate} environment.

\end{document}

여기에 이미지 설명을 입력하세요

expl3동일한 구현 :

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\cs_new:Nn \carucel_item_or_enum:
 {
  \peek_charcode_remove:NTF * 
   {
    \carucel_enum:
   }
   {
    \item
   }
 }
\cs_new:Nn \carucel_enum:
 {
  \tl_if_eq:vnTF { @currenvir } { enumerate }
   {
    \end{enumerate}
   }
   {
    \begin{enumerate}
   }
 }
\cs_generate_variant:Nn \tl_if_eq:nnTF { v }

\char_set_catcode_active:N *
\cs_set_eq:NN * \carucel_item_or_enum:
\ExplSyntaxOff

\begin{document}

Some text which we try to make long enough to reach
the right margin, so that we can see a line break
before the \texttt{enumerate} environment.
**
* First
* Second
**

Some other text which we try to make long enough to reach
the right margin, so that we can see a line break
after the \texttt{enumerate} environment.

\end{document}

관련 정보