tikz,修改绘制 DNA 序列的代码

tikz,修改绘制 DNA 序列的代码

我需要修改绘画方式DNA序列.有一个函数\DNASequence可以完成这个任务。它使用固定大小的节点。但是,我需要随意的单元格中只有一个字母,而不是文本。怎么做?我尝试修改原始代码,但我不知道如何获取节点的实际宽度。

\documentclass[10pt]{beamer}
\usepackage[utf8]{inputenc} % Language = Spanish
\usepackage{color}
\usepackage{tikz}
\usetheme{Warsaw} 
\usecolortheme{whale}
\usepackage{xstring}
\usetikzlibrary{calc, arrows}

\newcommand*{\NodeSize}{0.5cm}%
\newcommand*{\YShiftBetweenRows}{-1cm}% Subsequent rows are shited down so they don't overlap
\tikzset{DNA Style/.style={minimum size=0.5cm, draw=gray, line width=1pt}}{}

\newlength{\YShift}% 
\newcounter{ColumnCounter}% Prefix for node labels

\setlength{\YShift}{0cm}% 
\setcounter{ColumnCounter}{0}
\newcounter{angle}
\setcounter{angle}{0}
%
\begin{document}
\newcommand*{\DNASequence}[2][Mark]{%
% https://tex.stackexchange.com/questions/12091/tikz-foreach-loop-with-macro-defined-list
\def\Sequence{#2}
\foreach [count=\xi] \Label/\Color in \Sequence {%
    \pgfmathsetmacro{\XShift}{\NodeSize*\xi}%
    \IfStrEq{\Color}{}{\def\Color{white}}{}
    \edef\NodeName{#1-\arabic{ColumnCounter}}
    \node [DNA Style, fill=\Color, xshift=\XShift] (\NodeName) {\Label};
    \stepcounter{ColumnCounter}
} 
}%


\begin{frame}{bla}
\begin{tikzpicture}
\DNASequence{AAAA/cyan,, Big/orange,,,Small/blue,,G/red!25,,C/yellow};
\end{tikzpicture}
\end{frame}
\end{document}

编辑:我需要 \DNASequence在节点中绘制任意文本的函数。

答案1

以下内容改编自我之前的回答使用 TikZ 绘制 DNA 序列的显示问题

在此处输入图片描述

代码:

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



\begin{document}
\begin{tikzpicture}
\begin{scope}
    \DNASequence{ABCC/magenta!20,,, XYZ/violet!30,,ABC/green, G/blue!20,, G/cyan!30,, C/olive};
\end{scope}
\begin{scope}[yshift=-1cm]
    \DNASequence{ABCDEFGHS/brown!20,, XYS/cyan!30,,, G/blue!20,, G/orange!30,, C/blue!30, X/red!20, Y/yellow!75};
\end{scope}
\end{tikzpicture}
\end{document}

相关内容