1

I have the following picture and I'm looking to draw a legend that contains arrows in it and labels for each of the blue and orange arrows.

Can somebody give me pointers on how I should go about doing this? I tried doing it with a node but cant seem to draw an arrow within a node

Here is what I have so far:

\begin{tikzpicture}

\draw[step=0.5cm,color=gray] (-2,-2) grid (2,2);
\node(start) at (-1.75,1.75)[fill=green]{};
\node(end) at (0.25,-1.75)[fill=red]{};
\
  \foreach \y in {-1.75,-1.25,...,1} {
      \foreach \x in {-1.75,-1.25,...,0} {
          \node[fill=gray,scale=1.75] at (\x,\y){}; 
      }
  }

  \foreach \x in {-1.75,-1.25,...,1.25}{
        \draw[-latex,color=blue](\x,1.75)--(\x+0.5,1.75);}

    \foreach \y in{1.75,1.25,...,-1.25}{
        \draw[-latex,color=blue](1.75,\y)--(1.75,\y-0.5);
    }
    \foreach \x in {1.75,1.25,...,0.75}{
        \draw[-latex,color=blue](\x,-1.75)--(\x-0.5,-1.75);}
     \draw[-latex,orange](-1.75,1.75)--(-1.75,1.25);   
    \foreach \x in {-1.75,-1.25,...,-0.25}{
        \draw[-latex,orange](\x,1.25)--(\x+0.5,1.25);}

    \foreach \y in{1.25,0.75,...,-1.25}{
        \draw[-latex,color=orange](0.25,\y)--(0.25,\y-0.5);
    }
\end{tikzpicture}

enter image description here

1

2 Answers 2

3

You just need a matrix.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}

\draw[step=0.5cm,color=gray] (-2,-2) grid (2,2);
\node(start) at (-1.75,1.75)[fill=green]{};
\node(end) at (0.25,-1.75)[fill=red]{};

  \foreach \y in {-1.75,-1.25,...,1} {
      \foreach \x in {-1.75,-1.25,...,0} {
          \node[fill=gray,scale=1.75] at (\x,\y){}; 
      }
  }

  \foreach \x in {-1.75,-1.25,...,1.25}{
        \draw[-latex,color=blue](\x,1.75)--(\x+0.5,1.75);}

    \foreach \y in{1.75,1.25,...,-1.25}{
        \draw[-latex,color=blue](1.75,\y)--(1.75,\y-0.5);
    }
    \foreach \x in {1.75,1.25,...,0.75}{
        \draw[-latex,color=blue](\x,-1.75)--(\x-0.5,-1.75);}
     \draw[-latex,orange](-1.75,1.75)--(-1.75,1.25);   
    \foreach \x in {-1.75,-1.25,...,-0.25}{
        \draw[-latex,orange](\x,1.25)--(\x+0.5,1.25);}

    \foreach \y in{1.25,0.75,...,-1.25}{
        \draw[-latex,color=orange](0.25,\y)--(0.25,\y-0.5);
    }
 \path ([xshift=1cm,yshift=-5mm]current bounding box.north east)
 node[matrix,anchor=north west,cells={nodes={font=\sffamily,anchor=west}},
 draw,thick,inner sep=1ex]{
  \draw[-latex,color=orange](0,0) -- ++ (0.6,0); & \node{Label 1};\\
  \draw[-latex,color=blue](0,0) -- ++ (0.6,0); & \node{Label 2};\\
 };
\end{tikzpicture}
\end{document}

enter image description here

BTW, this seems to cry for the parser library. This allows one to simplify the meandering arrow paths to

 \draw[-latex,color=blue,every
 edge/.style={draw,-latex,color=blue}](start.center)
  [arrow moves={rrrrrrrdddddddll}] -- (end.center);
 \draw[-latex,color=orange,every edge/.style={draw,-latex}](start.center)
  [arrow moves={drrrrddddd}] -- (end.center);

(One may simplify it further.)

