3

How does the tikzpicture environment support multiple options, e.g. \begin{tikzpicture}[red, thick] ... \end{tikzpicture}?

According to some LaTeX documentation, multiple options are not permissible for an environment (see page 43 of this document).

Note that this question is similar to How to pass two parameters to a \newenvironment?, where it went unanswered.

Please note the distinction this question is making with regards to required parameters/arguments, which are supplied with curly braces, and options, which are supplied with rectangular braces.

  • The document you cite says that you cannot define more than one optional argument to a new environment; what's in that unique optional argument is something that the body of the definition can manage. – egreg Apr 1 '16 at 22:11
4

The supported syntax for \newenvironment is

\newenvironment{<name>}[<n>]{<begin-code>}{<end-code>}

or

\newenvironment{<name>}[<n>][<default>]{<begin-code>}{<end-code>}

and this is what's explained in the document you cite.

The first type defines an environment taking <n> mandatory arguments, so after

\newenvironment{foo}[2]{<begin-code>}{<end-code>}

you have to call

\begin{foo}{first}{second}
body
\end{foo}

With the second mode, the environment accepts a single optional argument, with default value <default>; so after

\newenvironment{foo}[2][default]{<begin-code>}{<end-code>}

you can call the environment either with

\begin{foo}{argument}
body
\end{foo}

or with

\begin{foo}[option]{argument}
body
\end{foo}

The case of tikzpicture is

\newenvironment{tikzpicture}[1][]{<begin-code>}{<end-code>}

What the environment does with the optional argument is left to <begin-code> to manage. When you do

\begin{tikzpicture}[red,black]
...
\end{tikzpicture}

there is one optional argument, which is processed to extract the TikZ options.

3

Technically this is only one optional argument (red,thick), two would look like \begin{tikzpicture}[red][thick] or similar.

Also the statement is not strictly true if you use low level TeX commands; just think about the signature for \newcommand{\command}[number of arguments][default value of first argument]{code}.

What happens here is that the one argument the environment gets is parsed and split along commas into several sub-arguments, that are then used in the code. A lot of packages use some derivative of xkeyval (you can read up how to use it there) but I am sure tikz rolls its own routines.

  • While this answer is complete and suggests a possible implementation of tikz, @egreg's answer addressed, clearly, the different ways in which arguments can/should be passed and their implications. – fuzzybear3965 Apr 1 '16 at 22:32
  • @fuzzybear3965 Well, egreg's answer addresses the possible ways arguments can/should be specified for environments defined using \newenvironment{}[][]{}{}. This answer points out that there are other ways of defining environments which are not subject to the same restrictions. However, it does also suggest that the alternatives require moving to lower-level TeX. This has not been the case for some time, but is certainly not the case now we have xparse's \New..., \Declare... etc. species of macros for defining things. – cfr Apr 1 '16 at 23:08
  • Right, I had mentioned this answer's value in regards to it addressing xkeyval as a way in which these things are handled sometimes. Thanks for addressing the other options. I have seen references to xparse and \New (not \Declare), myself. Hopefully, the information you've provided will help others. – fuzzybear3965 Apr 1 '16 at 23: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.