Indexing arrays in a foreach in tikz












1















The arrays xcoordinates and ycoordinates define points on a grid. I'm simply trying to connect all of the points sequentially with short line segments.



documentclass[12pt]{article}
usepackage{tikz}
usepackage[active,tightpage]{preview}%
PreviewEnvironment{tikzpicture}%
setlengthPreviewBorder{0.1cm}%

usetikzlibrary{positioning}%
usetikzlibrary{shapes}%
usetikzlibrary{arrows}%
usetikzlibrary{calc}%

begin{document}

defthexslant{-1}
deftheyslant{0.5}

newlengthmarkeroffset
setlengthmarkeroffset{0.20cm}

% particle diameter
defmarkerdiameter{0.2cm}


begin{tikzpicture}[scale=.9,every node/.style={minimum size=1cm},on grid]

begin{scope}[
yshift=0cm,every node/.append style={
yslant=theyslant,xslant=thexslant},yslant=theyslant,xslant=thexslant]
fill[white,fill opacity=.9] (0,0) rectangle (5,5);
draw[black,very thick] (0,0) rectangle (5,5);
draw[step=10mm, black] (0,0) grid (5,5);

foreach i / j in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}{%
fill[black] (i - 0.5, j - 0.5) circle (markerdiameter/2);%
node at (i - 0.5, j - 0.5) [blue,xshift=markeroffset, yshift=markeroffset,font=tiny] () {$i,j$};
}%

% draw a red line indicating the path of the survey

defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

% ****** uncomment this to reveal problem
%foreach i in {1,...,15}{
%coordinate (A) at (pgfmathparse{xcoordinates[i]} pgfmathresult, pgfmathparse{ycoordinates[i]} pgfmathresult)-(0.5,0.5);
%coordinate (B) at (pgfmathparse{xcoordinates[i+1]} pgfmathresult, pgfmathparse{ycoordinates[i+1]} pgfmathresult)-(0.5,0.5);
%draw[gray] (B) -- (3,3);
%}

end{scope}

end{tikzpicture}

end{document}


I'm not confident that one can do xcoordinates[i+1]. When I compile this, I get the error:



! Incomplete iffalse; all text was ignored after line 83.


The same mwe with more functionality looks like:



documentclass[12pt]{article}
usepackage{tikz}
usepackage[active,tightpage]{preview}%
PreviewEnvironment{tikzpicture}%
setlengthPreviewBorder{0.1cm}%

usetikzlibrary{positioning}%
usetikzlibrary{shapes}%
usetikzlibrary{arrows}%
usetikzlibrary{calc}%

begin{document}

defthexslant{-1}
deftheyslant{0.5}

newlengthmarkeroffset
setlengthmarkeroffset{0.20cm}

% particle diameter
defmarkerdiameter{0.2cm}


begin{tikzpicture}[scale=.9,every node/.style={minimum size=1cm},on grid]

begin{scope}[
yshift=0cm,every node/.append style={
yslant=theyslant,xslant=thexslant},yslant=theyslant,xslant=thexslant]
fill[white,fill opacity=.9] (0,0) rectangle (5,5);
draw[black,very thick] (0,0) rectangle (5,5);
draw[step=10mm, black] (0,0) grid (5,5);

foreach i / j in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}{%
fill[black] (i - 0.5, j - 0.5) circle (markerdiameter/2);%
node at (i - 0.5, j - 0.5) [blue,xshift=markeroffset, yshift=markeroffset,font=tiny] () {$i,j$};
}%

% draw a red line indicating the path of the survey

defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

% suggestion by marmot
draw[red, very thick] plot[variable=x,samples at={0,...,16}] ({xcoordinates[x]-0.5},{ycoordinates[x]-0.5});

% from the first point to every other point
coordinate (A) at (0.5,0.5);
foreach position in {(1,2), (1,3), (1,4), (1,5), (2,5), (3,5), (3,4), (3,3), (3,2), (3,1), (4,1), (5,1), (5,2), (5,3), (5,4), (5,5)}{
draw[gray] position+(-0.5,-0.5) -- (A);
}%

% and here is how we draw all the possible connections
foreach xa / ya [count=i] in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}
foreach xb / yb [count=j] in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}
{
ifnum i=j
else
draw[gray] (xa-0.5,ya-0.5) -- (xb-0.5,yb-0.5);
fi
}%

end{scope}

end{tikzpicture}

end{document}


This is really quite ugly (done quickly, learning on the fly) because I'm defining the survey points / coordinates in three different ways. They should only be defined once.



Any suggestions on structure are appreciated.










share|improve this question




















  • 4





    please extend your code snippet to complete small document, which reproduce your problem.

    – Zarko
    Jan 22 at 3:49






  • 2





    You cannot subtract coordinates like thisL: (pgfmathparse{xcoordinates[i]} pgfmathresult, pgfmathparse{ycoordinates[i]} pgfmathresult)-(0.5,0.5). You need either to do this with calc or to subtract the components.

    – marmot
    Jan 22 at 4:00











  • Updated question to include mwe.

    – John Chris
    Jan 22 at 13:36











  • Also added another mwe illustrating a problem of my defining the input coordinates multiple times, and in multiple ways.

    – John Chris
    Jan 22 at 13:45
















1















The arrays xcoordinates and ycoordinates define points on a grid. I'm simply trying to connect all of the points sequentially with short line segments.



documentclass[12pt]{article}
usepackage{tikz}
usepackage[active,tightpage]{preview}%
PreviewEnvironment{tikzpicture}%
setlengthPreviewBorder{0.1cm}%

usetikzlibrary{positioning}%
usetikzlibrary{shapes}%
usetikzlibrary{arrows}%
usetikzlibrary{calc}%

begin{document}

defthexslant{-1}
deftheyslant{0.5}

newlengthmarkeroffset
setlengthmarkeroffset{0.20cm}

% particle diameter
defmarkerdiameter{0.2cm}


begin{tikzpicture}[scale=.9,every node/.style={minimum size=1cm},on grid]

begin{scope}[
yshift=0cm,every node/.append style={
yslant=theyslant,xslant=thexslant},yslant=theyslant,xslant=thexslant]
fill[white,fill opacity=.9] (0,0) rectangle (5,5);
draw[black,very thick] (0,0) rectangle (5,5);
draw[step=10mm, black] (0,0) grid (5,5);

foreach i / j in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}{%
fill[black] (i - 0.5, j - 0.5) circle (markerdiameter/2);%
node at (i - 0.5, j - 0.5) [blue,xshift=markeroffset, yshift=markeroffset,font=tiny] () {$i,j$};
}%

% draw a red line indicating the path of the survey

defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

% ****** uncomment this to reveal problem
%foreach i in {1,...,15}{
%coordinate (A) at (pgfmathparse{xcoordinates[i]} pgfmathresult, pgfmathparse{ycoordinates[i]} pgfmathresult)-(0.5,0.5);
%coordinate (B) at (pgfmathparse{xcoordinates[i+1]} pgfmathresult, pgfmathparse{ycoordinates[i+1]} pgfmathresult)-(0.5,0.5);
%draw[gray] (B) -- (3,3);
%}

end{scope}

end{tikzpicture}

end{document}


I'm not confident that one can do xcoordinates[i+1]. When I compile this, I get the error:



! Incomplete iffalse; all text was ignored after line 83.


The same mwe with more functionality looks like:



documentclass[12pt]{article}
usepackage{tikz}
usepackage[active,tightpage]{preview}%
PreviewEnvironment{tikzpicture}%
setlengthPreviewBorder{0.1cm}%

usetikzlibrary{positioning}%
usetikzlibrary{shapes}%
usetikzlibrary{arrows}%
usetikzlibrary{calc}%

begin{document}

defthexslant{-1}
deftheyslant{0.5}

newlengthmarkeroffset
setlengthmarkeroffset{0.20cm}

