In my question The Spiral of Roots in TikZ, the answer by Mark Wibrow caught my attention but while trying to edit the code I found myself struggling with an ifelse statement. What I am trying to achieve is just to put a 1 instead of the square root of one in the base of the first right triangle. I tried saying
if \n = 1 then {1} else {$\sqrt{\n}$}
but it does not work. I don't clearly understand the math library (page 652 of the pgfmanual) and how it works. I am pretty sure that where I have the ifelse statement is wrong; probably more outside of the declaration. I even tried
\p = (\n == \N && \N > 1) ? "$\sqrt{\n}$" : "1";
but no good. Any insight as to how to achieve the desired result.
\newcommand\sqrtspiral[2][]{%
\tikz[line cap=round, x=2cm,y=2cm, line join=round,#1]{%
\tikzmath{%
int \n;
\b = 0; \d = 1; \N = #2;
for \n in {1,...,\N}{
\l = (\n == \N && \N > 1) ? "red" : "black";
\e = (\n == 1) ? " -- cycle" : "";
{
\path [rotate=\b, draw=\l] (0,0) -- (\d,1) -- (\d,0)
node [\l, midway, anchor=\b+180] {1}
\e (\d/2, 0) node [fill=white] {if \n = 1 then {1} else {$\sqrt{\n}$}}; % edit here
\path [draw=\l, rotate=\b] (\d-.1,0) |- ++(.1,.1);
};
\d = sqrt(1+(\d)^2); \b = \b + asin(1/\d);
};
{
\path [rotate=\b, \l] (\d/2, 0) node [fill=white] {$\sqrt x$};
};
}}}
Here is the link to the answer: link
A MWE follows (of course merely one):
\documentclass[tikz,border=0.125cm]{standalone}
\usetikzlibrary{math}
\begin{document}
\newcommand\sqrtspiral[2][]{%
\tikz[line cap=round, x=2cm,y=2cm, line join=round,#1]{%
\tikzmath{%
int \n;
\b = 0; \d = 1; \N = #2;
for \n in {1,...,\N}{
\l = (\n == \N && \N > 1) ? "red" : "black";
\e = (\n == 1) ? " -- cycle" : "";
{
\path [rotate=\b, draw=\l] (0,0) -- (\d,1) -- (\d,0)
node [\l, midway, anchor=\b+180] {1}
\e (\d/2, 0) node [fill=white] {%
if \n = 1 then {1} else {$\sqrt{\n}$} %edit here
};
\path [draw=\l, rotate=\b] (\d-.1,0) |- ++(.1,.1);
};
\d = sqrt(1+(\d)^2); \b = \b + asin(1/\d);
};
{
\path [rotate=\b, \l] (\d/2, 0) node [fill=white] {$\sqrt x$};
};
}}}
\sqrtspiral{10}
\end{document}

1and2or even1and\nbut it doesn't work when\sqrtis used. – Paul Gessler Jan 11 '15 at 23:49