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


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


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!

  • 3
    omg guys... I just went to the groceries, and after returning I see three splendid answers to my question... I feel very humble now. – Timtico Apr 25 '11 at 13:27
  • Is there also a way to put a line under a specified nucleotide? – user5644 May 20 '11 at 10:58
  • @Ros: You could change a literate line like {B}{\textcolor{blue}{B}}{1} to {B}{\underline{B}}{1} in my above example. – Tobi May 20 '11 at 13:44
  • Thanks, toby! But I don't want to underline all B's. E.g. if I want to emphasize a start-codon in, let's say, the sequence ACTACATGCA, I only want to underline ATG, and not the other nucleotides. – user5644 May 20 '11 at 13:49
  • You might want to have a look at the texshade package (Typesetting nucleotide and peptide alignments) and maybe also textopo (for shaded membrane protein topology plots) by the same author. – cgnieder Mar 22 '12 at 17:51

Like this?

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

\section{Somthing about \DNA{abcabcababc}}

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

  • +1 for listings. Thanks to the more robust implementation, it works better in macro arguments. – Leo Liu Apr 25 '11 at 14:00

(Revised version following Martin's suggestion)

\gdef A{\textcolor{red}{\string A}}
\gdef T{\textcolor{green}{\string T}}
\gdef C{\textcolor{blue}{\string C}}
\gdef G{\textcolor{black}{\string G}}






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.

  • I just looked at some examples in the manual of texshade, and indeed it's a very nice package. However, in this case I have already preprocessed information. In which I already truncated part of the sequence I want to show with a perlscript. I'm combining that information with some other tabular info – Timtico Apr 25 '11 at 13:40
  • Your example doesn't work in the xelatex / fontspec environment. I will need to take a look into that. – Timtico Apr 25 '11 at 13:41
  • @Timtico: The code I listed works fine with fontspec on my computer. Maybe you can provide more information about it. (egreg's code is actually the same.) BTW, I knew texshade from LaTeX Graphics Companion, but I've never used it in practice. – Leo Liu Apr 25 '11 at 13:53
  • Yep, I didn't want to make it too complex. You can easily add some code for lowercase. – Leo Liu Apr 25 '11 at 14:08
  • 1
    Instead of \symbol{`A} you could also use \string A. If you use \scantokens the \DNA macro could also be used in macro arguments. – Martin Scharrer Apr 25 '11 at 14:33

For completeness, here is a solution using LuaTeX. I use LPEG to parse the input and then apply the appropriate style to it. The solution is in ConTeXt, but it should be easy to translate it to LaTeX.


  thirddata = thirddata or {}
  thirddata.dna = thirddata.dna or {}
  local dna = thirddata.dna

  dna.attributes ={ 
      A = { color = "red",   style = "mono" },
      T = { color = "green", style = "mono" },
      C = { color = "blue",  style = "mono" },
      G = { color = "black", style = "mono" },
      default = { color = "black", style = "mono"},

  local function format(char)
    local hash = dna.attributes[char] or dna.attributes[default]
    context.style(hash, char)

  local char = lpeg.S("ATCG") / format
  dna.parser = lpeg.Cs(char^0)

  function commands.dnaparse(str)




The advantage of using Lua is that you don't have to mess around with catcodes.




  \catcode`A=\active \catcode`T=\active
  \catcode`C=\active \catcode`G=\active
  \catcode`a=\active \catcode`t=\active
  \catcode`c=\active \catcode`g=\active



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.

  • You could use \scantokens to allow \DNA to work in macro arguments. If you use \lowercase to define the letter macros you don't need to use \char`\A, simply A etc. should do it. – Martin Scharrer Apr 25 '11 at 14:35
  • The \char\A` remained from a try with \mathcode. Edited. Thanks. – egreg Apr 25 '11 at 19:41

New solution, with a parser.

Using core TeX:

    \csname dna#1\endcsname

Using pgf's parser module:

\pgfparserdef{dna}{initial}{\meaning A}{\textcolor{red}{A}}
\pgfparserdef{dna}{initial}{\meaning T}{\textcolor{green}{T}}
\pgfparserdef{dna}{initial}{\meaning C}{\textcolor{blue}{C}}
\pgfparserdef{dna}{initial}{\meaning G}{\textcolor{black}{G}}
\pgfparserdef{dna}{initial}{\meaning ;}{\pgfparserswitch{final}}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.