TransWikia.com

Predefine variale for foreach loop

TeX - LaTeX Asked on April 18, 2021

I currently try to draw a tikz pic like in Figure 1. It basically is what I want and I could draw it like that for the whole Sequence, but it is very long. Since it is just the same task over and over again I thought I could just put (at least some) into a foreach loop.

Figure 2 was my first try, but as you can see the distances are not alike and not adjustable with [node distance=0 mm]. Also I think it will become intricate once the sequence makes a curve.

So I tried something else (Figure 3). This Idea I prefer, but unfortunately it does not work. I fails because the variable j is not defined when the foreach starts and tries to write a node under the node[draw,circle,below =of j]node. -> At least I think that this is the problem. So my question is, is it possibly to define a variablejfor theforeach` loop so she has a starting point?

Is there another approach that could solve the problem?

Left = Figure 1; middle = Figure 2; right = Figure 3

enter image description here enter image description here enter image description here

documentclass[tikz,border=3.5 mm]{standalone}
usetikzlibrary{backgrounds,positioning}

begin{document}

    
    begin{tikzpicture}[node distance=0 mm]
        
    %   Sequence = GGUCCCAUK
        
        How it should look like Figure 1
        node[draw,circle] at (0,0)  (G) {Large G};
        node[draw,circle,below=of G] (G) {Large G};
        node[draw,circle,below=of G] (U) {Large U};
        node[draw,circle,below=of U] (C) {Large C};
        node[draw,circle,below=of C] (C) {Large C};
        node[draw,circle,below=of C] (C) {Large C};
        node[draw,circle,below=of C] (A) {Large A};
        node[draw,circle,below left=of A] (U) {Large U};
        node[draw,circle,below left=of U] (K) {Large K};
    
        
    % Foreach part works but do not like distances (not easy adjustable) and will maybe make problems later
    %   foreach ij in {1/G,2/G,3/U,4/C,5/C,6/C,7/A}
    %   {   
    %       node[draw,circle] at (0,-i)(j){Largej};
    %
    %   }
    %       node[draw,circle,below left=of A] (U) {Large U};
    %       node[draw,circle,below left=of U] (K) {Large K};
            
            
    %   Fails because variable j is unknown in the beginning
    %   starter node
    %   node[draw,circle] at (0,0)  (G) {Large G};
    %
    %   foreach ij in {1/G,2/U,3/C,4/C,5/C,6/A}
    %   {
    %       i
    %   }
    
    % Whole Sequence: GGUCCCAUKGUGPAAU#GDDAGCACUCPGGABUNUGAAPCCAGCGAU??GAGPPCA”AUCUCGGUGGGACCUCCA
    end{tikzpicture}
    
end{document}

2 Answers

chains can be programmed to do pretty much anything in a single chain. You can use the placed key and change parameters on the fly. A very blunt implementation, which uses two parameters, the direction growth dir and the distance growth dist, which get changed using some ifnum.

documentclass[tikz,border=3mm]{standalone}
usetikzlibrary {chains}
begin{document}
begin{tikzpicture}[growth dir/.store in=tikzgrowthdir,
    growth dist/.store in=tikzgrowthdist,growth dir=-90,growth dist=0.1]
 begin{scope}[start chain=going {at=(tikzchainprevious.tikzgrowthdir),
    shift=(tikzgrowthdir:tikzgrowthdist),anchor=180+tikzgrowthdir},
    nodes={on chain,circle,draw}]
  foreach X [count=Y] in {G,G,U,C,C,C,A,U,K,G,U,G,P,A,A,U}   
  {ifnumnumexprY>8relax 
  tikzset{growth dir=-135}
  fi
  node {X};}
 end{scope} 
end{tikzpicture}
end{document}

enter image description here

There are many more variations possible.

Correct answer by user234180 on April 18, 2021

Like this?

enter image description here

With two loops and chains library:

documentclass[tikz,border=3.5 mm]{standalone}
usetikzlibrary{chains,
                positioning}

begin{document}
    begin{tikzpicture}[
node distance = 1mm,
  start chain = A going below,
  start chain = B going below left,
     C/.style = {circle, draw, minimum size=1.3em, inner sep=2pt}
                       ]
foreach i in {G,G,U,C,C,C}
    node[C, on chain=A] {i};

    node[C, on chain=B,
          below left=of A-6]    {A};
foreach i in {U,K}
    node[C, on chain=B]        {i};

     end{tikzpicture}
end{document}

Addendum: Inspired by interesting @Pumuckl answer and considering example from TikZ & PGF manual, page 605 (version 3.1.8b), is possible to draw above image in one loop:

documentclass[tikz,border=3.5 mm]{standalone}
usetikzlibrary{chains,
                positioning}

begin{document}
    begin{tikzpicture}[
node distance = 1mm,
  start chain,
     C/.style = {circle, draw, minimum size=1.3em, inner sep=2pt}
                       ]
foreach i [count=j] in {G,G,U,C,C,C, A,U,K}
{
ifnumj<7
    node[C, on chain=going below] {i};
else
    node[C, on chain=going below left] {i};
fi
}
     end{tikzpicture}
end{document}

Result is the same as before.

Answered by Zarko on April 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP