0

What is the right setting to get a decoration at every center of every box?

enter image description here

\documentclass[margin=5mm, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}

\begin{tikzpicture}[>=latex, scale=0.5]
\draw (0,0) grid (8,8); 

\tikzset{mymarks/.style={
->, 
decoration={markings,
mark=
between positions 0 and 1 step 0.7071 cm
with {\arrow{>}}},
postaction={decorate}
}
}

\draw[mymarks,red] (0.5,0.5) -- (7.5,7.5); 
\draw[mymarks,blue] (3.5,0.5) -- (3.5,5.5); 
\end{tikzpicture}
\end{document}
3

You correctly chose the 1/sqrt(2) factor for the diagonal path. Why do you expect that this is correct factor for the vertical path? The following allows you to choose the factor. However, it does not automatically dial it. In fact, an automatic solution for arbitrary paths will be nontrivial because it is not guaranteed that these run through the centers of the cells.

\documentclass[margin=5mm, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}

\begin{tikzpicture}[>=latex, scale=0.5]
\draw (0,0) grid (8,8); 

\tikzset{mymarks/.style={
->, 
decoration={markings,
mark=
between positions 0 and 0.99 step #1
with {\draw[-latex](0,0)--(0.1,0);}},
postaction={decorate}
}
}

\draw[mymarks=0.7071cm,red] (0.5,0.5) -- (7.5,7.5); 
\draw[mymarks=0.5cm,blue] (3.5,0.5) -- (3.5,5.5); 
\end{tikzpicture}
\end{document}

enter image description here

Here is something for your chess board setup: you can add arrows in certain directions, where the numbers have the same meaning as on the numeric keyoard, i.e. 1 means down left, 2 means down ... and 9 means up right. You can also use * to indicate several steps in a given direction. This is illustrated in the following example.

