# LaTeX package for stencil pictures in numerical analysis

Are there any LaTeX package to draw computational stencils (also known as computational molecules) such as the one in this book?

Another example of a computational stencil:

• This is a fairly simple tikz/pgf diagram. You could also use other drawing packages like pstricks If you are unfamiliar with them, then another option is to use an external drawing package and import the graphics, though you then won't have tex typesetting. If you're going to do this sort of thing regularly, then the effort to learn tikz will be worthwhile. I was able to create useful diagrams for my thesis in weeks of beginning to learn. – Nigel Nov 19 '14 at 3:20
• or you don't really need any package at all, the base latex format could produce that using \line and \circle – David Carlisle Nov 19 '14 at 8:29

A much more sophisticated approach could be devised, but without more details about what you want there's not much incentive.

\documentclass[tikz]{standalone}
\newcommand{\stencilpt}[4][]{\node[circle,draw,inner sep=0.1em,minimum size=0.8cm,font=\tiny,#1] at (#2) (#3) {#4}}

\begin{document}
\begin{tikzpicture}
\stencilpt{-2,0}{i-2}{$-1/12$};
\stencilpt{-1,0}{i-1}{$4/3$};
\stencilpt{ 0,0}{i}  {$-5$};
\stencilpt{ 1,0}{i+1}{$4/3$};
\stencilpt{ 2,0}{i+2}{$-1/12$};
\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 (j-2) -- (j-1)
(j-1) -- (i)
(i)   -- (j+1)
(j+1) -- (j+2)
(i-2) -- (i-1)
(i-1) -- (i)
(i)   -- (i+1)
(i+1) -- (i+2);
\end{tikzpicture}
\end{document}


As shown with the blue node, an optional argument exists for any extra styling code per-node. As you can see, the circled numerals work fine, but start to get unwieldy (too big) when we start to involve fractions. Something like

\documentclass[tikz]{standalone}
\tikzset{every label/.style={font=\footnotesize,inner sep=1pt}}
\newcommand{\stencilpt}[4][]{\node[circle,fill,draw,inner sep=1.5pt,label={above left:#4},#1] at (#2) (#3) {}}

\begin{document}
\begin{tikzpicture}
\stencilpt{-2,0}{i-2}{$-1/12$};
\stencilpt{-1,0}{i-1}{$4/3$};
\stencilpt{ 0,0}{i}  {$-5$};
\stencilpt{ 1,0}{i+1}{$4/3$};
\stencilpt{ 2,0}{i+2}{$-1/12$};
\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 (j-2) -- (j-1)
(j-1) -- (i)
(i)   -- (j+1)
(j+1) -- (j+2)
(i-2) -- (i-1)
(i-1) -- (i)
(i)   -- (i+1)
(i+1) -- (i+2);
\end{tikzpicture}
\end{document}


(just changed the \newcommand and added the \tikzset line) might give better results when fractions/larger numbers are involved:

• Thank you very much @PaulGessler for your answer. I'm using your template for drawing stencil extensively. Is it possible to define the separation between nodes. I tried to change several parameters in the \newcommand line but couldn't do it. I essentially want to make the spacing bigger – ecjb Jan 1 '19 at 18:23
• Hi @ecjb, in this version the stencil points are manually positioned because there weren't detailed requirements for positioning in the original question. The first argument to the \stencilpt macro is the coordinate location. But there are libraries like positioning available in TikZ if you wanted to change it to use relative positioning. With relative positioning, there would then be parameters available to adjust the spacing of all nodes on the grid at once. – Paul Gessler Jan 2 '19 at 16:43