Graphics, Figures & TablestikZ | Using Loops for creating Lines

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

tikZ | Using Loops for creating Lines

Post by usr345 »

I am having this code to connect the nodes in a flowchart:

Code: Select all

\path [line] (8-1.south) -- (8-2.north);
\path [line] (8-2.south) -- (8-3.north);
\path [line] (8-3.south) -- (8-4.north);
\path [line] (8-4.south) -- (8-5.north);
\path [line] (8-5.south) -- (8-6.north);
\path [line] (8-6.south) -- (8-7.north);
\path [line] (8-7.south) -- (8-8.north);
Is it possible to write something like this (I am using Perl-style for loop):

Code: Select all

for($i=1; $i< 8; ++$i)
{
    $j = $i + 1;
    print "\path [line] (8-$i.south) -- (8-$j.north);";
}
Last edited by usr345 on Tue Nov 15, 2011 12:55 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

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tikZ | Using Loops for creating Lines

Post by localghost »

Search the pgf/tikZ manual for the \foreach command.


Thorsten
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

tikZ | Using Loops for creating Lines

Post by usr345 »

1. How to add 1 to a var and save the result to the other var?
2. How to concat strings with vars?

I tried this, but it doesn't work.

Code: Select all

\foreach \x in {1,2,3,4,5,6,7}
{
  \def \y{\x};
  \y = \y + 1;
  \def \y1{8-\y.north};
  \def \x1{8-\x.south};

  \path [line] (\x1) -- (\y1);
}
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

tikZ | Using Loops for creating Lines

Post by usr345 »

Though, when I needed this stuff:

Code: Select all

\coordinate[above=of 5] (5-0);
\coordinate[above=of 6] (6-0);
\coordinate[above=of 7] (7-0);
\coordinate[above=of 8] (8-0);
\coordinate[above=of 9] (9-0);
This code worked:

Code: Select all

\foreach \x in {4,5,6,7,8,9}
{
  \def \y{\x-0};
  \coordinate[above=of \x] (\y);
}
So, perhaps, I need to know only how to add data to var and save it to the other var.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tikZ | Using Loops for creating Lines

Post by localghost »

It could be very helpful if you would post a minimal example instead of useless code snippets. But this should do what you want. At least it can serve as a basic structure.

Code: Select all

\documentclass{minimal}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
    \foreach \x in {1,2,3,4,5,6,7}{%
      \pgfmathadd{\x}{1}\pgfmathsetmacro{\y}{\pgfmathresult};
      \draw (\x,0) -- (\y,1);
    }
  \end{tikzpicture}
\end{document}
Details in the pgf/tikZmanual.
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

tikZ | Using Loops for creating Lines

Post by usr345 »

I did it. Here is the working code:

Code: Select all

\foreach \x in {1,2,3,4,5,6,7,8}
{
  \pgfmathint{\x + 1}
  \edef\y{\pgfmathresult}

  \path [line] (8-\x.south) -- (8-\y.north);
}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tikZ | Using Loops for creating Lines

Post by localghost »

I would do it entirely with the math engine.

Code: Select all

\foreach \x in {1,2,3,4,5,6,7,8}{%
  \pgfmathint{\x+1}\pgfsetmathmacro{\y}{\pgfmathresult};
  \path [line] (8-\x.south) -- (8-\y.north);
};
It can't do any harm if you share a complete solution.
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

tikZ | Using Loops for creating Lines

Post by usr345 »

localghost wrote:It can't do any harm if you share a complete solution.
What do you mean? You want me to post the entire diagram code?

Also I got another question. Is it possible to have a nested loop to draw the lines for all the nodes. As the code has a pattern. For example, we have 2 columns with different number of nodes in each.
tikZ-example.png
tikZ-example.png (5.93 KiB) Viewed 9805 times
Here is the explicit code, describing all the lines:

Code: Select all

\path [line] (1-1.south) -- (1-2.north);

\path [line] (2-1.south) -- (2-2.north);
\path [line] (2-2.south) -- (2-3.north);
I want to make a code, connecting all of them. In Perl it would be something like this:

Code: Select all

#1-st element - number of nodes in branch
#2-nd element - branch number
@data = ([2, 1] , [3, 2]);
foreach $element (@data)
{
       for($i = 1; $i < $element->[0]; ++$i)
       {
                $j = $i + 1;
                print "\path [line] ($element->[1]-$i.south) -- ($element->[1]-$j.north);";
       }
}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tikZ | Using Loops for creating Lines

Post by localghost »

usr345 wrote:What do you mean? You want me to post the entire diagram code? […]
At least a minimal version. At the moment there is no utilizable example here. And please upload attachments exclusively to the forum server. External links can expire and make a problem incomprehensible.
usr345 wrote:[…] Is it possible to have a nested loop to draw the lines for all the nodes. As the code has a pattern. For example, we have 2 columns with different number of nodes in each.
tikZ-example.png
Here is the explicit code, describing all the lines:

Code: Select all

\path [line] (1-1.south) -- (1-2.north);

\path [line] (2-1.south) -- (2-2.north);
\path [line] (2-2.south) -- (2-3.north);
[…]
As I already mentioned, help without a minimal example is difficult. At the moment you don't let us help you by posting only useless code snippets.
usr345 wrote:[…] I want to make a code, connecting all of them. In Perl it would be something like this:

Code: Select all

#1-st element - number of nodes in branch
#2-nd element - branch number
@data = ([2, 1] , [3, 2]);
foreach $element (@data)
{
       for($i = 1; $i < $element->[0]; ++$i)
       {
                $j = $i + 1;
                print "\path [line] ($element->[1]-$i.south) -- ($element->[1]-$j.north);";
       }
}
Currently my Perl knowledge is insufficient to help you in this regard.
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

tikZ | Using Loops for creating Lines

Post by usr345 »

Here is the minimal snippet, the idea is to automate the lines creation for all the branches with a nested loop and a data structure. This structure is an array of arrays.
1-st element - number of nodes in branch
2-nd element - branch number

Code: Select all

%{{{prerequisites

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage[cm]{fullpage}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{calc,shapes,arrows,positioning}
%}}}

\begin{document}
\begin{tikzpicture}
%{{{styles

\tikzset{
  line/.style = {draw},
  block/.style = {rectangle, draw, text width=2cm, text centered, minimum height=2em},
}

%}}}

%{{{branch 1
\node[block] (1-1) {
1-1
};
\node[block, below = of 1-1] (1-2) {
1-2
};
%}}}
%{{{branch 2

\node[block, right = of 1-1] (2-1) {
2-1
};
\node[block, below = of 2-1] (2-2) {
2-2
};
\node[block, below = of 2-2] (2-3) {
2-3
};

%}}}

%{{{lines

\path [line] (1-1) -- (1-2);

\path [line] (2-1) -- (2-2);
\path [line] (2-2) -- (2-3);

%}}}

\end{tikzpicture}

\end{document}
Post Reply