[Tex/LaTex] typesetting different characters with different colors in a DNA sequence

colorformattingverbatim

I have created a macro which I used to format DNA sequences, as they should be in the monospaced font and uppercased:

\newcommand{\DNA}[1]{\texttt{\uppercase{#1}}}

Now I have a column with some different DNA sequences on different rows, lets say:

1 & \DNA{GCATCCAATGCC}\\
2 & \DNA{GGGACCAATGCG}\\
3 & \DNA{TTATCCAATCTC}\

I would like to color each character (base) in the DNA sequence with a different color, as it easier for the reader to see certain patterns (in this case the reoccuring CCAAT sequence, a putative Hap complex binding domain).
(for example as done here)

that means, all
A -> Red
T -> Green
C -> Blue
G -> Black

Is there a way to do this, or a verbatim package in which this can be achieved ?
Thanks alot for your help!

———-

Liu's example posted works well, but not in my tabular/floatrow table
Tobi's example works well in all environments tested..

Thanks again for your great help all!

Best Answer

Like this?

[EDIT: I changed the example so it doesn’t touch other listings.]

\documentclass{article}
\usepackage{listings,xcolor}
\lstdefinestyle{dna}{%
    literate={A}{\textcolor{green}{A}}{1}
        {B}{\textcolor{blue}{B}}{1}
        {C}{\textcolor{red}{C}}{1}
        {a}{\textcolor{green}{A}}{1}
        {b}{\textcolor{blue}{B}}{1}
        {c}{\textcolor{red}{C}}{1},
    basicstyle=\ttfamily,
}
\newcommand{\DNA}[1]{%
    \lstinline[style=dna]{#1}%
}
\begin{document}
\lstinline{ABCabc}\quad\DNA{bbCa}
\section{Somthing about \DNA{abcabcababc}}
\end{document}

You can use it also in commands like \section See the listingsmanual for more info.

Related Question