Определите команду, которая открывает и закрывает среду.

Определите команду, которая открывает и закрывает среду.

Я хотел бы определить команду, которая запускает ( \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}

Связанный контент