Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In the following code, I would like to use one style so as to color background for each hint, and not all the hints, and I would also like to put each single hint in a rounded rectangle.

This is to illustrate what the players call "chain-rules".

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

% Some customizable styles
    \tikzset {
        highlight/.style = {
            yellow,
            opacity = 0.3
        },
        digit/.style = {
            minimum height = 5mm,
            minimum width = 5mm,
            anchor = center
        },
        circle/.style = {
            draw = green!80!black,
            dotted,
            very thick
        },
        circle number/.style = {
            draw = #1,
            very thick
        },
        cross/.style = {
            red,
            opacity = .5,
            shorten >= 1mm,
            shorten <= 1mm,
            very thick,
            line cap = round
        },
        hint/.style = {
            blue,
            font = \sf,
            minimum width = 3mm,
            minimum height = 3mm
        }
    }

% Modified the \node to give a unique name to each one, which is the
% row number, a dash and the column number. E.g: 1-1, 4-5, etc.
    \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}
    }

% New code -------------------------------------------------------------
    \def\highlightcell#1#2{
        \begin{scope}[on background layer]
            \fill[highlight] (#1-#2.north west) rectangle (#1-#2.south east);
        \end{scope}
    }

    \def\circlecell#1#2{
        \draw[circle] (#1-#2) circle(4mm);
    }

    \def\crosscell#1#2{
        \draw[cross] (#1-#2.north west) -- (#1-#2.south east);
        \draw[cross] (#1-#2.north east) -- (#1-#2.south west);
    }

    \def\highlightrow#1{
        \begin{scope}[on background layer]
            \fill[highlight] (#1-1.north west) rectangle (#1-9.south east);
        \end{scope}
    }

    \def\highlighcolumn#1{
        \begin{scope}[on background layer]
            \fill[highlight] (1-#1.north west) rectangle (9-#1.south east);
        \end{scope}
    }

    \def\highlightrectangle#1#2#3#4{
        \begin{scope}[on background layer]
            \fill[highlight] (#1-#2.north west) rectangle (#3-#4.south east);
        \end{scope}
    }

    \def\hintcell#1#2#3{
        \node at (#1-#2) {\hintbox{#3}};
    }


% Command to circle numbers:
% #1: optional -> circle color
% #2: mandatory -> cell identifier
% #3: mandatory -> name of the cell
    \newcommand\circlenumber[3][red!80!black]{
        \draw[circle number=#1, radius=5mm] (#2) circle node[outer sep=1mm] (#3){};
    }

% UGLY code. Do not read :-)
    \def\hintbox#1{
        \resizebox{4.5mm}{4.5mm}{%
            \tikz[scale=0.3]{%
                \def\auxc{0}
                \foreach \m in {1,...,9} {
                    \pgfmathparse{mod(\auxc,3)}
                    \xdef\x{\pgfmathresult}
                    \pgfmathparse{-floor(\auxc/3)}
                    \xdef\y{\pgfmathresult}
                    \xdef\hintprinted{0}
                    \foreach \n in {#1} {
                        \ifnum\n=\m
                            \node[hint] at (\x,\y) {\n};
                            \xdef\hintprinted{1}
                        \fi
                    }
                    \ifnum\hintprinted=0
                        \node[hint, opacity=0.1] at (\x,\y) {\m};
                    \fi
                    \pgfmathparse{\auxc+1}
                    \xdef\auxc{\pgfmathresult}
                }
            }%
        }
    }


\begin{document}

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

        \setcounter{row}{1}
        \setrow { }{2}{ }  {5}{ }{1}  { }{9}{ }
        \setrow {8}{ }{ }  {2}{ }{3}  { }{ }{6}
        \setrow { }{3}{ }  { }{6}{ }  { }{7}{ }

        \setrow { }{ }{1}  { }{ }{ }  {6}{ }{ }
        \setrow {5}{4}{ }  { }{ }{ }  { }{1}{9}
        \setrow { }{ }{2}  { }{ }{ }  {7}{ }{ }

        \setrow { }{9}{ }  { }{3}{ }  { }{8}{ }
        \setrow {2}{ }{ }  {8}{ }{4}  { }{ }{7}
        \setrow { }{1}{ }  {9}{ }{7}  { }{6}{ }

        \hintcell{5}{5}{2,7,8}
        \hintcell{1}{1}{3,4,6,7}

    \end{scope}
\end{tikzpicture}

\end{document}
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

One way would to pass in a style for each specfic hint in the form of a /border for a border, or /special for highlighting.

    \hintcell{5}{5}{2,7/special,8/border}
    \hintcell{1}{1}{3/border,4,6/special,7}

This yields:

enter image description here

Code:

\documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{backgrounds,shapes}
    \usepackage{graphicx}
    \usepackage{xstring}

% Some customizable styles
    \tikzset {
        highlight/.style = {
            yellow,
            opacity = 0.3
        },
        digit/.style = {
            minimum height = 5mm,
            minimum width = 5mm,
            anchor = center
        },
        circle/.style = {
            draw = green!80!black,
            dotted,
            very thick
        },
        circle number/.style = {
            draw = #1,
            very thick
        },
        cross/.style = {
            red,
            opacity = .5,
            shorten >= 1mm,
            shorten <= 1mm,
            very thick,
            line cap = round
        },
        hint/.style = {
            blue,
            font = \sf,
            minimum width = 3mm,
            minimum height = 3mm,
        },
        hint special/.style = {
            blue,
            font = \sf,
            minimum width = 3mm,
            minimum height = 3mm,
            fill=red!20,
            inner sep=0pt,
        },
        hint border/.style = {
            blue,
            font = \sf,
            minimum width = 2mm,
            minimum height = 3mm,
            inner sep=0pt, outer sep=0pt,
            draw=red, thick,
            rounded corners=1pt,
        }
    }