% particle diameter
defmarkerdiameter{0.2cm}


begin{tikzpicture}[scale=.9,every node/.style={minimum size=1cm},on grid]

begin{scope}[
yshift=0cm,every node/.append style={
yslant=theyslant,xslant=thexslant},yslant=theyslant,xslant=thexslant]
fill[white,fill opacity=.9] (0,0) rectangle (5,5);
draw[black,very thick] (0,0) rectangle (5,5);
draw[step=10mm, black] (0,0) grid (5,5);

foreach i / j in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}{%
fill[black] (i - 0.5, j - 0.5) circle (markerdiameter/2);%
node at (i - 0.5, j - 0.5) [blue,xshift=markeroffset, yshift=markeroffset,font=tiny] () {$i,j$};
}%

% draw a red line indicating the path of the survey

defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

% suggestion by marmot
draw[red, very thick] plot[variable=x,samples at={0,...,16}] ({xcoordinates[x]-0.5},{ycoordinates[x]-0.5});

% from the first point to every other point
coordinate (A) at (0.5,0.5);
foreach position in {(1,2), (1,3), (1,4), (1,5), (2,5), (3,5), (3,4), (3,3), (3,2), (3,1), (4,1), (5,1), (5,2), (5,3), (5,4), (5,5)}{
draw[gray] position+(-0.5,-0.5) -- (A);
}%

% and here is how we draw all the possible connections
foreach xa / ya [count=i] in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}
foreach xb / yb [count=j] in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}
{
ifnum i=j
else
draw[gray] (xa-0.5,ya-0.5) -- (xb-0.5,yb-0.5);
fi
}%

end{scope}

end{tikzpicture}

end{document}


This is really quite ugly (done quickly, learning on the fly) because I'm defining the survey points / coordinates in three different ways. They should only be defined once.



Any suggestions on structure are appreciated.










share|improve this question




















  • 4





    please extend your code snippet to complete small document, which reproduce your problem.

    – Zarko
    Jan 22 at 3:49






  • 2





    You cannot subtract coordinates like thisL: (pgfmathparse{xcoordinates[i]} pgfmathresult, pgfmathparse{ycoordinates[i]} pgfmathresult)-(0.5,0.5). You need either to do this with calc or to subtract the components.

    – marmot
    Jan 22 at 4:00











  • Updated question to include mwe.

    – John Chris
    Jan 22 at 13:36











  • Also added another mwe illustrating a problem of my defining the input coordinates multiple times, and in multiple ways.

    – John Chris
    Jan 22 at 13:45














1












1








1








The arrays xcoordinates and ycoordinates define points on a grid. I'm simply trying to connect all of the points sequentially with short line segments.



documentclass[12pt]{article}
usepackage{tikz}
usepackage[active,tightpage]{preview}%
PreviewEnvironment{tikzpicture}%
setlengthPreviewBorder{0.1cm}%

usetikzlibrary{positioning}%
usetikzlibrary{shapes}%
usetikzlibrary{arrows}%
usetikzlibrary{calc}%

begin{document}

defthexslant{-1}
deftheyslant{0.5}

newlengthmarkeroffset
setlengthmarkeroffset{0.20cm}

% particle diameter
defmarkerdiameter{0.2cm}


begin{tikzpicture}[scale=.9,every node/.style={minimum size=1cm},on grid]

begin{scope}[
yshift=0cm,every node/.append style={
yslant=theyslant,xslant=thexslant},yslant=theyslant,xslant=thexslant]
fill[white,fill opacity=.9] (0,0) rectangle (5,5);
draw[black,very thick] (0,0) rectangle (5,5);
draw[step=10mm, black] (0,0) grid (5,5);

foreach i / j in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}{%
fill[black] (i - 0.5, j - 0.5) circle (markerdiameter/2);%
node at (i - 0.5, j - 0.5) [blue,xshift=markeroffset, yshift=markeroffset,font=tiny] () {$i,j$};
}%

% draw a red line indicating the path of the survey

defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

% ****** uncomment this to reveal problem
%foreach i in {1,...,15}{
%coordinate (A) at (pgfmathparse{xcoordinates[i]} pgfmathresult, pgfmathparse{ycoordinates[i]} pgfmathresult)-(0.5,0.5);
%coordinate (B) at (pgfmathparse{xcoordinates[i+1]} pgfmathresult, pgfmathparse{ycoordinates[i+1]} pgfmathresult)-(0.5,0.5);
%draw[gray] (B) -- (3,3);
%}

end{scope}

end{tikzpicture}

end{document}


I'm not confident that one can do xcoordinates[i+1]. When I compile this, I get the error:



! Incomplete iffalse; all text was ignored after line 83.


The same mwe with more functionality looks like:



documentclass[12pt]{article}
usepackage{tikz}
usepackage[active,tightpage]{preview}%
PreviewEnvironment{tikzpicture}%
setlengthPreviewBorder{0.1cm}%

usetikzlibrary{positioning}%
usetikzlibrary{shapes}%
usetikzlibrary{arrows}%
usetikzlibrary{calc}%

begin{document}

defthexslant{-1}
deftheyslant{0.5}

newlengthmarkeroffset
setlengthmarkeroffset{0.20cm}

% particle diameter
defmarkerdiameter{0.2cm}


begin{tikzpicture}[scale=.9,every node/.style={minimum size=1cm},on grid]

begin{scope}[
yshift=0cm,every node/.append style={
yslant=theyslant,xslant=thexslant},yslant=theyslant,xslant=thexslant]
fill[white,fill opacity=.9] (0,0) rectangle (5,5);
draw[black,very thick] (0,0) rectangle (5,5);
draw[step=10mm, black] (0,0) grid (5,5);

foreach i / j in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}{%
fill[black] (i - 0.5, j - 0.5) circle (markerdiameter/2);%
node at (i - 0.5, j - 0.5) [blue,xshift=markeroffset, yshift=markeroffset,font=tiny] () {$i,j$};
}%

% draw a red line indicating the path of the survey

defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

% suggestion by marmot
draw[red, very thick] plot[variable=x,samples at={0,...,16}] ({xcoordinates[x]-0.5},{ycoordinates[x]-0.5});

% from the first point to every other point
coordinate (A) at (0.5,0.5);
foreach position in {(1,2), (1,3), (1,4), (1,5), (2,5), (3,5), (3,4), (3,3), (3,2), (3,1), (4,1), (5,1), (5,2), (5,3), (5,4), (5,5)}{
draw[gray] position+(-0.5,-0.5) -- (A);
}%

% and here is how we draw all the possible connections
foreach xa / ya [count=i] in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}
foreach xb / yb [count=j] in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}
{
ifnum i=j
else
draw[gray] (xa-0.5,ya-0.5) -- (xb-0.5,yb-0.5);
fi
}%

end{scope}

end{tikzpicture}

end{document}


This is really quite ugly (done quickly, learning on the fly) because I'm defining the survey points / coordinates in three different ways. They should only be defined once.



Any suggestions on structure are appreciated.










share|improve this question
















The arrays xcoordinates and ycoordinates define points on a grid. I'm simply trying to connect all of the points sequentially with short line segments.



documentclass[12pt]{article}
usepackage{tikz}
usepackage[active,tightpage]{preview}%
PreviewEnvironment{tikzpicture}%
setlengthPreviewBorder{0.1cm}%

usetikzlibrary{positioning}%
usetikzlibrary{shapes}%
usetikzlibrary{arrows}%
usetikzlibrary{calc}%

begin{document}

defthexslant{-1}
deftheyslant{0.5}

newlengthmarkeroffset
setlengthmarkeroffset{0.20cm}

% particle diameter
defmarkerdiameter{0.2cm}


begin{tikzpicture}[scale=.9,every node/.style={minimum size=1cm},on grid]

