# Nested ifthenelse in tikz fails after arbitrary changes

Attempting to draw a random polymer mesh, which I intended to visualize by drawing a series of random walks between (x,0) and (x,1) where x is arbitrary. My first attempt is attached, and it compiles. But if I change certain things it gives the error message "Package tikz Error: Giving up on this path. Did you forget a semicolon? }}" Given this I have not progressed beyond this, my first attempt.

By "certain things" I mean for example removing "nicefrac" or sometimes arbitrarily altering the \foreach statements. Or even if I change the starting coordinates for coordinate A to for example (rand,rnd).

The code below is then clearly not ideal. The lines, if not drawn as ultra thin, have ugly gaps at each joint, there is the error message I do not understand where comes from. So, how can I draw a messy mesh in a better fashion?

The rules are simple: Each polymer chain in the mesh is drawn as a set of jointed segments of equal length. The joints between these should be able to deviate only by a certain angle (Depending on the polymer stiffness) and they should all remain within a given distance of the drawn surface. If they reach this limit, the polymers will bend back down or up, as the case may be.

\documentclass{article}
\usepackage{tikz}
\usepackage{nicefrac}

\usepackage{pgfplots}
\usetikzlibrary{calc}
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord};}%

\begin{document}
\begin{tikzpicture}
\clip (-.5,-.5)rectangle(.5,2);

\foreach \x in {-1.0,-.9,...,1}{
\draw[ultra thin] (\x,0.1) coordinate[name=A];
\foreach \y in {0,.2,...,10}{

\ExtractCoordinate{A}
\ifthenelse{\lengthtest{\YCoord <28 pt}}%Checked if below 1.
{%Is below 1.
\ifthenelse{\lengthtest{\YCoord >0 pt}}{%Check if above 0
\draw[ultra thin] (A)--++(rand*120:.05) coordinate[name=A];%Yes, above zero
}{\draw[ultra thin] (A)--++(rnd*89:.05) coordinate[name=A];}%NOT above zero
}%Is not below 1
{\draw[ultra thin] (A)--++(-rnd*89:.05) coordinate=name=A];
}%ELSE 1
}}
\draw[fill=blue!15] (-1,0)rectangle(1,-1);%surface coated by the mesh
\end{tikzpicture}
\end{document}


The code results in something akin to this:
or this, with normal line thickness:

which is fairly close to what I would like, though I would like to tweak the behavior of the angles somewhat so I can change how far each polymer segment can bend (Thereby simulating different polymer stiffnesses). Also, at this point, all polymers tend to move to the right, their motion is not truly random but directed in one direction.

• If you remove nicefrac, you are removing the ifthen package (which is loaded by nicefrac) so \ifthenelse is not defined anymore. In fact, you don’t need \ifthenelse since PGFmath provides that for you. Can you show us how that mesh looks usually? What are the rules? – Qrrbrbirlbel Nov 28 '13 at 10:17

I don’t know about the clipped path or the rectangle at the bottom. But you can easily draw a randomized line on one path.

I am using the coordinate c-0 and c-1 as a reference for the rand factor instead of using the pt units this calculation will be based on the Coordinate System by PGF/TikZ.

## Code

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[
line join=round, line cap=round,
inner sep=+1pt, near end, nodes={fill=white}]
\path[very thin] coordinate (c-0) at (-1,0) (c-0) edge node {0} ++(right:5)
coordinate (c-1) at (-1,1) (c-1) edge node {1} ++(right:5);
\foreach \x[evaluate={\col=((\x+1)/2)*100}] in {-1.0, -.9, ..., 1}
\draw[color=blue!\col!red] (\x, .5) \foreach \cnt in {1,...,50}{
coordinate (@) let \p0=($(@)-(c-1)$),
\p1=($(@)-(c-0)$) in
-- ++ ({rand*ifthenelse(and(\x0>0,\x1<0),120,89)}:.05)};
\end{tikzpicture}
\end{document}