# Simplifying Tikz picture of a span

I've made a picture to show how a span works and I know there's a more efficient way of doing this:

\begin{tikzpicture} [thick,scale=0.5, every node/.style={scale=0.5}]
\draw[step=1cm,gray,very thin] (-1.25,-1.25) grid (1.25,1.25);
\draw[latex-latex] (-1.25,0) -- (1.25,0) node[right] {$x$-axis} ;
\draw[latex-latex] (0,-1.25) -- (0,1.25) node[above] {$y$-axis} ;
\draw[red][ultra thick][->] (0,0) -- (0.4,0.4);
\draw[red][ultra thick][->] (0,0) -- (0.8,0.8);
\draw[red][ultra thick][->] (0,0) -- (1.2,1.2);
\draw[red][ultra thick][->] (0,0) -- (-0.4,-0.4);
\draw[red][ultra thick][->] (0,0) -- (-0.8,-0.8);
\draw[red][ultra thick][->] (0,0) -- (-1.2,-1.2);
\draw[red][ultra thick][->] (0,0) -- (-0.4,0.4);
\draw[red][ultra thick][->] (0,0) -- (-0.8,0.8);
\draw[red][ultra thick][->] (0,0) -- (-1.2,1.2);
\draw[red][ultra thick][->] (0,0) -- (0.4,-0.4);
\draw[red][ultra thick][->] (0,0) -- (0.8,-0.8);
\draw[red][ultra thick][->] (0,0) -- (1.2,-1.2);
\draw[red][ultra thick][->] (0,0) -- (0,0.4);
\draw[red][ultra thick][->] (0,0) -- (0,0.8);
\draw[red][ultra thick][->] (0,0) -- (0,1.2);
\draw[red][ultra thick][->] (0,0) -- (0,-0.4);
\draw[red][ultra thick][->] (0,0) -- (0,-0.8);
\draw[red][ultra thick][->] (0,0) -- (0,-1.2);
\draw[red][ultra thick][->] (0,0) -- (-0.4,0);
\draw[red][ultra thick][->] (0,0) -- (-0.8,0);
\draw[red][ultra thick][->] (0,0) -- (-1.2,0);
\draw[red][ultra thick][->] (0,0) -- (0.4,0);
\draw[red][ultra thick][->] (0,0) -- (0.8,0);
\draw[red][ultra thick][->] (0,0) -- (1.2,0);
\end{tikzpicture}


Right now it works since the vectors are simple, but I want to make a function that draws within a given region the span of a given vector, or set of vectors.

I enlarged the grid to avoid overlapping arrows.

\documentclass[border=10mm]{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[thick,scale=0.5, every node/.style={scale=0.5},]
\draw[step=1cm,gray,very thin] (-2.5,-2.5) grid (2.5,2.5);
\draw[latex-latex] (-2.5,0) -- (2.5,0) node[right] {$x$-axis} ;
\draw[latex-latex] (0,-2.5) -- (0,2.5) node[above] {$y$-axis} ;

\begin{scope}[red,ultra thick]
\foreach \i in {.4,.8,1.2}
{\foreach \j [evaluate =\j as \jj using \j*\i] in {-1,1}
{\draw [->](0,0)--(\i,\jj);
\draw [->](0,0)--(0,\jj);
\draw[->](0,0)--(-\i,\jj);
\draw[->](0,0)--(\jj,0);
}}
\end{scope}

\end{tikzpicture}
\end{document}

• This draws a couple of \draw[->] (0,0) -- (0,0);, which does not look good unless you do something like tips=proper or tips=on proper draw, but it would be better not to draw any vector of length 0. – Schrödinger's cat Nov 11 at 14:09
• There, it's done. – AndréC Nov 11 at 14:58
• You could at least mention that you fixed an issue after others pointed it out. – Schrödinger's cat Nov 11 at 17:17

You could use a pic for that. I added a pic definition with which drawing the span of two vectors is as simple as saying

 \pic[red,ultra thick]{span={vector 1={(1,0)},vector 2={(0,1)}}};


This can be used as follows:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[thick,scale=0.5,nodes={scale=0.5},
pics/span/.style={code={\tikzset{span/.cd,#1}
\foreach \XX in {-1,0,1}
{\foreach \YY in {-1,0,1}
{\pgfmathtruncatemacro{\itest}{ifthenelse(\XX==0 && \YY==0,0,1)}
\ifnum\itest=1
\edef\temp{\noexpand\draw[pic actions,->] (0,0) --
($\XX*\pgfkeysvalueof{/tikz/span/vector 1} +\YY*\pgfkeysvalueof{/tikz/span/vector 2}$);}
\temp
\fi}
}
}},span/vector 1/.initial={(1,0)},span/vector 2/.initial={(0,1)},
pics/axis/.style={code={
\draw[step=1cm,gray,very thin] (-#1,-#1) grid (#1,#1);
\draw[latex-latex] (-#1,0) -- (#1,0) node[right] {$x$-axis};
\draw[latex-latex] (0,-#1) -- (0,#1) node[above] {$y$-axis};
}}]
\pic{axis=3.5};
\foreach \X in {0.8,1.6,2.4}
{\pic[red,ultra thick,>=stealth]{span={vector 1={(\X,0)},vector 2={(0,\X)}}};}
\end{tikzpicture}
\end{document}


As you can see, this MWE also has a pic for the axis. It also avoids adding the linear combination in which both coefficients are 0.