Probably this question has been asked before, but I could not find a satisfactory answer anywhere. Is there a way to define a \newenviroment with an optional and a must have argument. e.g something like

do something if #1 is there else leave alone
do something with #2

My apologies in advance if question sounds too vague.

  • 3
    Load the etoolbox package and write \newenvironment{envname}[2][]{\ifbool{isempty{#1}}{do something}{}do something with #2}{} in your preamble. That should work.
    – Bernard
    Commented Aug 17, 2014 at 3:02

1 Answer 1


You can use xparse package. It provides variuos options for declaring new environments:


These have the syntax

\DeclareDocumentEnvironment {<environment>} {<arg spec>}
{<start code>} {<end code>}

Futher, to check whether the first argument is present, you can use one of these:


\IfNoValueTF has, for example the syntax of

\IfNoValueTF {#1}
{ \DoSomethingJustWithMandatoryArgument {#2} }
{ \DoSomethingWithBothArguments {#1} {#2} }

For more details, please refer to xparse package documentation.

One simple example, (in the absence of any specific MWE/need)

    \IfNoValueTF {#1}
    No optional argument
    With optional argument

enter image description here

This is just a model example and you have to modify/write your own as per your needs.

  • Hey Harish, in your example you defined myenviron as taking 2 arguments .. but you passed just the one optional argument. Actually that is more refined part of my problem. What would be the syntax to use with such an environment? Commented Aug 17, 2014 at 4:25
  • Technically the N in the first call to myenviron and W in the second call act as the mandatory argument in Harish's example.
    – Werner
    Commented Aug 17, 2014 at 5:53
  • I can understand that N/H being taken as the second args in the two case. But is there a way to have something on the lines of say \begin{myenviron}[]{} or \begin{myenviron}{} Commented Aug 17, 2014 at 12:06
  • @user3750702 I am sure that I understand. Can you elaborate please?
    – user11232
    Commented Aug 17, 2014 at 12:14
  • @HarishKumar Taking the example of newcommand if I define \newcommand\mycommand[2][] then I can either call \mycommand as \mycommand[a]{b} {do stuff } or \mycommand{} {do stuff}. In the second case \mycommand understand that first optional arg is not passed. Is there a similar syntax in \newenvironment ? Commented Aug 17, 2014 at 13:19

You must log in to answer this question.

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