4

In the preamble, these are the codes that I've kinda modified from the findings here. I am trying to enable different colours in the highlight style option, but to no avail.

Error: ! Illegal parameter number in definition of \pgfkeys@temp.

\usepackage{tikz}
\usepackage{parselines}
\usepackage{mathpazo}

\newcounter{row}
\newcounter{col}
\newcommand\setrow[9]{
  \setcounter{col}{1}
  \foreach \n in {#1, #2, #3, #4, #5, #6, #7, #8, #9} {
    \edef\x{\value{col} - 0.5}
    \edef\y{9.5 - \value{row}}
    \node[digit,name={\arabic{row}-\arabic{col}}] at (\x, \y) {\n};
    \stepcounter{col}
  }
  \stepcounter{row}
}

\usetikzlibrary{backgrounds}
% Some customizable styles
\tikzset {
  every highlight/.style = {color=#3, opacity=0.3},
  %highlight1/.style = {gray, opacity=0.3}
  digit/.style = { minimum height = 5mm, minimum width=5mm, anchor=center },
}
\def\highlightcell#1#2#3{
\fill[every highlight] (#1-#2.north west) rectangle (#1-#2.south east);
}

Somewhere in the document, I am trying to draw this picture.. With some cells highlighted in cyan, some in gray. Currently, I am able to colour it in one colour only. Here's my attempt to colour it in 2 or more colours.

\begin{tikzpicture}[scale=.55]
\begin{scope}
\draw (0, 0) grid (9, 9);
\draw[very thick, scale=3] (0, 0) grid (3, 3);

\setcounter{row}{1}
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }

\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }

\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }

% Highlight some cells
\highlightcell{1}{1}{cyan}
\highlightcell{1}{4}{cyan}
\highlightcell{2}{7}{cyan}
\highlightcell{2}{1}{cyan}
\highlightcell{3}{1}{gray}
\highlightcell{3}{4}{gray}
\highlightcell{3}{7}{gray}
\end{scope}
\end{tikzpicture}

Not sure if anyone could give me some guidiance?

  • Try every highlight/.style = {color=#1, opacity=0.3}, and \fill[every highlight=#3] (#1-#2.north west) rectangle (#1-#2.south east);. – Paul Gaborit Mar 28 '17 at 5:47
  • Hi Paul, thank you for your reply. Should it be color=#3 instead? :) In addition, can I ask how should I include #3 in the document body itself. is it still \highlightcell{3}{7}{gray}? – Bread Mar 28 '17 at 5:52
5

The key handler .style has only one parameter so you can not use the third (see pgfkeys documentation page 887). Therefore you should define the style as:

\tikzset {
    every highlight/.style = {color=#1, opacity=0.3},
    %highlight1/.style = {gray, opacity=0.3}
    digit/.style = { minimum height = 5mm, minimum width=5mm, anchor=center },
}

By the way, if you wanted to have a style with several parameters you could use .style n args (see page 888).

Then you must pass an argument to that parameter:

\newcommand{\highlightcell}[3]{%
    \fill[every highlight=#3] (#1-#2.north west) rectangle (#1-#2.south east);
}

I have replaced \def with \newcommand because \newcommand checks that you do not override an existing macro by mistake and you do not use \def's power here.

The complete code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\newcounter{row}
\newcounter{col}
\newcommand{\setrow}[9]{%
    \setcounter{col}{1}%
    \foreach \n in {#1, #2, #3, #4, #5, #6, #7, #8, #9} {%
        \edef\x{\value{col} - 0.5}%
        \edef\y{9.5 - \value{row}}%
        \node[digit,name={\arabic{row}-\arabic{col}}] at (\x, \y) {\n};%
        \stepcounter{col}%
    }%
    \stepcounter{row}%
}

% Some customizable styles
\tikzset {
    every highlight/.style = {color=#1, opacity=0.3},
    %highlight1/.style = {gray, opacity=0.3}
    digit/.style = { minimum height = 5mm, minimum width=5mm, anchor=center },
}
\newcommand{\highlightcell}[3]{%
    \fill[every highlight=#3] (#1-#2.north west) rectangle (#1-#2.south east);
}

\begin{document}
\begin{tikzpicture}[scale=.55]
\draw (0, 0) grid (9, 9);
\draw[very thick, scale=3] (0, 0) grid (3, 3);

\setcounter{row}{1}
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }

\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }

\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
\setrow { }{ }{ }  { }{ }{ }  { }{ }{ }

% Highlight some cells
\highlightcell{1}{1}{cyan}
\highlightcell{1}{4}{cyan}
\highlightcell{2}{7}{cyan}
\highlightcell{2}{1}{cyan}
\highlightcell{3}{1}{gray}
\highlightcell{3}{4}{gray}
\highlightcell{3}{7}{gray}
\end{tikzpicture}
\end{document}

screenshot

4

If no contents has to be added to cells, previous code can be simplified to:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

% Some customizable styles
\tikzset {
    every highlight/.style = {color=#1, opacity=0.3},
}
\newcommand{\highlightcell}[3]{%
    \fill[every highlight=#3] ({(#2-1)},{(9-#1)}) rectangle ++(1,1);
}

\begin{document}
\begin{tikzpicture}[scale=.55]
\draw (0, 0) grid (9, 9);
\draw[very thick, scale=3] (0, 0) grid (3, 3) (0,0) rectangle (3,3);

% Highlight some cells
\highlightcell{1}{1}{cyan}
\highlightcell{1}{4}{cyan}
\highlightcell{2}{7}{cyan}
\highlightcell{2}{1}{cyan}
\highlightcell{3}{1}{gray}
\highlightcell{3}{4}{gray}
\highlightcell{3}{7}{gray}
\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.