18

I am interested in defining a new environment in LaTeX that has a starred version. How is this done?

1 Answer 1

22

Since the \newenvironment command uses a \csname, you can define it directly.

\documentclass{article}
\newenvironment{test*}
{start}{end}
\begin{document}
\begin{test*}
hello
\end{test*}
\end{document}

You can have any combination of characters (with catcode 11 and 12) as you would in a \csname...\endcsname construct,

\documentclass{article}
\newenvironment{test123*}
{start}{end}
\begin{document}
\begin{test123*}
hello
\end{test123*}
\end{document}

What you do with the starred command is up to you.

11
  • I thought it would be much more complicated than this :-) I should have tried it myself before I asked. May 23, 2012 at 18:14
  • @digital-Ink It's a sort of a hidden feature. May 23, 2012 at 18:15
  • If you were to define a command, xparse would allow to handle the starred and non-starred case in one definition. For environments, the solution however is less beautiful.
    – dgs
    May 23, 2012 at 19:01
  • 1
    @Yiannis saying "any combination of characters" is perhaps a little misleading as one is only allowed to use characters with catcode 11 and 12, e.g. \newenvironment{foo$} isn't working. May 24, 2012 at 10:40
  • 1
    Right, I have a solution for the starred environment business: if it works, it will be in the next xparse update
    – Joseph Wright
    May 24, 2012 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.