tikz,如何将一系列节点作为一个对象进行操作

tikz,如何将一系列节点作为一个对象进行操作

代码绘制单词序列,每个单词位于一个单独的块中。我想将其作为一个块进行操作。例如,我如何获取下一张图片?在此处输入图片描述 (我想在它前面添加 $\rho$)。

\documentclass[border=3pt]{article}
\usepackage{tikz}
\usepackage{xstring,calc}
\usetikzlibrary{calc}

\newlength{\NodeSize}

\tikzset{DNA Style/.style={minimum size=0.5cm, draw=gray, line width=1pt, inner sep = 2pt}}{}


\newcounter{ColumnCounter}% Prefix for node labels


\newlength{\CurrentXPos}
\newcommand*{\PreviousNode}{}%
\newcommand*{\DNASequence}[2][Mark]{%
    % https://tex.stackexchange.com/questions/12091/tikz-foreach-loop-with-macro-defined-list
    \def\Sequence{#2}%
    \def\PreviousNode{}%
    \foreach [count=\xi] \Label/\Color in \Sequence {%
        \IfStrEq{\Color}{}{\def\Color{white}}{}
        \edef\NodeName{#1-\arabic{ColumnCounter}}
        \IfStrEq{\PreviousNode}{}{%
            \node [DNA Style, fill=\Color, anchor=west] (\NodeName) {\Label};
            \xdef\PreviousNode{\NodeName}%
        }{
            \node [DNA Style, fill=\Color, anchor=west, xshift=-\pgflinewidth] at (\PreviousNode.east)(\NodeName) {\Label};
            \xdef\PreviousNode{\NodeName}%
        }
        \stepcounter{ColumnCounter}
    } 
}%

答案1

您可以使用额外的节点或标签。我还使用chains自动命名节点的库简化了宏<chain name>-<increasing integer>

代码

\documentclass[tikz,border=3pt,convert=false]{standalone}
\usepackage{xstring}
\usetikzlibrary{chains}

\tikzset{
  @expand me/.style={#1},% auxiliary style
  DNA Style/.style={minimum size=0.5cm, draw=gray, line width=1pt, inner sep = 2pt}
}

\newcommand*{\DNASequence}[2][Mark]{%
    % http://tex.stackexchange.com/questions/12091/tikz-foreach-loop-with-macro-defined-list
    \edef\Sequence{#2}%
    \scope[start chain=#1 going right, node distance=+-\pgflinewidth]
      \foreach \Label/\Style[count=\xi] in \Sequence {
        \IfStrEq{\Style}{}{\def\Style{none}\tikzset{DNA Style/.append style={draw=none}}}{}
        \node [DNA Style, fill=\Style, anchor=west, on chain=#1] {\Label};
      }
    \endscope
}%
\begin{document}
\tikz[nodes={text depth=+0pt,font=\vphantom{A}}]{
  \DNASequence[Example 1]{$\rho$:/, C/cyan, B/red}
}

\tikz{
  \DNASequence[Example 1]{C/cyan, B/red}
  \path [late options={name=Example 1-1,label={[text depth=+0pt,label distance=.5cm]left:$\rho$:\vphantom{A}}}];
}
\end{document}

输出

在此处输入图片描述

在此处输入图片描述

相关内容