5

I'm having trouble finding good docs about this specific way of writing styles for latex documents.

The way I understand the grammar should be

<name of the command>/.style = {<options of the command you want to change> = new value}

What I tried is

\documentclass{report} 
\usepackage[dvipsnames]{xcolor} 
\usepackage{tikz}

\tikzset{
    draw/.style = {color=Blue}
    grid/.style = {color=Lavender}
}

\begin{document} 
    \begin{tikzpicture}[scale=2,cap=rect]
        \draw (0,0) -- (1,0) -- (1,1) -- cycle;
    \end{tikzpicture}
    
    \begin{tikzpicture}
        \draw (0,0) grid (2,2);
        \draw (2,0) grid (4,2);
    \end{tikzpicture}
    
\end{document}

and the previous code doesn't render anything visible at all, I also tried with just

\tikzset{
    draw/.style = {color=Blue}
}

but the result is the same.

What I'm getting wrong and what is the grammar of this ?

7
  • 1
    You are overriding the definition of draw etc in tikz. Try mydraw/.style={draw, red, dashed} and then for example \node[my draw]{content}
    – Rmano
    Mar 25 at 22:45
  • @Rmano can I get away without adding another element ? In this case \node
    – radm
    Mar 25 at 22:54
  • 1
    \draw means \path[draw], and [draw] sets a flag so that the path will drawn. Similarly, \fill means \path[fill] and [fill] sets a flag so that the path will be filled. This is why \drawfill, \draw[fill], \fill[draw], \path[draw, fill] all mean the same thing.
    – Symbol 1
    Mar 26 at 0:11
  • 1
    The general convention in TikZ is that every 〈blah〉/.style = {...} sets the default behavior of 〈blah〉. Examples are every picture, every path, and every node. More can be found in the doc. Go to the index and look for every ....
    – Symbol 1
    Mar 26 at 0:14
  • 1
    "having trouble finding good docs" Have you found the TikZ manual (pgfmanual.pdf)?
    – user202729
    Mar 26 at 3:43

1 Answer 1

6

Here is the basic idea. The syntax is

\tikzset{
    <firstname>/.style={<style elements>}, 
    <secondname>/.style={<style elements>}, 
    ...,
    <lastname>/.style={<style elements>}
}

You can create styles for nodes, lines and other objects (or for scopes or entire tikz pictures). In the example below, I define four styles, two I plan to use for nodes and two for lines. The syntax is the same. You can include any attribute that applies to that type of object. This setup is useful if you plan to use the same style of node (or line, or arrow, etc.) several times. There are more complicated things you can do with tikzset as well, but this should get you started.

enter image description here

\documentclass{article}

\usepackage{tikz}

\tikzset{
    my big node/.style={violet, draw=blue, circle, minimum width=1.5cm},
    my little node/.style={draw=green,very thick,font=\tiny, rotate=90},
    my line/.style={<-,thick,orange},
    my other line/.style={line width=3pt,teal,dotted}
}
\begin{document}

\begin{tikzpicture}

\node[draw] (A) at (0,0){default};
\node[my big node] (B) at (2,0){big};
\node[my little node] (C) at (4,0){small};
\node[fill=red] (D) at (6,0){};
\draw(A)--(B);
\draw[my line](B)--(C);
\draw[my other line](C)--(D);

\end{tikzpicture}

\end{document}
7
  • lastname needs a slash <lastname>/.style, not <lastname>.style
    – Bill Nace
    Mar 26 at 1:39
  • @BillNace, thanks. Fixed.
    – Sandy G
    Mar 26 at 2:10
  • so the elements to draw is defined in the .style part and not in the invocation of the \draw command ?
    – radm
    Mar 26 at 8:34
  • also, I have to wrap anything in a node ? for example how to customize a cycled draw \draw (0,0) -- (1,0) -- (1,1) -- cycle; ( a triangle ) ?
    – radm
    Mar 26 at 9:00
  • @radm: For nodes, draw must be included if you want the node shape drawn. Try removing draw from the default node or from the defined node styles to see the effect. You can use the defined styles on your triangle also, for example, \draw[my other line] (0,0) -- (1,0) -- (1,1) -- cycle;
    – Sandy G
    Mar 26 at 12:25

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.