# How can I automatically evaluate and align?

I am trying to write myself a TikZ but I am having trouble. This is what I am looking for: This is what I have been able to get: And finally this is the code I have written:

\begin{figure}[ht]
\centering
\begin{tikzpicture}[scale=10]
\coordinate (C0) at ($(0,0)$) {};
\coordinate (C1) at ($(1,0)$) {};
\draw[arrows=->] (C0) -- (C1);
\foreach \x in {2,3,4,5,6}
{
\coordinate (A\x) at ($(1/\x,0)$) {};
\draw ($(A\x)+(0,0.01)$) -- ($(A\x)-(0,0.01)$);
\node [below] at ($(A\x)+(0,-0.003)$) {$\frac{1}{\x}$};
}
\foreach \x [evaluate=\x as \xeval using \x-1] in {3,4,5,6}
{
\coordinate (B\x) at ($({(\x-1)/\x},0)$) {};
\draw ($(B\x)+(0,0.01)$) -- ($(B\x)-(0,0.01)$);
%\node [below] at ($(B\x)+(0,-0.003)$) {\frac{\xeval}{\x}}; %ERROR!
}
\node [below] at ($(1/12,-0.023)$) {$\ldots$};
\node [below] at ($(11/12,-0.023)$) {$\ldots$};
\draw ($(0,0.01)$) -- ($(0,-0.01)$);
\node [below] at ($(0,-0.011)$) {$0$};
\draw ($(1,0.01)$) -- ($(1,-0.01)$);
\node [below] at ($(1,-0.011)$) {$1$};
\end{tikzpicture}
\label{fig:dinamica F(x)}
\end{figure}

So what I ask you is:

1. how to write automatically \frac{2}{3}, \frac{3}{4} etc. using evaluation?
2. how can I write the curved arrows automatically too?

It is about 2 hours that I am trying to doing it.

Another thing: in order to vertically align the labels 0, \ldots, \frac{1}{2} I had to try manually a lot of coordinates until I reached a right combination. Is there any way to doing it automatically? Thanks!

• Tikz passes pretty much everything through \pgfmathparse, so 1/12 should be converted automatically. The $..$ just invokes calc, which in this case does nothing. As for the arcs, use to[...] with angles and node[midway,above] to label them. – John Kormylo Mar 17 '16 at 16:35
• if you want to draw the arcs automatically, what is the algorithm/logic used? for example the arc between 4/5 and 5/6 goes to space between 1/5 and 1/4. And arcs are labeled in pairs ? what is the logic behind that pairing? – AJN Mar 18 '16 at 11:48

It is not very clear to me, what you mind with "automatic evaluate". I only try to reproduce you hand sketch (for exercise ...). With

\documentclass[tikz,
border=3mm]{standalone}
\usetikzlibrary{calc,positioning,quotes}

\begin{document}
\begin{tikzpicture}[
every edge quotes/.style = {font=\footnotesize, auto},
L/.style = {->, shorten >=1mm, shorten <=1mm},
LA/.style = {out= 75, in=105, looseness=#1},
LB/.style = {out=255, in=285, looseness=#1}
]
\draw (0,0) -- (10,0);

\foreach \x/\y [count = \xx] in
{0/1, 1/6, 1/5, 1/4, 1/3, 1/2, 2/3, 3/4, 4/5, 5/6, 1/1}
{
\ifnum\y=1
\coordinate[label={[yshift=-1mm]below:$\x$}] (n\xx) at (10*\x/\y,0)
\else
\coordinate[label={[yshift=-1mm]below:$\frac{\x}{\y}$}] (n\xx) at (10*\x/\y,0)
\fi;
% draw ticks
\draw (10*\x/\y,0.1) -- + (0,-0.2);
}
% dots
\node [below=7pt] at ($(n1)!0.5!(n2)$) {$\dots$};
\node [below=7pt] at ($(n10)!0.5!(n11)$) {$\dots$};
% loops above
\draw[L]   ($(n2)!0.5!(n3)$) to [LA=2,"4"] ($(n3)!0.5!(n4)$);
\draw[L]   ($(n3)!0.5!(n4)$) to [LA=2,"3"] ($(n4)!0.5!(n5)$);
\draw[L]   ($(n4)!0.5!(n5)$) to [LA=2,"2"] ($(n5)!0.5!(n6)$);

\node (A) [above=17mm of n6,inner sep=0pt]  {A};
\draw[L]   ($(n5)!0.6!(n6)$) to [bend  left,"1"]    (A);
\draw[L]   ($(n6)!0.4!(n7)$) to [bend right,"1" ']  (A);
% loops below
\draw[L]   ($(n8)!0.5!(n9)$) to [LB=1.5,"4" '] ($(n3)!0.5!(n4)$);
\draw[L]   ($(n7)!0.5!(n8)$) to [LB=1.5,"3" '] ($(n4)!0.5!(n5)$);
\draw[L]   ($(n6)!0.5!(n7)$) to [LB=2.5,"2" '] ($(n5)!0.5!(n6)$);
\end{tikzpicture}
\end{document}

I obtain:

Heres my attempt at drawing the number line. I do not know the algorithm/logic for drawing the arcs automatically. The arcs have to start at the mid point of the tick marks and end between midpoints of other tickmarks (this is all i have understood form the picture). In this code the tick labels and ellipses are semi automatically placed and labeled. The 0 and 1 are undesirable labeled as 0/1 and 1/1 as a side effect.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}

% the unit interval is magnified by 10 times
\draw (0,0) -- (10,0);

\foreach \x/\y [count = \cc] in
{0/1, 1/6, 1/5, 1/4, 1/2, 4/5, 5/6, 1/1}
{
\coordinate (nn\cc) at(10*\x / \y, 0);
\node [below of= nn\cc,
node distance = 0.5cm]
{$\frac{\x}{\y}$};

% comment below node. it is not required
\node [above of= nn\cc,
node distance = 0.5cm,
rotate=45]
{$nn\cc$};

% draw ticks
\draw (10*\x / \y, -0.1) --
(10*\x / \y,  0.1);
}
%Find midpoint of first two and last two nodes
\draw (nn1) -- coordinate [pos=0.5](firstdot) (nn2);
\draw (nn7) -- coordinate [pos=0.5](lasttdot) (nn8);
% draw ellipsis at the midpoint shifted by 0.5cm
\node [below of= firstdot, node distance = 0.5cm]
{$\cdots$};
\node [below of= lasttdot, node distance = 0.5cm]
{$\cdots$};
\end{tikzpicture}
\end{document}