3

Suppose I have:

\documentclass[tikz, border=1cm]{standalone}
\tikzset{
    particle/.style={%
    draw=none, 
    fill=blue, 
    circle, 
    minimum size=3mm
  }
}

\begin{document}

\begin{tikzpicture}
    \node[particle] at (0, 0) {};
\end{tikzpicture}

\end{document}

If I had wanted the particle to have lighter blue fill I could have written:

\node[particle, fill=blue!20] at (0, 0) {};

But what if I change my fill color in global style (say to red)? Then I need to remember to change the above line to:

\node[particle, fill=red!20] at (0, 0) {};

My question: Is it possible to write something like whatevercolor!20?

blackened
  • 4,181
  • You can define your own colour, i.e. \definecolor{particlecolor}{RGB}{141,127,86} and change the colour-value in the definition. Then you can lighten/darken the colour within a tikzpicture environment. – Huang_d Jun 14 '17 at 11:51
  • Yes, that's a workaround. – blackened Jun 14 '17 at 11:52
  • 2
    If you don't mind the transparency effect, you can also use opacity. – Rmano Jun 14 '17 at 12:23

2 Answers2

8

Using styles with arguments is one way to do this.

enter image description here

A possible way is :

\documentclass[tikz, border=1cm]{standalone}
\tikzset{
    particle/.style=
    {
      draw=none, 
      fill=blue!#1, 
      circle, 
      minimum size=3mm
    },
    particle/.default=100,
}

\begin{document}

\begin{tikzpicture}
    \node[particle] at (0, 0) {};
    \node[particle=70] at (1, 0) {};
    \node[particle=40] at (2, 0) {};
\end{tikzpicture}

\end{document}
marsupilam
  • 6,383
  • @blackened Someone (you ?) un-upvoted then downvoted, so I revert back... Any feedback instead of getting mad ? – marsupilam Jun 14 '17 at 12:30
  • (not me, I just upvoted) but could be someone using a tablet. The upvote/downvote are quite itchy and it's easy to mis-touch, and then the thing locks during a time. It happened to me at other times... – Rmano Jun 14 '17 at 12:36
  • @marsupilam I downvoted (now re-upvoted) because I think this version is better. – blackened Jun 15 '17 at 12:29
3

Within tikz, there is an emulation of the xcolor package that enables similar functionality when it comes to defining colours. (The pgf manual, section 15.2, helpfully points out that the emulation is extremely basic). It works well enough for LaTeX.

For plain TEX users, it is not so easy to specify colors since plain TEX has no “standardized” color naming mechanism. Because of this, pgf emulates the xcolor package, though the emulation is extremely basic (more precisely, what I could hack together in two hours or so).

According to the manual, gray and rgb colour models are supported. The colour you define can be lightened or darkened by using !<number>. The number expresses a percentage and ranges from [0,200], with 0 being white and 200 being black. A few colours are pre-defined. Example:

\documentclass[tikz]{standalone}
\definecolor{mycolor}{RGB}{104,120,54}
\begin{document}
\begin{tikzpicture}
\fill[mycolor!50] (0,0) rectangle (1,1);
\fill[mycolor!100] (1,0) rectangle ++(1,1);
\fill[mycolor!150] (2,0) rectangle ++(1,1);
\end{tikzpicture}
\end{document}

Three colours via tikz

Huang_d
  • 1,797
  • 1
    That applies to plain TeX, you and the OP uses LaTeX, and then TikZ uses xcolor directly, no emulation needed. – Torbjørn T. Jun 14 '17 at 12:29
  • @TorbjørnT.: Then the manual (3.0.1a) is outdated, it still states that emulates, not loads. – Huang_d Jun 14 '17 at 13:09
  • No, you've misunderstood. The emulation only kicks in if you're using the plain TeX format (https://tex.stackexchange.com/questions/49 for reference), when you're using the LaTeX format, xcolor is loaded. The passage you quote explicitly says For plain TeX users ... Further, look in the .log file you get from your example and you'll see that xcolor.sty is read. See also section 8.1 in the manual. Or see the source: tikz.sty does \RequirePackage{pgf}, which does \RequirePackage{pgfcore}, which does \RequirePackage{xcolor}. – Torbjørn T. Jun 14 '17 at 13:24
  • @TorbjørnT.: I see, thanks for the clarification! It's good to know that I can use the full xcolor functionality. – Huang_d Jun 14 '17 at 13:37