begin{scope}[
yshift=0cm,every node/.append style={
yslant=theyslant,xslant=thexslant},yslant=theyslant,xslant=thexslant]
fill[white,fill opacity=.9] (0,0) rectangle (5,5);
draw[black,very thick] (0,0) rectangle (5,5);
draw[step=10mm, black] (0,0) grid (5,5);

foreach i / j in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}{%
fill[black] (i - 0.5, j - 0.5) circle (markerdiameter/2);%
node at (i - 0.5, j - 0.5) [blue,xshift=markeroffset, yshift=markeroffset,font=tiny] () {$i,j$};
}%

% draw a red line indicating the path of the survey

defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

% ****** uncomment this to reveal problem
%foreach i in {1,...,15}{
%coordinate (A) at (pgfmathparse{xcoordinates[i]} pgfmathresult, pgfmathparse{ycoordinates[i]} pgfmathresult)-(0.5,0.5);
%coordinate (B) at (pgfmathparse{xcoordinates[i+1]} pgfmathresult, pgfmathparse{ycoordinates[i+1]} pgfmathresult)-(0.5,0.5);
%draw[gray] (B) -- (3,3);
%}

end{scope}

end{tikzpicture}

end{document}


I'm not confident that one can do xcoordinates[i+1]. When I compile this, I get the error:



! Incomplete iffalse; all text was ignored after line 83.


The same mwe with more functionality looks like:



documentclass[12pt]{article}
usepackage{tikz}
usepackage[active,tightpage]{preview}%
PreviewEnvironment{tikzpicture}%
setlengthPreviewBorder{0.1cm}%

usetikzlibrary{positioning}%
usetikzlibrary{shapes}%
usetikzlibrary{arrows}%
usetikzlibrary{calc}%

begin{document}

defthexslant{-1}
deftheyslant{0.5}

newlengthmarkeroffset
setlengthmarkeroffset{0.20cm}

% particle diameter
defmarkerdiameter{0.2cm}


begin{tikzpicture}[scale=.9,every node/.style={minimum size=1cm},on grid]

begin{scope}[
yshift=0cm,every node/.append style={
yslant=theyslant,xslant=thexslant},yslant=theyslant,xslant=thexslant]
fill[white,fill opacity=.9] (0,0) rectangle (5,5);
draw[black,very thick] (0,0) rectangle (5,5);
draw[step=10mm, black] (0,0) grid (5,5);

foreach i / j in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}{%
fill[black] (i - 0.5, j - 0.5) circle (markerdiameter/2);%
node at (i - 0.5, j - 0.5) [blue,xshift=markeroffset, yshift=markeroffset,font=tiny] () {$i,j$};
}%

% draw a red line indicating the path of the survey

defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

% suggestion by marmot
draw[red, very thick] plot[variable=x,samples at={0,...,16}] ({xcoordinates[x]-0.5},{ycoordinates[x]-0.5});

% from the first point to every other point
coordinate (A) at (0.5,0.5);
foreach position in {(1,2), (1,3), (1,4), (1,5), (2,5), (3,5), (3,4), (3,3), (3,2), (3,1), (4,1), (5,1), (5,2), (5,3), (5,4), (5,5)}{
draw[gray] position+(-0.5,-0.5) -- (A);
}%

% and here is how we draw all the possible connections
foreach xa / ya [count=i] in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}
foreach xb / yb [count=j] in {1/1, 1/2, 1/3, 1/4, 1/5, 2/5, 3/5, 3/4, 3/3, 3/2, 3/1, 4/1, 5/1, 5/2, 5/3, 5/4, 5/5}
{
ifnum i=j
else
draw[gray] (xa-0.5,ya-0.5) -- (xb-0.5,yb-0.5);
fi
}%

end{scope}

end{tikzpicture}

end{document}


This is really quite ugly (done quickly, learning on the fly) because I'm defining the survey points / coordinates in three different ways. They should only be defined once.



Any suggestions on structure are appreciated.







tikz-pgf foreach






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 22 at 13:42







John Chris

















