# Mathematical calculation within \foreach loop in tikz

The minimal code below demonstrates the problem. The code as it is does not give the desired output. I assume the \x-\y is not being correctly handled. I have tried including braces, brackets, but to no avail.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\connection}[]{\draw[{#1}] (#2#3)--([yshift={#4cm}]#2#3) --([yshift={#4cm}]#2#3-|#3#2)--(#2#3-|#3#2)}

\begin{document}

\begin{tikzpicture}

% create the coordinates
\foreach \Lletter/\l in {A/0,B/1,C/2}
{\foreach \Rletter/\r in {A/0,B/1,C/2}
{\coordinate (\Lletter\Rletter) at ({\l+\r/10},0);}
}

% create the paths
\foreach \L/\x in {A/5}
{\foreach \R/\y in {A/0,B/1,C/2}
{\connection{\L}{\R}{\x-\y};}
}

% this is the desired result
% \connection{A}{A}{5};
% \connection{A}{B}{4};
% \connection{A}{C}{3};

\end{tikzpicture}

\end{document}

• try \foreach \L/\x in {A/5} {\foreach \R/\y in {A/0,B/1,C/2} {\pgfmathsetmacro\result{\x-\y} \connection{\L}{\R}{\result};} } – touhami May 8 '16 at 17:56
• Just in case you do know it: "an empty line, in the body, is a paragraph break" -thus you want to avoid empty lines for markup. tex.stackexchange.com/questions/272926/… – hpekristiansen May 8 '16 at 22:55
• @Hans-PeterE.Kristiansen In a tikzpicture spaces and empty lines usually don't matter (exception before the environment's optional argument, see Q41567). A nullfont is installed. – Qrrbrbirlbel May 9 '16 at 12:13
• The problem is indeed the subtraction, as #4 gets replaced by \x-\y and thus: \x-\y cm becomes \x pt - \y cm. Use either (#4)*1cm or, staying in the local coordinate system: shift=(up:{#4}). – Qrrbrbirlbel May 9 '16 at 12:18

As touhami said, one way is to include an explicit call to \pgfmathsetmacro within the loop. However, since this is a loop, we might as well use the facilities provided by \foreach itself to evaluate variables using pgfmath expressions.

For example

\documentclass[tikz,border=10pt,multi]{standalone}
\newcommand{\connection}[]{%
\draw [{#1}] (#2#3) -- ([yshift={#4cm}]#2#3) -- ([yshift={#4cm}]#2#3 -| #3#2) -- (#2#3 -| #3#2)
}
\begin{document}
\begin{tikzpicture}
\foreach \Lletter/\l in {A/0,B/1,C/2}
{\foreach \Rletter/\r in {A/0,B/1,C/2}
{\coordinate (\Lletter\Rletter) at ({\l+\r/10},0);}
}
\foreach \L/\x in {A/5}
{\foreach \R/\y [evaluate=\y as \z using {\x-\y}] in {A/0,B/1,C/2}
{\connection{\L}{\R}{\z};}
}
\end{tikzpicture}
\end{document} • Might also be a good idea to introduce OP to count=\l from 0. – Qrrbrbirlbel May 9 '16 at 12:20
• @Qrrbrbirlbel I assumed those were just examples, to be honest. Perhaps you're right, though. – cfr May 9 '16 at 22:03
{"ft_lang_label":"__label__en","ft_lang_prob":0.6116038,"math_prob":0.9441299,"size":1337,"snap":"2020-10-2020-16","text_gpt3_token_len":497,"char_repetition_ratio":0.12228057,"word_repetition_ratio":0.0754717,"special_character_ratio":0.37696335,"punctuation_ratio":0.121323526,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9866366,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-27T02:44:44Z\",\"WARC-Record-ID\":\"<urn:uuid:9cbf3f98-43b1-40b6-b9ff-663b00b4bd5a>\",\"Content-Length\":\"148610\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f26070fc-58f4-4457-af1c-ee7f54b37c12>\",\"WARC-Concurrent-To\":\"<urn:uuid:2c95af7c-4649-440a-bf93-e44fba71daa3>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/308527/mathematical-calculation-within-foreach-loop-in-tikz\",\"WARC-Payload-Digest\":\"sha1:PCHTAK3WTSVKPXTOYEYOWZZBKPOCJDC7\",\"WARC-Block-Digest\":\"sha1:MW63DYL46ZAB3MMAAG2IUQPSQRTVJIGM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875146643.49_warc_CC-MAIN-20200227002351-20200227032351-00149.warc.gz\"}"}