我是 tikz 新手。我有以下网格:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{patterns}
\usetikzlibrary{shapes}
\usetikzlibrary{plotmarks}
\newcommand{\nvar}[2]{
\newlength{#1}
\setlength{#1}{#2}
}
\nvar{\dx}{1cm}
\begin{document}
\begin{tikzpicture}
% fill outside cells
\fill [orange!40] (-1,-1) rectangle ( 3, 0);
\fill [orange!40] (-1, 2) rectangle ( 3, 3);
\fill [orange!40] (-1, 0) rectangle ( 0, 2);
\fill [orange!40] ( 2, 0) rectangle ( 3, 2);
% pressure cells
\draw [semithick][help lines, step=\dx] (-1,-1) grid ( 3, 3);
\draw [thin,dashed][help lines, step=.5*\dx] (-1,-1) grid ( 3, 3);
\draw [red,thick] ( 0, 0) rectangle ( 2, 2);
\draw [red,thick] (-1,-1) rectangle ( 3, 3);
\end{tikzpicture}
\end{document}
我想注释网格的边缘以生成在 tikz 中看起来像这样的图片:
答案1
像这样吗?
\documentclass[tikz]{standalone}
\newcommand{\nvar}[2]{
\newlength{#1}
\setlength{#1}{#2}
}
\nvar{\dx}{1cm}
\begin{document}
\begin{tikzpicture}
\path [draw=red, thick, fill=orange!40, even odd rule,] (-1,-1) rectangle (3,3) (0,0) rectangle (2,2);
\draw [semithick][help lines, step=\dx] (-1,-1) grid ( 3, 3);
\draw [thin,dashed][help lines, step=.5*\dx] (-1,-1) grid ( 3, 3);
\foreach \i in {0,...,2}
\foreach \j in {.5, 1.5}
{
\node [green] at (\i,\j) {$\times$};
\node [black!80] at (\j,\i) {$\otimes$};
}
\end{tikzpicture}
\end{document}
初始命令使用两个矩形替换了 4 个矩形和边框的绘制even odd rule
。(我只是觉得这是一个巧妙的技巧,最近发现它在 tikz 中有效,而不仅仅是 metapost。;) 所以我忍不住……)
为了进行注释,我使用两个循环遍历变量,将它们嵌套起来,并使用每个值对坐标进行迭代x
和对坐标进行迭代。基本上,tikz 将使用所有可能的/y
对运行这些循环,从而节省输入时间。\i
\j
答案2
因为为什么不呢?:P
\documentclass[tikz]{standalone}
\usetikzlibrary{plotmarks}
\begin{document}
\begin{tikzpicture}
\def\dx{1cm}\def\mylista{}\def\mylistb{}
\foreach \x in {0,1,2}{
\foreach \y in {.5,1.5}{
\xdef\mylista{\mylista (\x,\y)}\xdef\mylistb{\mylistb (\y,\x)}
}
}
\draw[thick,red,double distance between line centers=\dx,double=orange!40] (-0.5,-0.5) rectangle (2.5,2.5);
\draw [semithick][help lines, step=\dx] (-1,-1) grid ( 3, 3);
\draw [thin,dashed][help lines, step=.5*\dx] (-1,-1) grid ( 3, 3);
\draw --plot [only marks,mark=otimes] coordinates \mylistb;
\draw --plot [only marks,mark=x,mark options={color=green}] coordinates \mylista;
\end{tikzpicture}
\end{document}
答案3
在环境的末尾tikzpicture
(就在 之前\end{tikzpicture}
)插入下面的代码。
\foreach \a in {.5,1.5}{
\foreach \b in {0,1,2}{
\node at (\a,\b){$\otimes$};
\node[green] at (\b,\a){$\times$};
}
}