% Modified the \node to give a unique name to each one, which is the
% row number, a dash and the column number. E.g: 1-1, 4-5, etc.
    \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}
    }

% New code -------------------------------------------------------------
    \def\highlightcell#1#2{
        \begin{scope}[on background layer]
            \fill[highlight] (#1-#2.north west) rectangle (#1-#2.south east);
        \end{scope}
    }

    \def\circlecell#1#2{
        \draw[circle] (#1-#2) circle(4mm);
    }

    \def\crosscell#1#2{
        \draw[cross] (#1-#2.north west) -- (#1-#2.south east);
        \draw[cross] (#1-#2.north east) -- (#1-#2.south west);
    }

    \def\highlightrow#1{
        \begin{scope}[on background layer]
            \fill[highlight] (#1-1.north west) rectangle (#1-9.south east);
        \end{scope}
    }

    \def\highlighcolumn#1{
        \begin{scope}[on background layer]
            \fill[highlight] (1-#1.north west) rectangle (9-#1.south east);
        \end{scope}
    }

    \def\highlightrectangle#1#2#3#4{
        \begin{scope}[on background layer]
            \fill[highlight] (#1-#2.north west) rectangle (#3-#4.south east);
        \end{scope}
    }

    \def\hintcell#1#2#3{
        \node at (#1-#2) {\hintbox{#3}};
    }
%    \def\hintcellSpecial#1#2#3{
%        \node  [hint special] at (#1-#2) {\hintbox{#3}};
%    }
%    \def\hintcellBorder#1#2#3{
%        \node  [hint border] at (#1-#2) {\hintbox{#3}};
%    }

% Command to circle numbers:
% #1: optional -> circle color
% #2: mandatory -> cell identifier
% #3: mandatory -> name of the cell
    \newcommand\circlenumber[3][red!80!black]{
        \draw[circle number=#1, radius=5mm] (#2) circle node[outer sep=1mm] (#3){};
    }


% UGLY code. Do not read :-)
%  Sorry, it needed to be read to obtain solution :-)
    \def\hintbox#1{
        \resizebox{4.5mm}{4.5mm}{%
            \tikz[scale=0.3]{%
                \def\auxc{0}
                \foreach \m in {1,...,9} {
                    \pgfmathparse{mod(\auxc,3)}
                    \xdef\x{\pgfmathresult}
                    \pgfmathparse{-floor(\auxc/3)}
                    \xdef\y{\pgfmathresult}
                    \xdef\hintprinted{0}
                    \foreach \n/\Style in {#1} {
                        \ifnum\n=\m
                            \IfStrEqCase{\Style}{%
                                {}{\node[hint] at (\x,\y) {\n};}                                
                                {\n}{\node[hint] at (\x,\y) {\n};}
                                {border}{\node[hint border] at (\x,\y) {\n};}                           
                                {special}{\node[hint special] at (\x,\y) {\n};}
                            }
                            \xdef\hintprinted{1}
                        \fi
                    }
                    \ifnum\hintprinted=0
                        \node[hint, opacity=0.1] at (\x,\y) {\m};
                    \fi
                    \pgfmathparse{\auxc+1}
                    \xdef\auxc{\pgfmathresult}
                }
            }%
        }
    }


\begin{document}

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

        \setcounter{row}{1}
        \setrow { }{2}{ }  {5}{ }{1}  { }{9}{ }
        \setrow {8}{ }{ }  {2}{ }{3}  { }{ }{6}
        \setrow { }{3}{ }  { }{6}{ }  { }{7}{ }

        \setrow { }{ }{1}  { }{ }{ }  {6}{ }{ }
        \setrow {5}{4}{ }  { }{ }{ }  { }{1}{9}
        \setrow { }{ }{2}  { }{ }{ }  {7}{ }{ }

        \setrow { }{9}{ }  { }{3}{ }  { }{8}{ }
        \setrow {2}{ }{ }  {8}{ }{4}  { }{ }{7}
        \setrow { }{1}{ }  {9}{ }{7}  { }{6}{ }

        \hintcell{5}{5}{2,7/special,8/border}
        \hintcell{1}{1}{3/border,4,6/special,7}

    \end{scope}
\end{tikzpicture}

\end{document}
share|improve this answer
    
I'm sorry for my english. Indeed I would like to only have this styles for single hints and not the whole hints. In the following link, there is a picture to understand better what I'm looking for : sudokuwiki.org/XY_Chains (in this example, I do not need to display the double lines). –  projetmbc Jan 21 '13 at 20:52
    
The border only gets applied with \hintcellBorder, and the shading only gets applied with \hintcellSpecial. You can still use your original hintcell and which does not apply the border nor the shading. –  Peter Grill Jan 21 '13 at 21:29
    
I feel ashamed for the misunderstood due to my week english. For example, your rectangle is around 3, 4, 6 and 7 and I would like to only put 7 in such a rectangle. The problem is to work within the macro \hintbox#. –  projetmbc Jan 21 '13 at 21:35
    
Ok, I understand now. However that is probably going to require looking at code marked as UGLY code. Do not read :-) –  Peter Grill Jan 21 '13 at 21:37
    
It is not written Do not change !. Lol ! –  projetmbc Jan 21 '13 at 21:40
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.