Graphics, Figures & TablesComplicated 'foreach'

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

Complicated 'foreach'

Post by usr345 »

Hello. I am trying to build the diagram, describing the Perl Moose Attributes. The diagram contains a matrix with nodes, containing the name of the attribute and a code snippet.
tikZ10.png
tikZ10.png (14.34 KiB) Viewed 1249 times
The questions:

1. How to organize a loop to draw the lines automatically? Cause the pattern of node ids is repeated. C syntax:

Code: Select all

for(i = 1, i < 3; i++)
{
  \draw[line] (i.''_1''.west) --++ (-5mm, 0) |-
}
//the last node is nasty - I can't use |- with it
(3_1.west);
2. Is it possible to create a loop to draw the nodes in matrix, just taking there content structure?

The code for the diagram:

Code: Select all

\documentclass{article}
\usepackage[a3paper]{geometry}
\usepackage[cm]{fullpage}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,backgrounds,calc}
\begin{document}
\begin{tikzpicture}
%{{{ tikz styles
  \tikzset{
    header/.style={ellipse, draw=black, top color=white, bottom color=yellow!50,thick, inner sep=3mm, minimum size=3em, text centered, double, anchor=west},
    pointer/.style = {signal, signal to=east, anchor=west},
    mnemonic-block/.style={rectangle, anchor=west, inner sep=3mm},
    line/.style = {draw},
    comment/.style = {rectangle, rounded corners, draw, inner sep=3mm, anchor=west},
  }
%}}}
\matrix[nodes={draw}, row  sep=5mm,
    column sep=2mm,minimum width=.875cm, minimum height=1cm]
{
\node[header] (1_1) {Moose Attributes};\\
%{{{read-write / read only
\node[pointer] (2_1) {read-write / read only}; &
\node[mnemonic-block] (2_2) {
\begin{lstlisting}[language=Perl]
has 'attr' => {is => 'rw'}
has 'attr' => {is => 'ro'}
\end{lstlisting}
}; \\
%}}}
%{{{accessor methods
\node[pointer] (3_1) {accessor methods}; &
\node[mnemonic-block] (3_2) {
\begin{lstlisting}[language=Perl]
has 'attr' => {reader => 'get_attr',}
has 'attr' => {writer => 'set_attr',}
\end{lstlisting}
}; \\
};
\draw[line] (1_1.west) --++ (-5mm, 0) |- (2_1.west) --++ (-5mm, 0) |- (3_1.west);
\draw[line] (2_1.east) -- (2_2.west);
\draw[line] (3_1.east) -- (3_2.west);

% \draw[line] (1.east) -- (18.west);
\end{tikzpicture}
\end{document}
Last edited by localghost on Sun Jun 12, 2011 4:37 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

Post Reply