asked Jan 22 at 3:44









John ChrisJohn Chris

381313




381313








  • 4





    please extend your code snippet to complete small document, which reproduce your problem.

    – Zarko
    Jan 22 at 3:49






  • 2





    You cannot subtract coordinates like thisL: (pgfmathparse{xcoordinates[i]} pgfmathresult, pgfmathparse{ycoordinates[i]} pgfmathresult)-(0.5,0.5). You need either to do this with calc or to subtract the components.

    – marmot
    Jan 22 at 4:00











  • Updated question to include mwe.

    – John Chris
    Jan 22 at 13:36











  • Also added another mwe illustrating a problem of my defining the input coordinates multiple times, and in multiple ways.

    – John Chris
    Jan 22 at 13:45














  • 4





    please extend your code snippet to complete small document, which reproduce your problem.

    – Zarko
    Jan 22 at 3:49






  • 2





    You cannot subtract coordinates like thisL: (pgfmathparse{xcoordinates[i]} pgfmathresult, pgfmathparse{ycoordinates[i]} pgfmathresult)-(0.5,0.5). You need either to do this with calc or to subtract the components.

    – marmot
    Jan 22 at 4:00











  • Updated question to include mwe.

    – John Chris
    Jan 22 at 13:36











  • Also added another mwe illustrating a problem of my defining the input coordinates multiple times, and in multiple ways.

    – John Chris
    Jan 22 at 13:45








4




4





please extend your code snippet to complete small document, which reproduce your problem.

– Zarko
Jan 22 at 3:49





please extend your code snippet to complete small document, which reproduce your problem.

– Zarko
Jan 22 at 3:49




2




2





You cannot subtract coordinates like thisL: (pgfmathparse{xcoordinates[i]} pgfmathresult, pgfmathparse{ycoordinates[i]} pgfmathresult)-(0.5,0.5). You need either to do this with calc or to subtract the components.

– marmot
Jan 22 at 4:00





You cannot subtract coordinates like thisL: (pgfmathparse{xcoordinates[i]} pgfmathresult, pgfmathparse{ycoordinates[i]} pgfmathresult)-(0.5,0.5). You need either to do this with calc or to subtract the components.

– marmot
Jan 22 at 4:00













Updated question to include mwe.

– John Chris
Jan 22 at 13:36





Updated question to include mwe.

– John Chris
Jan 22 at 13:36













Also added another mwe illustrating a problem of my defining the input coordinates multiple times, and in multiple ways.

– John Chris
Jan 22 at 13:45





Also added another mwe illustrating a problem of my defining the input coordinates multiple times, and in multiple ways.

– John Chris
Jan 22 at 13:45










1 Answer
1






active

oldest

votes


















3














You do not need any of the pgfmathparse thingies for that. (And you cannot subtract coordinates in the way you seem to intend.)



documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

foreach i [evaluate=i as j using {int(i+1)}] in {1,...,15} {
draw[gray] ({xcoordinates[i]-0.5},{ycoordinates[i]-0.5}) -- ({xcoordinates[j]-0.5},{ycoordinates[j]-0.5});
}
end{tikzpicture}
end{document}


enter image description here



If you really insist on retaining your original syntax to a large extent, you could use calc. And then I would like to argue that, if you create named coordinates, making them unique may help.



documentclass[tikz,border=3.14mm]{standalone}
usetikzlibrary{calc}
begin{document}
begin{tikzpicture}
defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

foreach i in {1,...,15} {
coordinate (A-i) at ($({xcoordinates[i]},{ycoordinates[i]} )-(0.5,0.5)$);
coordinate (B-i) at ($({xcoordinates[i+1]} ,{ycoordinates[i+1]})-(0.5,0.5)$);
draw[gray] (A-i) -- (B-i);
}
end{tikzpicture}
end{document}


enter image description here



Of course, you can get (almost) the same result much simpler (still using your lists).



documentclass[tikz,border=3.14mm]{standalone}
begin{document}
begin{tikzpicture}
defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

