TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 5 down vote accepted

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.

share|improve this answer
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

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

share|improve this answer

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 [].

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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