如何在网格上放置箭头?

如何在网格上放置箭头?

在此处输入图片描述

我正在尝试像上面一样在网格上放置箭头。我应该在下面的代码中添加什么?

\documentclass[margin=10pt]{standalone}    
\usepackage{tikz}  
\begin{document}  
\begin{tikzpicture}  
\draw[step=1cm, color=gray] (0, 0) grid (3, 2);  
\draw (0, 0) circle (0.1);
\draw (3, 2) circle (0.1);
\foreach \coord/\label [count=\xi] in {  
 {0,0}/{$A$},  
 {3,2}/{$B$}}{  
 \pgfmathsetmacro\anch{mod(\xi,2) ? "north" : "south"}  
 \node[anchor=\anch] at (\coord) {\label};  
 }

\end{tikzpicture}  
\end{document}

答案1

此示例使用了一些手动调整和循环。

\documentclass[margin=10pt]{standalone}    
\usepackage{tikz}  
\begin{document}  
\begin{tikzpicture}  
\draw[step=1cm, color=gray] (0, 0) grid (3, 2);  
\draw (0, 0) circle (0.1);
\draw (3, 2) circle (0.1);
\foreach \coord/\label [count=\xi] in {  
 {0,0}/{$A$},  
 {3,2}/{$B$}}{  
 \pgfmathsetmacro\anch{mod(\xi,2) ? "north" : "south"}  
 \node[anchor=\anch] at (\coord) {\label};  
 }

\foreach \x in {0,1,2}
{
    \draw [->,red](\x +0.1 , 0.1) --
              node [pos=0.5, above, blue] {a}
              (\x +0.9, 0.1);
}

\foreach \y in {0,1}
{
    \draw [->,red](0.1 , \y + 0.1) --
              node [pos=0.5, right, blue] {b}
              (0.1, \y + 0.9);
}

\end{tikzpicture}  
\end{document}

网格上的箭头

答案2

像这样

\documentclass[margin=10pt]{standalone}  
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
 \draw[step=1cm, color=gray] (0, 0) grid (5, 4);

\foreach \coord/\label [count=\xi] in {
    {0,0}/{$A(0, 0)$},
    {5,4}/{$B(x, y)$},
    {5,0}/{$(x, 0)$},
    {0,4}/{$(0, y)$}
}{
    \pgfmathsetmacro\anch{mod(\xi,2) ? "north" : "south"}
    \node[anchor=\anch] at (\coord) {\label};
}

\foreach \i [count=\j from 1] in {0,...,4}\draw[red,->,above=4pt] ([xshift=2pt]\i,0) -- ([xshift=-2pt]\j,0)         node[midway,above]{a} ;

\draw[red,->,right=4pt] ([yshift=2pt]0,1) -- ([yshift=-2pt]0,2)         node[midway,right]{b} ;

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

PSTricks 解决方案:

\documentclass{article}

\usepackage{multido}
\usepackage{pstricks}
\psset{dimen = m}
\usepackage{xfp}

% parameters
\def\width{1.5}
\def\height{1}
\def\widthNo{4}
\def\heightNo{3}
\def\spacing{0.2}

\begin{document}

\begin{pspicture}(-0.4,-0.1)(\fpeval{\width*\widthNo+0.43},\fpeval{\height*\heightNo+0.1})
  \multido{\rA = 0+\width}{\widthNo}{%
    \multido{\rB = 0+\height}{\heightNo}{%
      \psframe(\rA,\rB)(\fpeval{\rA+\width},\fpeval{\rB+\height})}}
  \uput[180](0,0){$A$}
  \uput[0](\fpeval{\width*\widthNo},\fpeval{\height*\heightNo}){$B$}
  \psset{labelsep = 2pt}
  \multido{\rA = \spacing+\width, \rB = \fpeval{\width/2}+\width}{\widthNo}{%
    \psline[linecolor = red]{->}(\rA,0.15)(\fpeval{\rA+\width-2*\spacing},0.15)
    \uput[90](\rB,0.15){\textcolor{blue!50}{$a$}}}
  \multido{\rA = \spacing+\height, \rB = \fpeval{\height/2}+\height}{\heightNo}{%
    \psline[linecolor = red]{->}(0.15,\rA)(0.15,\fpeval{\rA+\height-2*\spacing})
    \uput[0](0.15,\rB){\textcolor{blue!50}{$b$}}}
\end{pspicture}

\end{document}

输出

您所要做的就是改变参数的值,绘图就会相应地调整。

相关内容