draw[gray] plot[variable=x,samples at={1,...,16}] ({xcoordinates[x]-0.5},{ycoordinates[x]-0.5});
end{tikzpicture}
end{document}


Apart from being shorter, this variant has the advantage that the line joins look good.
enter image description here






share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "85"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471226%2findexing-arrays-in-a-foreach-in-tikz%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You do not need any of the pgfmathparse thingies for that. (And you cannot subtract coordinates in the way you seem to intend.)



    documentclass[tikz,border=3.14mm]{standalone}
    begin{document}
    begin{tikzpicture}
    defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
    defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

    foreach i [evaluate=i as j using {int(i+1)}] in {1,...,15} {
    draw[gray] ({xcoordinates[i]-0.5},{ycoordinates[i]-0.5}) -- ({xcoordinates[j]-0.5},{ycoordinates[j]-0.5});
    }
    end{tikzpicture}
    end{document}


    enter image description here



    If you really insist on retaining your original syntax to a large extent, you could use calc. And then I would like to argue that, if you create named coordinates, making them unique may help.



    documentclass[tikz,border=3.14mm]{standalone}
    usetikzlibrary{calc}
    begin{document}
    begin{tikzpicture}
    defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
    defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

    foreach i in {1,...,15} {
    coordinate (A-i) at ($({xcoordinates[i]},{ycoordinates[i]} )-(0.5,0.5)$);
    coordinate (B-i) at ($({xcoordinates[i+1]} ,{ycoordinates[i+1]})-(0.5,0.5)$);
    draw[gray] (A-i) -- (B-i);
    }
    end{tikzpicture}
    end{document}


    enter image description here



    Of course, you can get (almost) the same result much simpler (still using your lists).



    documentclass[tikz,border=3.14mm]{standalone}
    begin{document}
    begin{tikzpicture}
    defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
    defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

    draw[gray] plot[variable=x,samples at={1,...,16}] ({xcoordinates[x]-0.5},{ycoordinates[x]-0.5});
    end{tikzpicture}
    end{document}


    Apart from being shorter, this variant has the advantage that the line joins look good.
    enter image description here






    share|improve this answer






























      3














      You do not need any of the pgfmathparse thingies for that. (And you cannot subtract coordinates in the way you seem to intend.)



      documentclass[tikz,border=3.14mm]{standalone}
      begin{document}
      begin{tikzpicture}
      defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
      defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

      foreach i [evaluate=i as j using {int(i+1)}] in {1,...,15} {
      draw[gray] ({xcoordinates[i]-0.5},{ycoordinates[i]-0.5}) -- ({xcoordinates[j]-0.5},{ycoordinates[j]-0.5});
      }
      end{tikzpicture}
      end{document}


      enter image description here



      If you really insist on retaining your original syntax to a large extent, you could use calc. And then I would like to argue that, if you create named coordinates, making them unique may help.



      documentclass[tikz,border=3.14mm]{standalone}
      usetikzlibrary{calc}
      begin{document}
      begin{tikzpicture}
      defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
      defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

      foreach i in {1,...,15} {
      coordinate (A-i) at ($({xcoordinates[i]},{ycoordinates[i]} )-(0.5,0.5)$);
      coordinate (B-i) at ($({xcoordinates[i+1]} ,{ycoordinates[i+1]})-(0.5,0.5)$);
      draw[gray] (A-i) -- (B-i);
      }
      end{tikzpicture}
      end{document}


      enter image description here



      Of course, you can get (almost) the same result much simpler (still using your lists).



      documentclass[tikz,border=3.14mm]{standalone}
      begin{document}
      begin{tikzpicture}
      defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
      defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

      draw[gray] plot[variable=x,samples at={1,...,16}] ({xcoordinates[x]-0.5},{ycoordinates[x]-0.5});
      end{tikzpicture}
      end{document}


      Apart from being shorter, this variant has the advantage that the line joins look good.
      enter image description here






      share|improve this answer




























        3












        3








        3







        You do not need any of the pgfmathparse thingies for that. (And you cannot subtract coordinates in the way you seem to intend.)



        documentclass[tikz,border=3.14mm]{standalone}
        begin{document}
        begin{tikzpicture}
        defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
        defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

        foreach i [evaluate=i as j using {int(i+1)}] in {1,...,15} {
        draw[gray] ({xcoordinates[i]-0.5},{ycoordinates[i]-0.5}) -- ({xcoordinates[j]-0.5},{ycoordinates[j]-0.5});
        }
        end{tikzpicture}
        end{document}


        enter image description here



        If you really insist on retaining your original syntax to a large extent, you could use calc. And then I would like to argue that, if you create named coordinates, making them unique may help.



        documentclass[tikz,border=3.14mm]{standalone}
        usetikzlibrary{calc}
        begin{document}
        begin{tikzpicture}
        defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
        defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

        foreach i in {1,...,15} {
        coordinate (A-i) at ($({xcoordinates[i]},{ycoordinates[i]} )-(0.5,0.5)$);
        coordinate (B-i) at ($({xcoordinates[i+1]} ,{ycoordinates[i+1]})-(0.5,0.5)$);
        draw[gray] (A-i) -- (B-i);
        }
        end{tikzpicture}
        end{document}


        enter image description here



        Of course, you can get (almost) the same result much simpler (still using your lists).



        documentclass[tikz,border=3.14mm]{standalone}
        begin{document}
        begin{tikzpicture}
        defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
        defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

        draw[gray] plot[variable=x,samples at={1,...,16}] ({xcoordinates[x]-0.5},{ycoordinates[x]-0.5});
        end{tikzpicture}
        end{document}


        Apart from being shorter, this variant has the advantage that the line joins look good.
        enter image description here






        share|improve this answer















        You do not need any of the pgfmathparse thingies for that. (And you cannot subtract coordinates in the way you seem to intend.)



        documentclass[tikz,border=3.14mm]{standalone}
        begin{document}
        begin{tikzpicture}
        defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
        defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

        foreach i [evaluate=i as j using {int(i+1)}] in {1,...,15} {
        draw[gray] ({xcoordinates[i]-0.5},{ycoordinates[i]-0.5}) -- ({xcoordinates[j]-0.5},{ycoordinates[j]-0.5});
        }
        end{tikzpicture}
        end{document}


        enter image description here



        If you really insist on retaining your original syntax to a large extent, you could use calc. And then I would like to argue that, if you create named coordinates, making them unique may help.



        documentclass[tikz,border=3.14mm]{standalone}
        usetikzlibrary{calc}
        begin{document}
        begin{tikzpicture}
        defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
        defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

        foreach i in {1,...,15} {
        coordinate (A-i) at ($({xcoordinates[i]},{ycoordinates[i]} )-(0.5,0.5)$);
        coordinate (B-i) at ($({xcoordinates[i+1]} ,{ycoordinates[i+1]})-(0.5,0.5)$);
        draw[gray] (A-i) -- (B-i);
        }
        end{tikzpicture}
        end{document}


        enter image description here



        Of course, you can get (almost) the same result much simpler (still using your lists).



        documentclass[tikz,border=3.14mm]{standalone}
        begin{document}
        begin{tikzpicture}
        defxcoordinates{{1,1,1,1,1,2,3,3,3,3,3,4,5,5,5,5,5}}
        defycoordinates{{1,2,3,4,5,5,5,4,3,2,1,1,1,2,3,4,5}}

        draw[gray] plot[variable=x,samples at={1,...,16}] ({xcoordinates[x]-0.5},{ycoordinates[x]-0.5});
        end{tikzpicture}
        end{document}


        Apart from being shorter, this variant has the advantage that the line joins look good.
        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 22 at 5:30

























        answered Jan 22 at 4:04









        marmotmarmot

        95.5k4110210




        95.5k4110210






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f471226%2findexing-arrays-in-a-foreach-in-tikz%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?