经过几天的工作和阅读有关 tikz 的信息,以及大家的帮助,我开始实现我的目标。我的一个图表运行正常,但我决定让它接受“参数”,因此我能够为它制作动画并在许多情况下进行更新,下面是我的代码片段:
\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}
它的失败在于:
! 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);
这可能与以下行有关:
\fill [color=blue] (\Mx + 3*\s - \o*\s, \My + \sz*\s) rectangle (\Mx + 3*\s + \o*\s, \My + (\sz+1)*\s);
但我不知道原因是什么。谢谢。
编辑(添加答案)
为了方便参考,我将在此处添加修复代码
\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}
答案1
使用
(\Mx + 3*\s + \o*\s, {\My + (\sz+1)*\s});
代替
(\Mx + 3*\s + \o*\s, \My + (\sz+1)*\s);
坐标内的 () 会使解析器感到困惑,添加一组{}
会隐藏坐标解析器内部的 ()。