如何使用带注释的 TikZ 包

如何使用带注释的 TikZ 包

我想制作如图所示的网格网格。你能帮助我吗?

答案1

主要构造为\foreachgrid;该shapes.geometric库用于星形和菱形:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\definecolor{myred}{RGB}{236,1,59}
\definecolor{myblue}{RGB}{116,122,255}
\definecolor{mygreen}{RGB}{130,214,149}
\definecolor{mygray}{RGB}{196,196,196}

\begin{document}

\begin{tikzpicture}[
  dot/.style 2 args={circle,draw=#1,fill=#2,inner sep=1.5pt},
  square/.style 2 args={draw=#1,fill=#2,inner sep=1.5pt},
  mystar/.style 2 args={star,draw=#1,fill=#2,inner sep=1.5pt},
  mydiamond/.style 2 args={diamond,draw=#1,fill=#2,inner sep=1.5pt}
]
% Upper grid
\draw
  (0,0) grid (4,4);
\foreach \Fila in {0,...,4}
{
  \foreach \Columna in {0,...,4}
  {
    \node[dot={black}{black}] at (\Fila,\Columna) {};
  }
}  
\foreach\Columna in {-2,6}
{
  \foreach \Fila in {0,...,4}
  {
      \node[mydiamond={myblue!60!black}{myblue}] at (\Columna,\Fila) {};
  }  
}
\draw[red]
  (-1,-1) -- (-1,4)  (5,-1) -- (5,4);
\foreach\Columna in {-1,5}
{
  \foreach \Fila in {-1,...,4}
  {
      \node[mystar={red}{myred}] at (\Columna,\Fila) {};
  }  
}

% middle grid
\draw[mygray]
  (0,-1) grid (4,-1);
\foreach \Fila in {0,...,4}
{
    \node[square={mygray}{white}] at (\Fila,-1) {};
}  

% lower grid
\draw[mygreen]
  (0,-3) grid (4,-2);
\foreach \Fila in {0,...,4}
{
  \foreach \Columna in {-3,-2}
  {
    \node[dot={green!70!black}{mygreen}] at (\Fila,\Columna) {};
  }
}  
\draw[mygreen]
  (-1,-3) -- (-1,-2)  (5,-3) -- (5,-2);
\foreach\Columna in {-1,5}
{
  \foreach \Fila in {-3,-2}
  {
      \node[mystar={green!70!black}{mygreen}] at (\Columna,\Fila) {};
  }  
}

% auxiliary white lines to ''erase'' part of the grids
\draw[white,line width=4pt]
  (-2,1.5) -- (6,1.5);
\draw[white,line width=4pt]
  (-2,-2.5) -- (6,-2.5);

% labels  
\foreach \Columna/\Posi/\Label in 
  {
    -2/above left/-1,
    -1/above left/0,
    0/above left/1,
    2/above right/i,
    4/above/I,
    5/above/I+1,
    6/above right/I+2%
  }
  \node[\Posi,font=\footnotesize] at (\Columna,2) {$(\Label,n)$};

\foreach \Fila/\Posi/\Label in 
  {
    4/above/N,
    1/below/M,
    0/below/1,
    -1/below/0,
    -2/below/-1,
    -3/below/-M%
  }
  \node[\Posi,font=\footnotesize] at (2,\Fila) {$(i,\Label)$};

\node at (-0.5,3.5) {$\mathcal{Q}_h^k$};
\node at (-0.5,-1.5) {$\mathcal{D}_h^k$};

\end{tikzpicture}

\end{document}

答案2

可以使用 TikZ 绘制符号,参见回答pifont贡萨洛·梅迪纳 (Gonzalo Medina) 的。以下示例使用包和包的符号pdfrender进行着色(需要 pdfTeX)。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{plotmarks}
\usepackage{graphicx}
\usepackage{pdfrender}
\usepackage{pifont}

\newcommand*{\MarkSymbol}[3]{%
  \textpdfrender{%
    TextRenderingMode=FillStroke,
    LineWidth=.4pt,
    FillColor={#2},
    StrokeColor={#3},
  }{\scriptsize\ding{#1}}%
}
\newcommand*{\BlueRoute}{%
  \rotatebox{45}{%
    \MarkSymbol{110}{blue!60!white}{blue!80!black}%
  }%
}
\newcommand*{\WhiteSquare}{%
  \MarkSymbol{110}{white}{gray}%
}
\newcommand*{\RedStar}{%
  \MarkSymbol{72}{red!60!white}{red!80!black}%
}
\newcommand*{\GreenStar}{%
  \MarkSymbol{72}{green!60!white}{green!80!black}%
}
\newcommand*{\GreenCirc}{%
  \MarkSymbol{108}{green!80!white}{green!80!black}%
}
\newcommand*{\BlackCirc}{%
  \MarkSymbol{108}{black}{black}%
}

\begin{document}
\begin{tikzpicture}[line cap=round]
  % Lines
  \draw[red!80!black]
    (-3, 0) -- (-3, 1.4) (-3, 1.6) -- (-3, 6)
    (3, 0) -- (3, 1.4) (3, 1.6) -- (3, 6)
  ;
  \draw[black]
    \foreach \x in {-2, ..., 2} {
      (\x, 1) -- (\x, 1.4) (\x, 1.6) -- (\x, 6) 
    }
    \foreach \y in {1, ..., 6} {
      (-2, \y) -- (2, \y)
    }
  ;
  \draw[gray] (-2, 0) -- (2, 0);
  \draw[green!80!black]
    \foreach \x in {-3, ..., 3} {
      (\x, -2) -- (\x, -1.6) (\x, -1.4) -- (\x, -1)
    }
    (-2, -2) -- (2, -2)
    (-2, -1) -- (2, -1)
  ;
  % Mark symbols
  \path
    \foreach \x in {-4, 4} {
      \foreach \y in {0, ..., 6} {
        (\x, \y) node{\BlueRoute}
      }
    }
    \foreach \x in {-3, 3} {
      \foreach \y in {0, ..., 6} {
       (\x, \y) node{\RedStar}
      }
    }
    \foreach \x in {-3, 3} {
      \foreach \y in {-1, -2} {
        (\x, \y) node {\GreenStar}
      }
    }
    \foreach \x in {-2, ..., 2} {
       \foreach \y in {-1, -2} {
         (\x, \y) node {\GreenCirc}
       }
       (\x, 0) node {\WhiteSquare}
       \foreach \y in {1, ..., 6} {
         (\x, \y) node {\BlackCirc}
       }
    }
  ;
  % Annotations
  \node at (-2.5, 4.5) {$\mathcal{Q}_h^k$};
  \node at (-2.5, -.5) {$\mathcal{D}_h^k$};
  \node[below] at (0, -2) {$(i, -M)$};
  \node[below] at (0, -1) {$(i, -1)$};
  \node[below] at (0, 0) {$(i, 0)$};
  \node[above] at (0, 6) {$(i, N)$};
  \node[above] at (-4, 3) {$(-1, n)$};
  \node[above] at (-3, 3) {$(0, n)$};
  \node[above] at (-2, 3) {$(1, n)$};
  \node[above] at (0, 3) {$(i, n)$};
  \node[above] at (2, 3) {$(I, n)$};
  \node[above, xshift=2mm] at (3, 3) {$(I{+}1, n)$};
  \node[above right] at (4, 3) {$(I{+}2, n)$};
\end{tikzpicture}
\end{document}

结果

相关内容