Next: , Previous: , Up: Options   [Contents][Index]


3.1.5.4 TikZ/PGF user-defined styles

TikZ/PGF allows named styles defined by the user, for example

  \tikzstyle{mypolygonstyle} = [fill=blue!20,fill opacity=0.8]  
  \tikzstyle{mylinestyle} = [red!20,dashed]

Since sketch has no information on the contents of such styles, it omits them entirely from lines, polygons, and their edges during option splitting. For example,

  polygon[style=mypolygonstyle,style=thick](0,0,1)(1,0,0)(0,1,0)
  line[style=mylinestyle](-1,-1,-1)(2,2,2)

produces the TikZ output

  \draw(-1,-1)--(.333,.333);
  \filldraw[thick,fill=white](0,0)--(1,0)--(0,1)--cycle;
  \draw(.333,.333)--(2,2);

Note that the user-defined styles are not present. Sketch also issues warnings:

  warning, unknown polygon option style=mypolygonstyle will be ignored
  warning, unknown line option style=mylinestyle will be ignored

The remedy is to state explicitly whether a user-defined style should be attched to polygons or lines in the TikZ output using pseudo-options fill style and line style,

  polygon[fill style=mypolygonstyle,style=thick](0,0,1)(1,0,0)(0,1,0)
  line[line style=mylinestyle](-1,-1,-1)(2,2,2)

Now, the output is

  \draw[mylinestyle](-1,-1)--(.333,.333);
  \filldraw[mypolygonstyle,thick](0,0)--(1,0)--(0,1)--cycle;
  \draw[mylinestyle](.333,.333)--(2,2);

A useful technique is to include user-defined style definitions in sketch code as specials with option [lay=under] to ensure that the styles are emitted first in the output, before any uses of the style names. 2 For example,

  special|\tikzstyle{mypolygonstyle} = [fill=blue!20,fill opacity=0.8]|[lay=under]
  special|\tikzstyle{mylinestyle} = [red!20,dashed]|[lay=under]

The author is responsible for using the key, line style or fill style, that matches the content of the style definition.


Footnotes

(2)

This clever trick is due to Kjell Magne Fauske.