3

After a few days working and reading about tikz and with everyone help, I'm starting to reach my goals. One of my graphs was working ok, but I decided to make it accept "parameters", hence I would be able to animate it and update for many situations and here follows my snippet:

\documentclass[12pt]{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=2,y=-1cm]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUT PARAMS 

% Domain start (xy)
\pgfmathsetmacro\xn{1};
\pgfmathsetmacro\yn{1};

% Stencil order
\pgfmathsetmacro\o{2};

% Size of stencil
\pgfmathsetmacro\s{0.1};
% Size of domain
\pgfmathsetmacro\d{1};

% Stencil position
\pgfmathsetmacro\sx{7};
\pgfmathsetmacro\sz{3};


% Number of subdomains
\pgfmathsetmacro\nx{3};
\pgfmathsetmacro\nz{3};

% END OF INPUT PARAMS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Limits
\pgfmathsetmacro\Mx{max(\xn-2*\s,0)};
\pgfmathsetmacro\My{max(\yn-2*\s,0)};
\pgfmathsetmacro\mx{min(\xn+\d+(2*\s),\nx)};
\pgfmathsetmacro\my{min(\yn+\d+(2*\s),\nz)};

% Grid
\draw [step=\s cm,very thin,black!10] (0,0) grid (\nx,\nz);
\draw [step=\d cm,thick,black!40] (0,0) grid (\nx,\nz);
\draw [very thick,black] (0,0) rectangle (\nx,\nz);

%Stencil

% Horizontal
\fill [color=blue] (\Mx + 3*\s - \o*\s, \My + \sz*\s) rectangle (\Mx + 3*\s + \o*\s, \My + (\sz+1)*\s);

% Numbering
\foreach \ix [evaluate={\x=int(\ix-1);}] in {1,2,...,\nx} {
    \foreach \iz [evaluate={\z=int(\iz-1);}] in {1,2,...,\nz} {
        \pgfmathsetmacro\t{int(\x+\nx*(\z))};
        \node  [anchor=north west] at (\x,\z) {\t};
    }
}

\draw [->,purple] (\xn + .25*\d  , \yn + .25*\d) -- (\xn +0.75*\d,\yn+.75*\d);
\fill [color=yellow,fill opacity=0.1] (\Mx,\My) rectangle (\mx, \my);

\end{tikzpicture}
\end{document}

Its failing with:

! Package tikz Error: Giving up on this path. Did you forget a semicolon?.

See the tikz package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.2480 ...ngle (\Mx + 3*\s + \o*\s, \My + (\sz+1)*
                                                  \s);

That is probably related with line:

\fill [color=blue] (\Mx + 3*\s - \o*\s, \My + \sz*\s) rectangle (\Mx + 3*\s + \o*\s, \My + (\sz+1)*\s);

But I can't see the reason why. Thanks.

EDIT (added answer)

For sake of reference I will add the fixed code here

\documentclass[12pt]{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=2,y=-1cm]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUT PARAMS 

% Domain start (xy)
\pgfmathsetmacro\xn{1};
\pgfmathsetmacro\yn{1};

% Stencil order
\pgfmathsetmacro\o{2};

% Size of stencil
\pgfmathsetmacro\s{0.1};
% Size of domain
\pgfmathsetmacro\d{1};

% Stencil position
\pgfmathsetmacro\sx{7};
\pgfmathsetmacro\sz{3};


% Number of subdomains
\pgfmathsetmacro\nx{3};
\pgfmathsetmacro\nz{3};

% END OF INPUT PARAMS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Limits
\pgfmathsetmacro\Mx{max(\xn-2*\s,0)};
\pgfmathsetmacro\My{max(\yn-2*\s,0)};
\pgfmathsetmacro\mx{min(\xn+\d+(2*\s),\nx)};
\pgfmathsetmacro\my{min(\yn+\d+(2*\s),\nz)};

% Grid
\draw [step=\s cm,very thin,black!10] (0,0) grid (\nx,\nz);
\draw [step=\d cm,thick,black!40] (0,0) grid (\nx,\nz);
\draw [very thick,black] (0,0) rectangle (\nx,\nz);

%Stencil
\fill [color=blue!70] (\Mx + \sx*\s - \o*\s, \My + \sz*\s) rectangle ({ \Mx + (\sx+1)*\s + \o*\s}, {\My + (\sz+1)*\s});
\fill [color=blue!70] (\Mx + \sx*\s, \My + \sz*\s - \o*\s) rectangle ({ \Mx + (\sx+1)*\s}, {\My + (\sz+1)*\s + \o*\s});
\fill [color=red!70] (\Mx + \sx*\s, \My + \sz*\s) rectangle ({ \Mx + (\sx+1)*\s}, {\My + (\sz+1)*\s});

% Numbering
\foreach \ix [evaluate={\x=int(\ix-1);}] in {1,2,...,\nx} {
    \foreach \iz [evaluate={\z=int(\iz-1);}] in {1,2,...,\nz} {
        \pgfmathsetmacro\t{int(\x+\nx*(\z))};
        \node  [anchor=north west] at (\x,\z) {\t};
    }
}

\draw [->,purple] (\xn + .25*\d  , \yn + .25*\d) -- (\xn +0.75*\d,\yn+.75*\d);
\fill [color=yellow,fill opacity=0.1] (\Mx,\My) rectangle (\mx, \my);

\end{tikzpicture}
\end{document}
  • You can't use brackets (...) there. You could use \sz*\s+\s instead there. – dexteritas Jun 11 '18 at 15:20
  • omg. that is what I want to avoid. – Lin Jun 11 '18 at 15:21
7

Use

(\Mx + 3*\s + \o*\s, {\My + (\sz+1)*\s});

Instead of

(\Mx + 3*\s + \o*\s, \My + (\sz+1)*\s);

the ()'s inside the coordinate confuses the parser, adding a set of {} hides the inner ()'s from the coordinate parser.

  • 1
    I think @Martin Scharrer's in depth explanation also applies here - tex.stackexchange.com/a/31833/8650 – hpekristiansen Jun 11 '18 at 15:27
  • 1
    also about syntax is possible to make a line like this: \foreach \ix [evaluate={\x=int(\ix-1);}] in {1,2,...,\nx} like : \foreach \ix in {0,1,...,\nx - 1}, since I have added the evaluate there just to count to nx-1 – Lin Jun 11 '18 at 15:45

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.