typesetting different characters with different colors in a DNA sequence
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 listings
manual for more info.
(Revised version following Martin's suggestion)
\documentclass{article}
\usepackage{xcolor}
\begingroup
\catcode`A\active
\catcode`T\active
\catcode`C\active
\catcode`G\active
\gdef A{\textcolor{red}{\string A}}
\gdef T{\textcolor{green}{\string T}}
\gdef C{\textcolor{blue}{\string C}}
\gdef G{\textcolor{black}{\string G}}
\endgroup
\DeclareRobustCommand\DNA[1]{\begingroup
\scantokens{%
\catcode`A\active
\catcode`C\active
\catcode`G\active
\catcode`T\active
\ttfamily#1\endinput}%
\endgroup}
\begin{document}
\section{\DNA{ACGT}}
\DNA{ACGTACGT}, \fbox{\DNA{ACGTACGT}}
\end{document}
A more professional choice is to use texshade
package, especially for biology. I almost forgot it. It is quite complex for me, and there're some predefined styles. You'd better read the manual first.
\begingroup\lccode`~=`A
\lowercase{\endgroup\def~}{\textcolor{red}{A}}
\begingroup\lccode`~=`T
\lowercase{\endgroup\def~}{\textcolor{green}{T}}
\begingroup\lccode`~=`C
\lowercase{\endgroup\def~}{\textcolor{blue}{C}}
\begingroup\lccode`~=`G
\lowercase{\endgroup\def~}{\textcolor{black}{G}}
\let\NX\noexpand
\def\DNAmathcodes{%
\catcode`A=\active \catcode`T=\active
\catcode`C=\active \catcode`G=\active
\catcode`a=\active \catcode`t=\active
\catcode`c=\active \catcode`g=\active
}
\DeclareRobustCommand{\DNA}{\bgroup\DNAmathcodes\doDNA}
\newcommand\doDNA[1]{\texttt{\uppercase{\scantokens{#1\NX}}}\egroup}
\begin{tabular}{ll}
1 & \DNA{GCATCCAATGCC}\\
2 & \DNA{GGGACCAATGCG}\\
3 & \DNA{TTATCCAATCTC}
\end{tabular}
Note: without \noexpand
, \scantokens
would add a space; but \noexpand
contains an "a"! Also \DNA
must be robustified, if we need it in a section title.