如何绘制有限差分模板和图形边框

如何绘制有限差分模板和图形边框

我有下面的乳胶代码,它给出了 1D 中的 5 点模板。我想给图形添加边框,还想将节点的颜色从黑色更改为中心,并将每个节点的名称置于中心。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{tikz,calc}
\usetikzlibrary{angles}
\usepackage{siunitx}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{blindtext}

\tikzset{every label/.style={font=\footnotesize,inner sep=1.5pt}}
\newcommand{\stencilpt}[4][]{\node[circle,fill,draw, label={below right:#4},#1] at (#2) (#3) {}}
\begin{figure}[h]
    \centering
    \begin{tikzpicture}
  \stencilpt{-2,0}{i-2}{$u_{i-2}$};
  \stencilpt{-1,0}{i-1}{$u_{i-1}$};
  \stencilpt{ 0,0}{i}  {$u_{i}$};
  \stencilpt{ 1,0}{i+1}{$u_{i+1}$};
  \stencilpt{ 2,0}{i+2}{$u_{i+2}$};
 % \stencilpt{0,-2}{j-2}{$-1/12$};
 % \stencilpt{0,-1}{j-1}{$4/3$};
 % \stencilpt[blue]{0, 1}{j+1}{$4/3$};
  %\stencilpt{0, 2}{j+2}{$-1/12$};
  \draw
        (i-2) -- (i-1)
        (i-1) -- (i)
        (i)   -- (i+1)
        (i+1) -- (i+2);
\end{tikzpicture}

  \caption{The five point stencil in 1D.} 
    \label{fig:1}  
\end{figure}

答案1

由于您没有指定颜色,我在stencilpt命令中使用了蓝色,并将标签的位置更改为居中。您可以使用fit库来实现框架,在库中定义要包含的节点。

在此处输入图片描述

\documentclass[varwidth=\maxdimen]{standalone}

\usepackage{tikz}
\usetikzlibrary{fit}

\tikzset{every label/.style={font=\footnotesize,inner sep=1.5pt}}

\newcommand{\stencilpt}[4][]{\node[circle,fill=blue,draw=blue, label={below:#4},#1] at (#2) (#3) {}}

\begin{document}
    \begin{figure}[h]
        \centering
        \begin{tikzpicture}
            \stencilpt{-2,0}{i-2}{$u_{i-2}$};
            \stencilpt{-1,0}{i-1}{$u_{i-1}$};
            \stencilpt{ 0,0}{i}  {$u_{i}$};
            \stencilpt{ 1,0}{i+1}{$u_{i+1}$};
            \stencilpt{ 2,0}{i+2}{$u_{i+2}$};
%           \stencilpt{0,-2}{j-2}{$-1/12$};
%           \stencilpt{0,-1}{j-1}{$4/3$};
%           \stencilpt[blue]{0, 1}{j+1}{$4/3$};
%           \stencilpt{0, 2}{j+2}{$-1/12$};
            \draw
            (i-2) -- (i-1)
            (i-1) -- (i)
            (i)   -- (i+1)
            (i+1) -- (i+2);
            
            \node[fit=(i-2)(i+2), draw=black, inner sep=10pt] {};
        \end{tikzpicture}
        
        \caption{The five point stencil in 1D.} 
        \label{fig:1}  
    \end{figure}
\end{document}

相关内容