I would like to define an environment which takes coordinate like arguments (1,2). I think about two different calling schemes:

  1. \begin{myenv}(mandatory coordinate)[optional](optional coordinate)

  2. \begin{myenv}[optional](mandatory coordinate)(optional coordinate)

I need suggestion how to implement them. I am using TikZso for the optional arguments I would like use pgfkeys, but I don't have a clue about how to specify arguments with parentheses.


This is rather easy with xparse.

\NewDocumentEnvironment{myenv}{ o r() d() }
 {<begin code>}
 {<end code>}

You can test whether the first optional argument is expressed with

 {<code with optional argument expressed>}
 {<code with optional argument not expressed>}%

or with the two branches exchanged if you use \IfNoValueTF{#1}. Similarly for #3 (a coordinate pair delimited by parentheses).

With this sequence of arguments, you have an optional argument (to be enclosed in brackets), a mandatory coordinate argument and an optional coordinate argument. It's easier this way, probably.

If you want to supply defaults for the case the optional arguments are not expressed, use something like

\NewDocumentEnvironment{myenv}{ O{default} r() D(){0,0} }

where the uppercase letter means that a default (specified in the following braced list) is used. In this case you won't use \IfValueTF or \IfNoValueTF.

  • It is working fine to parse the argument but I couldn't manage to store each component of the coordinates in a macro. I tried \SplitArgument{1}{,}r() but couldn't write a proper macro to handle this. – TeXtnik Apr 26 '13 at 13:09

enter image description here


  \par myenva[#1]\{#2\}(#3,#4)\par}

  \par myenvb\{#1\}[#2](#3,#4)\par}












Note that in both cases this is using the same base function with defaults supplied [] in the optional case and (0,0) in the coordinate case. However that is not forced if you want the function to do something completely different of no [] is supplied, just replace {\myenva@A[]} by \myenva@C and define that command to do whatever you want rather than omitting the argument being equivalent to supplying [].


Not sure if this is what you want

\parindent 0in\parskip 1em
First: \coordone\\
Second: \coordtwo}{}


\begin{myenv}{(1,2) (5,7)}\end{myenv}

enter image description here

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.