# Is this possible in Tikz?

I am wondering if it is possible to quickly create something like this in Tikz. In particular, I am concerned with making the zig-zag line, which has these properties:

• Each star is on a clearly identifiable horizontal "level"
• Each star is exactly between two of the characters (alphas and betas) along the top

Having the zig-zag line line up with the tree on the right would be a plus too!

• What is for You "quickly create"? – osjerick Dec 18 '13 at 2:30
• What do you have so far? If the zig-zag line is the problem, how are you creating the rest of the graphic? – cfr Dec 18 '13 at 2:37
• It might be easier to move the alphas and betas around to match the stars. – John Kormylo Dec 18 '13 at 2:43
• @cfr I was thinking of using absolute positioning, but wanted to know if there was an easier way. I guess one different way to think of it is that there is a grid (similar to the "array" environment in LaTeX) on which the stars sit. – angryavian Dec 18 '13 at 2:44
• @OSjerick I guess "quickly" was not the right word, sorry! I am looking for a natural way to make a zig-zag line with the above properties. Please see my above comment for my other thoughts. – angryavian Dec 18 '13 at 2:49

There are two problems in this drawing. First the zig-zag line, second the top formula which has special alignment requirements.

The following code suggests solutions for these problems. There is a short explanation:

1. First, each position for each asterisk is defined as a named coordinate, with names n0, n1, n2, and so on, but nothing is drawn yet. These names are useful to later annotate the figure or to add more auxiliar lines. Their positions are specified by a string of 1 and -1, which refers to the vertical position relative to the previous one.

2. The zig-zag line is drawn across the sequence (n0)--(n1)--(n2)-- etc

3. Some auxiliar horizontal lines are drawn between some pairs of n\i/n\j.

4. Stars. For each \i in 0..19 a star is drawn at (n\i).

5. Finally, and here is where syntax gets ugly, the formula is drawn at top. To draw the formula I split it in "fragments" so that each fragment is drawn at coordinates which are multiple of 0.5.

For example, the first fragment is \beta, and this is drawn at x=0.5, which is the middle point between n0 and n1. The second fragment is the left bracket (\bigg[) which is drawn at x=1, which is at the vertical of n1. The third fragment is a, drawn at x=1.5, which is at the middle of n1 and n2, and so on.

Those fragments are specified manually, separated by commas.

This is the code:

\documentclass{article}
\usepackage{tikz}
\usepackage{inputenc}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\tikzset{
star/.style = {
fill=white, circle,
inner sep=0pt,
}
}

\begin{document}
\begin{tikzpicture}[x=6mm, y=6mm]

% 1. define n0, n1, n2,  ... n19
\coordinate (n0) at (0,0);
\coordinate (prev) at (n0);
\foreach [count=\i] \d in {1,-1,-1,-1,1,-1,-1,-1,1,1,-1,-1,1,1,1,1,-1,1,1} {
\coordinate (n\i) at ($(prev)+(1,\d)$);
\coordinate (prev) at (n\i);
};

% 2. Draw zig-zag
\draw[thick] (n0) \foreach \n in {1,...,19} { -- (n\n) } ;

% 3. Draw some horizontal lines
\foreach \a/\b in {2/18,3/15,6/14,7/9,11/13} {
\draw[thin, gray, dashed] (n\a) -- (n\b);
};

% 4. Draw stars
\foreach \n in {0,...,19} {
\node[star] at (n\n) {$\star$};
};

% 5. Draw formula
\def\L{(}  % Parenthesis cannot be used as part of \foreach
\def\R{)}  % so I had to define here a macro for them
\foreach [count=\i] \txt in {\beta,\bigg[,
a,\Big(,a,\L,a,,\beta,\R\big(,
a,\L,a,,a,,\beta,,\beta,\R\L,
a,,a,,\beta,,\beta,\R,\beta,\big),
\beta,\Big)\L,a,,\beta,\R,\beta,\bigg]
} {
\node at (0.5*\i,2) {$\txt$};
};
\end{tikzpicture}
\end{document}


And this is the result:

• I never knew that Tikz had foreach loops! The flexibility there is amazing. Thanks for teaching me something new! – angryavian Dec 18 '13 at 15:28

You need to know at least the zig-zag's peaks coordinates.

Because the angles of zig-zag lines and level separation are the same, a simple way to draw the zig-zag with stars is by using decorations.

# Code

\documentclass[border=10pt,tikz]{standalone}
\usetikzlibrary{decorations.shapes,shapes}
\begin{document}
\begin{tikzpicture}[x=1cm, y=1.25cm]
\draw[thick,postaction={draw,decorate,decoration={shape backgrounds,shape=star,shape size=2mm,%
shape sep={1.6cm, between center}},fill=black, shape border rotate=20}]%
(0,0) -- (1,1) -- (4,-2) -- (5,-1) -- (8,-4) -- (10,-2) -- (12,-4) -- (16,0) -- (17,-1) -- (19.05,1.05);
\end{tikzpicture}
\end{document}


# Result

Note that the stars are trying to follow the slope of the line, hence they rotate. I do not know how do not rotate the stars (do not know if you can do that), perhaps using another shape it can be more useful.

I think that a manually drawing is not difficult or annoying. ;)

# Update

Just for general knowledge! :)

I did not know that prevent the slope rotation of the stars it's easy, also improve the star shape, with shape sloped=false and setting star point height.

\draw[thick,postaction={draw,decorate,decoration={shape backgrounds,shape=star,shape size=2mm,%
shape sep={1.6cm, between center},shape sloped=false},fill=black,star point height=30mm}]%
(0,0) -- (1,1) -- (4,-2) -- (5,-1) -- (8,-4) -- (10,-2) -- (12,-4) -- (16,0) -- (17,-1) -- (19.05,1.05);


• @blf You're Welcome! I wish grant a more useful solution, but I thought this was a simple way (for "quickly create"). :) – osjerick Dec 18 '13 at 15:39