I am trying to draw linear combinations of vectors. I started defining coordinates manually, but it's too time- and space-consuming:
\begin{center}
\pgfplotsset{compat=1.15}
{
\begin{tikzpicture}[scale=1]
\begin{axis}
[
% legend pos=outer north east,
grid,
xmin=-7, xmax=7, ymin=-7, ymax=7,
% xlabel = {$x$}, ylabel = {$y$},
xtick={-6,-5,...,6},
ytick={-6,-5,...,6},
xticklabels={-6,,-4,,-2,,0,,2,,4,,6,},
yticklabels={-6,,-4,,-2,,0,,2,,4,,6,},
clip=false,
grid = major, axis lines = middle,
axis line style={gray}
]
\coordinate (o) at (0,0);
\coordinate (c) at (1,2);
\coordinate (2c) at (2,4);
\coordinate (3c) at (3,6);
\coordinate (4c) at (4,8);
\coordinate (-c) at (-1,-2);
\coordinate (-2c) at (-2,-4);
\coordinate (-3c) at (-3,-6);
\coordinate (-4c) at (-4,-8);
\coordinate (d) at (1,1);
\coordinate (2d) at (2,2);
\coordinate (3d) at (3,3);
\coordinate (4d) at (4,4);
\coordinate (-d) at (-1,-1);
\coordinate (-2d) at (-2,-2);
\coordinate (-3d) at (-3,-3);
\coordinate (-4d) at (-4,-4);
\coordinate (c+d) at (2,3);
\coordinate (c+2d) at (3,4);
\coordinate (c+3d) at (4,5);
\coordinate (c+4d) at (5,6);
\coordinate (2d) at (2,2);
%total
\draw[line width=1pt, orange, -latex](o)--(c+2d) node[xshift=3mm, yshift=3mm]{$\vec{\bm{c}}+2\vec{\bm{d}}$};
%Start with c
\draw[line width=1pt, green, -latex](o)--(c) node[midway, xshift=-3mm, yshift=2mm]{$\vec{\bm{c}}$};
\draw[line width=1pt, violet, -latex](c)--(c+d) node[midway, xshift=-3mm, yshift=2mm]{$\vec{\bm{d}}$};
\draw[line width=1pt, violet, -latex](c+d)--(c+2d) node[midway, xshift=-3mm, yshift=2mm]{$\vec{\bm{d}}$};
%Start with d
\draw[line width=1pt, violet, -latex](o)--(d) node[midway, xshift=2mm, yshift=-2mm]{$\vec{\bm{d}}$};
\draw[line width=1pt, violet, -latex](d)--(2d) node[midway, xshift=2mm, yshift=-2mm]{$\vec{\bm{d}}$};
\draw[line width=1pt, green, -latex](2d)--(c+2d) node[midway, xshift=2mm, yshift=-2mm]{$\vec{\bm{c}}$};
\end{axis}
\end{tikzpicture}
}
\end{center}
I figured I could use for loops to accomplish this more efficiently:
I tried this, but it does not work:
\begin{center}
\pgfplotsset{compat=1.15}
{
\begin{tikzpicture}[scale=1]
\begin{axis}
[
% legend pos=outer north east,
grid,
xmin=-7, xmax=7, ymin=-7, ymax=7,
% xlabel = {$x$}, ylabel = {$y$},
xtick={-6,-5,...,6},
ytick={-6,-5,...,6},
xticklabels={-6,,-4,,-2,,0,,2,,4,,6,},
yticklabels={-6,,-4,,-2,,0,,2,,4,,6,},
clip=false,
grid = major, axis lines = middle,
axis line style={gray}
]
\coordinate (o) at (0,0);
\coordinate (c) at (1,2);
\coordinate (d) at (1,1);
\foreach \i in {-4, -3, ..., 4} {
\coordinate (\ic) at (\i, 2*\i);
\coordinate (\id) at (\i, \i);
\foreach \j in {-4, -3, ..., 4} {
\coordinate (\ic+\jd) at (\i+\j, 2*\i+\j);
}
}
%total
\draw[line width=1pt, orange, -latex](o)--(c+2d) node[xshift=3mm, yshift=3mm]{$\vec{\bm{c}}+2\vec{\bm{d}}$};
%Start with c
\draw[line width=1pt, green, -latex](o)--(c) node[midway, xshift=-3mm, yshift=2mm]{$\vec{\bm{c}}$};
\draw[line width=1pt, violet, -latex](c)--(1c+1d) node[midway, xshift=-3mm, yshift=2mm]{$\vec{\bm{d}}$};
\draw[line width=1pt, violet, -latex](1c+1d)--(1c+2d) node[midway, xshift=-3mm, yshift=2mm]{$\vec{\bm{d}}$};
%Start with d
\draw[line width=1pt, violet, -latex](o)--(d) node[midway, xshift=2mm, yshift=-2mm]{$\vec{\bm{d}}$};
\draw[line width=1pt, violet, -latex](d)--(0c+2d) node[midway, xshift=2mm, yshift=-2mm]{$\vec{\bm{d}}$};
\draw[line width=1pt, green, -latex](2d)--(1c+2d) node[midway, xshift=2mm, yshift=-2mm]{$\vec{\bm{c}}$};
\end{axis}
\end{tikzpicture}
}
\end{center}
So I have two questions:
- How can I get this to work with a for loop instead of defining each coordinate manually?
- Even the original solution doesn't work if I don't set compatibility backward with
\pgfplotsset{compat=1.15}
. Is there something about my code that needs to be updated to work with newer versions? What should I adjust to become forward-compatible?
\i c
etc. instead of\ic
. You haven't defined\ic
but only\i
. And please post compilable code rather than a snippet.