4

I'm trying to plot a vector field with 5 by 5 arrows (25 in total). It looks good, expect for the fact that it arbitrarily chooses "up" as the direction of the 0-vector. How can I tell it to not display 0-length arrows at all?

My code:

\documentclass{article}
\tikzset{>=stealth} 
\usepackage{tikz}
\usepackage{pgfplots} 
\begin{document}
\begin{tikzpicture}

\draw[->, thick] (-3,0)--(3,0) node[right] {$x$};
\draw[->, thick] (0,-3)--(0,3) node[above] {$y$};

\def\factor{0.15}
\foreach \x in {-2.5,-2,...,2.5} {
    \foreach \y in {-2.5,-2,...,2.5}{
            \draw[->] (\x,\y)--++(\factor*\y,-\factor*\x);
    }
}
\end{tikzpicture}
\end{document}

Result (note the arrow tip at the origin):

enter image description here

3 Answers 3

3

One possibility with \ifthenelse:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots} 
\usepackage{ifthen}
\tikzset{>=stealth} 
\begin{document}
\begin{tikzpicture}

\draw[->, thick] (-3,0)--(3,0) node[right] {$x$};
\draw[->, thick] (0,-3)--(0,3) node[above] {$y$};

\def\factor{0.15}
\foreach \x in {-2.5,-2,...,2.5} {
    \foreach \y [evaluate=\y as \r using int(ceil(abs(\x)+abs(\y)))] in {-2.5,-2,...,2.5}{
            \ifthenelse{\r=0}{}{\draw[->] (\x,\y)--++(\factor*\y,-\factor*\x);}
    }
}
\end{tikzpicture}
\end{document}

enter image description here

1
  • This is a great answer because of its generality: it doesn't directly check if x=y=0, it checks if the vector itself is 0.
    – 35T41
    Sep 8, 2021 at 14:22
3

Throw in a couple \ifx checks for zero.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots} 
\tikzset{>=stealth} 
\newcommand*\zero{0}
\begin{document}
\begin{tikzpicture}

\draw[->, thick] (-3,0)--(3,0) node[right] {$x$};
\draw[->, thick] (0,-3)--(0,3) node[above] {$y$};

\def\factor{0.15}
\foreach \x in {-2.5,-2,...,2.5} {
    \foreach \y in {-2.5,-2,...,2.5}{
     \def\tmp{\draw[->] (\x,\y)--++(\factor*\y,-\factor*\x)}
     \ifx\zero\x\ifx\zero\y\else\tmp\fi\else\tmp\fi;
    }
}
\end{tikzpicture}
\end{document}

enter image description here

3
  • Thanks! I'm not familiar with this syntax (what is \fi?), can you please direct me to a (preferably simple) explanation? Also, I notice the use of a command \zero; is it generalizable to other conditional statements? E.g. x is not 2, x is greater than 1 etc.
    – 35T41
    Sep 8, 2021 at 13:45
  • 2
    @35T41 TeX provides basic conditonals (see TeXbook by Knuth, p.209), the one I use here is \ifx which tests two unexpanded tokens for identicality. The form of all if-conditions are \ifx<condition><true code>\else<false code>\fi So fi, being if backwards, is what closes out the if condition. Sep 8, 2021 at 13:50
  • @35T41 Thus, in my code, I compare the identicality of \zero and \x and also \zero and \y. If both conditions are true, I do nothing, otherwise, I execute \tmp, which is the \draw statement. Sep 8, 2021 at 13:55
2

It's best to avoid decimal numbers in \foreach, because of rounding problems. You can test whether both \x and \y are zero:

\documentclass{article}
\usepackage{tikz}

\tikzset{>=stealth} 

\begin{document}

\begin{tikzpicture}

\draw[->, thick] (-3,0)--(3,0) node[right] {$x$};
\draw[->, thick] (0,-3)--(0,3) node[above] {$y$};

\def\factor{0.15}
\foreach \x in {-5,-4,...,5} {
  \foreach \y in {-5,-4,...,5}{
    \pgfmathparse{ifthenelse(and(\x==0,\y==0),1,0)}
    \ifnum\pgfmathresult=0
      \draw[->] (\x/2,\y/2)--++(\factor*\y/2,-\factor*\x/2);
    \fi
  }
}
\end{tikzpicture}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.