5

I want to draw a lot of different diagrams, but they all have common features. I want to have parameters for colour, shape, size, in a separate place so that I can fiddle with them at the end, and all the diagrams will change.

As far as I can tell the only way to define "variables" for multiple Tikz pictures is to use \newcommand.

My problem now is that if I want to set a colour like {rgb:blue,1;white,2}, it throws errors: "! Illegal unit of measure (pt inserted).", "Missing number, treated as zero.", etc.

More specifically: If I have

\filldraw[fill={rgb:red,1;white,2}]  (0,0) circle (0.5cm) node {$Z$};

it works fine, but:

\newcommand\zcolour{{rgb:red,1;white,2}}
...
\filldraw[fill=\zcolour]  (0,0) circle (0.5cm) node {$Z$};

throws the errors. It's not a colour thing, since

\newcommand\zcolour{white}
...
\filldraw[fill=\zcolour]  (0,0) circle (0.5cm) node {$Z$};

works fine. So I think it's the curly braces.

Hence: Do I need to do anything special to fix the curly braces, or is there a better way to define variables for multiple Tikz pictures?

Minimum Working Example:

\documentclass{article}

\usepackage{tikz}

\newcommand\zcolour{{rgb:red,1;white,2}}
\newcommand\whitecolour{white}

\begin{document}

\begin{tikzpicture}
    \filldraw[fill=\zcolour]  (0,0) circle (0.5cm) node {$Z$};%doesn't work
    \filldraw[fill=\whitecolour]  (0,0) circle (0.5cm) node {$Z$};%works
    \filldraw[fill={rgb:red,1;white,2}]  (0,0) circle (0.5cm) node {$Z$};%works
\end{tikzpicture}

\end{document}
  • 1
    Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. In the mean time you could try \filldraw[fill={\zcolour}]. However, the better way to do this is to use \tikzset to define your own styles: \tikzset{MyFill Style/.style={fill={rgb:red,1;white,2}}}. – Peter Grill Sep 5 '18 at 9:18
  • 1
    Just use \definecolor{zcolour}{RGB}{NUMBER1,NUMBER2,NUMBER3} and than (in the tikzpicture) \fill[zcolour] – current_user Sep 5 '18 at 9:20
  • 1
    Or you can do it as \newcommand\zfill{fill={rgb:red,1;white,2}} with usage as \filldraw\expandafter[\zfill] – Steven B. Segletes Sep 5 '18 at 9:30
  • Those are good ideas that solve the immediate problem, though if I need another command with braces, I'll still have a problem. They also inspired another solution: Create a \makestyles macro with a series of \tikzstyle commands, and use those styles. Basically a macro to construct more macros. – Sam Jaques Sep 5 '18 at 9:43
3

This is what .styles are used for. Change the style and the figure changes.

\documentclass{article}

\usepackage{tikz}

\tikzset{
    mystyle1/.style={
        fill=red,
        draw=blue,
        line width=1mm,
        text=green
        },
    mystyle2/.style={
        fill=blue!30,
        dashed,
        },
    mystyle3/.style={
        fill=none,
        draw=red,
        line width=1mm,
        densely dashed
        },
}       

%\newcommand\zcolour{{rgb:red,1;white,2}}
%\newcommand\whitecolour{white}

\begin{document}

\begin{tikzpicture}
    \filldraw[mystyle1]  (0,0) circle (0.5cm) node {$Z$};%doesn't work
    \filldraw[mystyle2]  (1.5,0) circle (0.5cm) node {$Z$};%works
    \filldraw[mystyle3]  (3,0) circle (0.5cm) node {$Z$};%works
\end{tikzpicture}

\end{document}

enter image description here

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.