\documentclass[tikz,border=3.14mm]{standalone}
\tikzset{1/.style={insert path={edge[->] ++ (-1.1,-1.1) (-1,-1)}},
2/.style={insert path={edge[->] ++ (0,-1.1) ++(0,-1)}},
3/.style={insert path={edge[->] ++ (1.1,-1.1) ++(1,-1)}},
4/.style={insert path={edge[->] ++ (-1.1,0) ++(-1,0)}},
6/.style={insert path={edge[->] ++ (1.1,0) ++(1,0)}},
7/.style={insert path={edge[->] ++ (-1.1,1.1)++ (-1,1)}},
8/.style={insert path={edge[->] ++ (0,1.1) ++(0,1)}},
9/.style={insert path={edge[->] ++ (1.1,1.1)++ (1,1)}},
*/.style 2 args={insert path={foreach \X in {1,...,#1} {[#2]}}}}
\begin{document}
\begin{tikzpicture}[>=latex, scale=0.5]
\draw (0,0) grid (8,8); 
 \draw[red] (0.5,0.5) [*={7}{9}];
 \draw[blue]  (3.5,0.5) [*={5}{8}];
 \draw[green!70!black]  (6.5,0.5) [8,4,7,8,8,7,4,8];
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • Mmmhh, I hoped for a automatical solution. – cis Jun 25 '19 at 21:07
  • @cis Depending on how you define "automatic solution" it can be almost trivial to very cumbersome. If you guarantee that the path always starts at a center and runs through the centers of the other cells, it is almost trivial because then there are only two cases. If you have curves that run through the grid and want to mark whenever the curve hits a center, it will be much harder. – user121799 Jun 25 '19 at 21:11
  • OK, I did not say: "It is like a cheesboard". – cis Jun 25 '19 at 21:13
  • @cis For that you may want to ask a completely new question. TikZ has now section 92 Parser Module which would allow you to convert strings into paths over a chess board. Or you work with a comma-separated list. In any case, if you really want to describe paths over a chess board, there are much more elegant possibilities. Whether or not they are already part of some chess package, I do not know, but would not at all be surprised if they are. – user121799 Jun 25 '19 at 21:29
0

I used a command to compare x- and y-coordinates:

\pgfkeys{/tikz/savevalue/.code 2 args={\global\edef#1{#2}}}

% \move[options]{figure}{start}{end}
\newcommand\move[4][]{%
\path let \p1 = (#3) in coordinate (start) at (\x1,\y1) node[savevalue={\xStart}{\x1}, savevalue={\yStart}{\y1}]{};
\path let \p2 = (#4) in coordinate (end) at (\x2,\y2) node[savevalue={\xEnd}{\x2}, savevalue={\yEnd}{\y2}]{};
\pgfmathsetmacro\xTest{\xStart == \xEnd ? 1 : 0}
\pgfmathsetmacro\yTest{\yStart == \yEnd ? 1 : 0}
\pgfmathsetmacro\Test{\xTest==1 || \yTest== 1 ? "g" : "d"}
\draw[mymarks=\csname \Test \endcsname, #1] (start) -- (end) 
%node[align=left, anchor=west]{\xStart, \yStart;\\ \xEnd, \yEnd;\\ \xTest, \yTest, \Test} % Test
; 
\draw[fill=white] (start) circle(0.45) node{$#2$}; % Figur
}

\move[red]{D}{c-4}{g-8} 

(but this only works for straight and diagonal lines).

enter image description here

\documentclass[margin=5pt, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc}
\usepackage{amsmath, amssymb}

\begin{document}
\pgfkeys{/tikz/savevalue/.code 2 args={\global\edef#1{#2}}}

\begin{tikzpicture}[>=latex, scale=0.5,
font=\small]
% KoSy
%\draw[] (0,0) grid (8,8);
\foreach[count=\S from 0, count=\s] \t in {a,b,...,h} {
\path[] (\S,0) -- (\s,0) node[midway,anchor=north] {\strut \t};
\path[] (0,\S) -- (0,\s) node[midway,anchor=east] {\s};
\foreach[count=\Y from 0] \y in {1,...,8} {%
% coordinates
\coordinate[] (\t-\y) at (\S+0.5,\y-0.5);  
% filling
\pgfmathparse{mod(\S+\Y,2) ? "lightgray!50" : "white"}
\colorlet{Farbe}{\pgfmathresult}
\path[fill=Farbe, draw=black] (\S,\Y) rectangle ++(1,1);    }%
}


% Moves
\pgfmathsetmacro\D{sqrt(2)/2}
\pgfmathsetlengthmacro\d{\D cm}
\pgfmathsetlengthmacro\g{0.5cm}
\tikzset{mymarks/.style={->, 
decoration={markings,
mark=
between positions 0 and 0.99 step #1
with {\draw[-latex](0,0)--(0.1,0);}},
postaction={decorate}
}}

% \move[options]{figure}{start}{end}
\newcommand\move[4][]{%
\path let \p1 = (#3) in coordinate (start) at (\x1,\y1) node[savevalue={\xStart}{\x1}, savevalue={\yStart}{\y1}]{};
\path let \p2 = (#4) in coordinate (end) at (\x2,\y2) node[savevalue={\xEnd}{\x2}, savevalue={\yEnd}{\y2}]{};
\pgfmathsetmacro\xTest{\xStart == \xEnd ? 1 : 0}
\pgfmathsetmacro\yTest{\yStart == \yEnd ? 1 : 0}
\pgfmathsetmacro\Test{\xTest==1 || \yTest== 1 ? "g" : "d"}
\draw[mymarks=\csname \Test \endcsname, #1] (start) -- (end) 
%node[align=left, anchor=west]{\xStart, \yStart;\\ \xEnd, \yEnd;\\ \xTest, \yTest, \Test} % Test
; 
\draw[fill=white] (start) circle(0.45) node{$#2$}; % Figur
}

% \move[red]{D}{c-4}{g-8} % Test
% \move{D}{c-4}{h-4} % Test
% \move{D}{c-4}{c-8} % Test

\foreach \Ends in {a-4,c-8,h-4,c-1,g-8,f-1,a-2,a-6}{ 
\move[red]{Q}{c-4}{\Ends}  }
\end{tikzpicture}
\end{document}
| improve this answer | |

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.