3

I would like to define a command that, starts (\begin{...}'s it) an environment if not in such an environment, and closes (\end{...}'s it) it if in such an environment, for example:

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

Ofcourse, this means that the myenum environment cannot be nested. Does anybody know how to define such a command?

4
  • I'm wondering what this would be useful for. I can think of no application, can you tell something more?
    – egreg
    Feb 15, 2016 at 11:08
  • You have this commands already: \begin{foo}...\end{foo}
    – user31729
    Feb 15, 2016 at 11:34
  • @egreg: I am actually writing a wrapper for LaTeX, in which I would like to use * as \item and ** as \begin{enumerate} and \end{enumerate}. The use of one command to start and close such an environment just seems natural to me. @ChristianHupfer: is it possible to define \begin{foo} to be \myenum if not in such an environment and \end{foo} otherwise?
    – Carucel
    Feb 15, 2016 at 11:44
  • @carucel You might be better off using Markdown and then converting the file using pandoc. This has the benefit that you can convert to a huge variety of formats, such as HTML, PDF, DocBook, and even Microsoft Office. Feb 15, 2016 at 13:05

1 Answer 1

3

Not sure how useful this would be; of course, nesting is impossible.

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

enter image description here

An expl3 implementation of the same:

\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}
1
  • Thank you! Thank you very much! I will try to elaborate on your answer to be able to omit the ** entirely and automatically insert \begin{enumerate} if n times * is encountered where n is larger than the current nesting level, thus using ** as an enumerate inside another enumerate.
    – Carucel
    Feb 15, 2016 at 12:31

You must log in to answer this question.

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