1

Assumed we have this little code snippet:

\begin{tikzpicture}
    \begin{axis}[
                date coordinates in = x,
                table/col sep       = semicolon,
                date ZERO           = 2019-01-01 09:00:00,
                xmin                = 2019-01-01 09:00:00,
                xmax                = 2019-01-10 21:00:00,
                xticklabel          = \day.\month,
                xlabel              = Test,
                ]
    \addplot table [x=Date, y=Value] {data.csv};
    \end{axis}
\end{tikzpicture}

The result looks completely the same like compiled with this code:

\begin{tikzpicture}
    \begin{axis}[
                date coordinates in = {x},
                table/col sep       = {semicolon},
                date ZERO           = {2019-01-01 09:00:00},
                xmin                = {2019-01-01 09:00:00},
                xmax                = {2019-01-10 21:00:00},
                xticklabel          = {\day.\month},
                xlabel              = {Test},
                ]
    \addplot table [x=Date, y=Value] {data.csv};
    \end{axis}
\end{tikzpicture}

Therefore:

When is it mandatory to use parentheses and when isn't it necessary?

  • 1
    Suppose you have (for example) legend style = draw = none, fill = white. How do you tell what is a key and what is the value passed to that key? The curly brackets are used to make clear for the underlying key-value parsing package what is a key and what is a value. Normally they are only required when you have a either ,, =, or ] in the value of a key to avoid the parser getting confused, but using them always won't do harm. – Phelype Oleinik Apr 21 '19 at 11:13
  • @PhelypeOleinik: Thanks a lot! Okay, this is making sense! Could you post it as an answer maybe? – Dave Apr 21 '19 at 11:59
3

Basically, you must surround value to a key in braces when you have either of these three (that I can remember) characters in it: ,, =, and ].

Suppose you have (for example) an option like this:

legend style = draw = none, fill = white

How do you tell what is a key and what is the value passed to that key?

The curly brackets are used to make clear for the underlying key-value parsing package what is a key and what is a value. This “protection” works in the TeX level because when TeX is grabbing a delimited argument (used in key-value parsers) it will (quoting the TeXbook, with my emphasis):

A delimited parameter is followed in the <parameter text> by one or more non-parameter tokens, before reaching the end of the parameter text or the next parameter token; in this case the corresponding argument is the shortest (possibly empty) sequence of tokens with properly nested {...} groups that is followed in the input by this particular list of non-parameter tokens. (Category codes and character codes must both match, and control sequence names must be the same.)

thus the {...} trick is to ensure that the parser will get the key and value correct.

From those three characters I mentioned, the , and the = need to be hidden because they are used by the key-value parser to determine where a key-value pair ends and later to determine what is a key and what is a value.

The ] needs to be protected as well because, much earlier, the axis environment grabs a [...]-delimited argument, and if you have an explicit ] not hidden TeX will think that it is the argument delimiter and will cut the argument midway through.

You can experiment with dummy keys to see what happens if you use or don't use the braces:

\documentclass{article}

\usepackage{pgfkeys}
\pgfkeys{
  legend style/.code = {
    Legend Style: #1.\par
  },
  draw/.code = {
    ERROR! Draw: #1.\par
  },
  fill/.code = {
    ERROR! Fill: #1.\par
  },
}

\begin{document}

\pgfkeys{legend style = draw = none, fill = white}

\pgfkeys{legend style = {draw = none, fill = white}}

\end{document}

enter image description here

  • 1
    I believe ) and ( are also in that list. – user121799 Apr 21 '19 at 13:18

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.