2

This code draws sequence of words, each word in a separate block. I want to manipulate it as one block. For example, how can I get next picture?enter image description here (I want to add $\rho$ in front of it).

\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}
    } 
}%
ashim
  • 1,783
  • 1
    You can include label=left:{$\rho$:} in the options of the first node, maybe with label distance=.5cm or something like that. – Qrrbrbirlbel May 06 '13 at 08:43
  • @Qrrbrbirlbel I have problems with accessing the first node. If I write \DNASequence{mysec}{C/cyan, B/red}; then I should access first node with mysec-1, right? – ashim May 06 '13 at 08:57
  • The first node is the one in the true branch of \IfStrEq. The correct syntax would be \DNASequence[mysec]{C/cyan, B/red}. But the \DNASequence doesn’t reset the ColumnCounter, so you might end-up with mysec-10 instead of -0 if the previous \DNASequence had 10 nodes. – Qrrbrbirlbel May 06 '13 at 09:03
  • @Qrrbrbirlbel I tried you suggestion, I have problem that the label is too close to the edge of a page. I tried to add two nodes in \IfStrEq but it does not draw two nodes. – ashim May 06 '13 at 09:16
  • Nevermind, I was able to add extra node which is a label essentially. Anyway thanks! – ashim May 06 '13 at 09:22

1 Answers1

3

You can either use an extra node or a label. I also have simplified the macro with the usage of the chains library which automatically names the node <chain name>-<increasing integer>.

Code

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

Output

enter image description here

enter image description here

Qrrbrbirlbel
  • 119,821