为 foreach 循环预定义变量

为 foreach 循环预定义变量

我目前尝试绘制如图 1 所示的 tikz 图片。这基本上就是我想要的,我可以为整个序列绘制它,但它很长。由于这只是一遍又一遍地重复相同的任务,我认为我可以将(至少一些)放入 foreach 循环中。

图 2 是我第一次尝试,但你可以看到距离并不相同,并且无法调整[node distance=0 mm]。而且我认为一旦序列形成曲线,它就会变得复杂。

所以我尝试了其他方法(图 3)。我更喜欢这个想法,但不幸的是它不起作用。我失败了,因为在启动\j时变量未定义\foreach,并尝试在node[draw,circle,be低 =of \j] node. -> At least I think that this is the problem. So my question is, is it possibly to define a variable\j for the\foreach` 循环下写入一个节点,所以她有一个起点?

还有其他方法可以解决这个问题吗?

左边=图1;中间=图2;正确的= 图 3

在此处输入图片描述在此处输入图片描述在此处输入图片描述

\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 \i\j in {1/G,2/G,3/U,4/C,5/C,6/C,7/A}
    %   {   
    %       \node[draw,circle] at (0,-\i)(\j){\Large\j};
    %
    %   }
    %       \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 \i\j in {1/G,2/U,3/C,4/C,5/C,6/A}
    %   {
    %       \i
    %   }
    
    % Whole Sequence: GGUCCCAUKGUGPAAU\#GDDAGCACUCPGGABUNUGAAPCCAGCGAU??GAGPPCA”AUCUCGGUGGGACCUCCA
    \end{tikzpicture}
    
\end{document}

答案1

chains可以编程为在单个链中执行几乎任何操作。您可以使用placed键并动态更改参数。一个非常直截了当的实现,它使用两个参数,方向growth dir和距离growth dist,使用一些进行更改\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}   
  {\ifnum\numexpr\Y>8\relax 
  \tikzset{growth dir=-135}
  \fi
  \node {\X};}
 \end{scope} 
\end{tikzpicture}
\end{document}

在此处输入图片描述

还有更多可能的变化。

答案2

像这样?

在此处输入图片描述

具有两个循环和chains库:

\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}

附录: 受到有趣的@Pumuckl 答案的启发,并考虑 TikZ 和 PGF 手册第 605 页(版本 3.1.8b)中的示例,可以在一个循环中绘制上述图像:

\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}
{
\ifnum\j<7
    \node[C, on chain=going below] {\i};
\else
    \node[C, on chain=going below left] {\i};
\fi
}
     \end{tikzpicture}
\end{document}

结果和以前一样。

相关内容