\documentclass[tikz,border=3mm]{standalone}
\usepgfmodule{parser}
\pgfparserdef{arrowparser}{initial}{the letter l}% 
{\edef\temp{\temp\space edge ++(-\pgfkeysvalueof{/tikz/am/x},0) -- ++ (-\pgfkeysvalueof{/tikz/am/x},0)}}
\pgfparserdef{arrowparser}{initial}{the letter r}% 
{\edef\temp{\temp\space edge ++(\pgfkeysvalueof{/tikz/am/x},0) -- ++ (\pgfkeysvalueof{/tikz/am/x},0)}}
\pgfparserdef{arrowparser}{initial}{the letter u}% 
{\edef\temp{\temp\space edge ++(0,\pgfkeysvalueof{/tikz/am/y}) -- ++ (0,\pgfkeysvalueof{/tikz/am/y})}}
\pgfparserdef{arrowparser}{initial}{the letter d}% 
{\edef\temp{\temp\space edge ++(0,-\pgfkeysvalueof{/tikz/am/y}) -- ++ (0,-\pgfkeysvalueof{/tikz/am/y})}}
\pgfparserdef{arrowparser}{initial}{the character ;}% 
{\pgfparserswitch{final}}

\begin{document}
\begin{tikzpicture}[am/x/.initial=5mm,am/y/.initial=5mm,
    arrow moves/.code={\edef\temp{}%
    \pgfparserparse{arrowparser}#1;
    \tikzset{insert path=\temp}}]
  \draw[step=0.5cm,color=gray] (-2,-2) grid (2,2);
  \node(start) at (-1.75,1.75)[fill=green]{};
  \node(end) at (0.25,-1.75)[fill=red]{};
  \foreach \y in {-1.75,-1.25,...,1} {
      \foreach \x in {-1.75,-1.25,...,0} {
          \node[fill=gray,scale=1.75] at (\x,\y){}; 
      }
  }
 \draw[-latex,color=blue,every
 edge/.style={draw,-latex,color=blue}](start.center)
  [arrow moves={rrrrrrrdddddddll}] -- (end.center);
 \draw[-latex,color=orange,every edge/.style={draw,-latex}](start.center)
  [arrow moves={drrrrddddd}] -- (end.center);
%
 \path ([xshift=1cm,yshift=-5mm]current bounding box.north east)
 node[matrix,anchor=north west,cells={nodes={font=\sffamily,anchor=west}},
 draw,thick,inner sep=1ex]{
  \draw[-latex,color=orange](0,0) -- ++ (0.6,0); & \node{Label 1};\\
  \draw[-latex,color=blue](0,0) -- ++ (0.6,0); & \node{Label 2};\\
 };
\end{tikzpicture}
\end{document}

enter image description here

1

Here is another try, using tikz's decoration utility.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{
  decorations.markings,
  arrows.meta
}

\def\cellsep{.1}

% #1 = coordinate, e.g., (1, 1)
% #2 = color
\newcommand{\fillcell}[2]{%
  \fill[color=#2] #1 ++(-1 + \cellsep, -1 + \cellsep) 
    rectangle +(1 - 2*\cellsep, 1 - 2*\cellsep); 
}

\tikzset{
  coord/.style={shift={(-.5, -.5)}},
  line with arrows/.style={postaction=decorate, coord}
}

\begin{document}
\begin{tikzpicture}[decoration={markings,
  mark=between positions .5cm and -.5cm step .9cm with {\arrow{Latex}}}]
  % grid
  \draw[gray] (0, 0) grid (8, 8);
  % filled cells
  \foreach \i in {1, ..., 5} {
    \foreach \j in {1, ..., 3} {
      \fillcell{(\i, \j)}{gray}
    }
  }
  \fillcell{(1, 6)}{blue}
  \fillcell{(8, 1)}{green}
  % line with arrows
  \draw[line with arrows, orange] 
    (1, 6) -- (8, 6) -- (8, 1);
  \draw[line with arrows, red] 
    (1, 6) -- (1, 4) -- (6, 4) -- (6, 1) -- (8, 1);
  % coordinate tips (helper)
  \foreach \i in {1, ..., 8} {
    \foreach \j in {1, ..., 8} {
      \node[coord, black!60, font=\small] at (\i, \j) {(\i, \j)};
    }
  }
\end{tikzpicture}
\end{document}

output of the example

Lines adding coordinate tips in the center of each cell can be safely commented, when the whole picture is finished.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .