Graphics, Figures & Tablesgraphics and loops

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
boxus
Posts: 1
Joined: Sun Mar 26, 2023 12:18 pm

graphics and loops

Post by boxus »

Hello, friends! I have a simple code for drawing blue dots:

Code: Select all

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro\m{5}
  \pgfmathsetmacro\n{4}
  \begin{axis}[    
    xmin=0, xmax=\m, ymin=0, ymax=\n,
    xtick pos=left,ytick pos=bottom,
    grid = major, axis line style={draw=none},
    ]
    % dots:
    \foreach \i in {0,...,\m}
    \foreach \j in {0,...,\n} {
      \addplot+[mark=*,only marks, mark options={color=blue}] coordinates {(\i,\j)};
    }    
    % left arrows:
    \foreach \i in {0,...,\inteval{\m - 1}} 
    \foreach \j in {0,...,\n} {
       \draw[->, blue, thick] (axis cs: \inteval{\i}, \inteval{\j}) -- (axis cs: \inteval{\i+1}, \inteval{\j});
    }
  \end{axis}
\end{tikzpicture}
\end{document}
and it works nice in Overleaf. Though any attempts to add unit horizontal vectors fail, that is the next code generate errors (Undefined control sequence.) and I do not understand why.

Code: Select all

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro\m{5}
  \pgfmathsetmacro\n{4}
  \begin{axis}[    
    xmin=0, xmax=\m, ymin=0, ymax=\n,
    xtick pos=left,ytick pos=bottom,
    grid = major, axis line style={draw=none},
    ]
    % dots:
    \foreach \i in {0,...,\m}
    \foreach \j in {0,...,\n} {
      \addplot+[mark=*,only marks, mark options={color=blue}] coordinates {(\i,\j)};
    }    
    % left arrows:
    \foreach \i in {0,...,\inteval{\m - 1}} 
    \foreach \j in {0,...,\n} {
      \draw[->, blue, thick] (axis cs: \i, \j) -- (axis cs: \fpeval{\i+1}, \j);
    }
  \end{axis}
\end{tikzpicture}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

graphics and loops

Post by Bartman »

I'd also like to know why the arrows in your first example aren't drawn and why the second example doesn't run without an error.

If you're looking for a solution to draw the arrows apart from the answers to these questions, the following should work:

Code: Select all

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{tikzpicture}[>=Latex]
\pgfmathsetmacro\m{5}
\pgfmathsetmacro\n{4}
\begin{axis}[
  clip=false,% added
  xmin=0, xmax=\m, ymin=0, ymax=\n,
  grid = major, 
  axis line style={draw=none},
%  x tick label style={below=1pt}% added
]

\foreach \i in {0,...,\m}
  \foreach \j in {0,...,\n}{
    % dots:
    \addplot[mark=*, blue] coordinates {(\i,\j)};
    % left arrows:
    \ifnum\i<\m
      \addplot[->, blue, thick] coordinates {(\i, \j) (\i+1, \j)};
    \fi
  }
\end{axis}
\end{tikzpicture}
\end{document}
Post Reply