9

I want to color simple functional groups in organic molecules using the chemfig package. I've come up with a solution that achieves what I want (MWE), but I find it very cumbersome. I thought it could be possible to wrap an entire group in one single color-statement. This question about colouring the background of a group of atoms in chemfig also gives a neat graphical emphasis of a group of atoms.

Is my solution, and the solution to the question above, the easiest way to colour or mark groups of atoms (including bonds) in chemfig? Just now, I don't have a need to easily reproduce loads of coloured functional groups, but I would like to know if there are easier ways to solve this problem without predefining macros for each group.

I believe I understand that the underlying problem is the way chemfig uses TikZ nodes to draw the structures, but I can't help to think that there should be a way to define some color environment that applies to all the TikZ nodes made by chemfig.

MWE:

\documentclass{article}

\usepackage{chemfig}

\begin{document}
    \chemfig{H-C(-[2]H)(-[6]H)-C(-[2]H)(-[6]H)-{\color{blue}C}(=[1,,,,blue] {\color{blue}O})(-[7,,,,blue] {\color{blue}OH})}
\end{document}

Producing

enter image description here

  • I don't really have an answer but I'd probably really define macros or submols (depending on the wanted functionality) – clemens Feb 21 '15 at 18:50
  • I believe that what you did is the best you can do in chemfig at the moment. – pisoir Feb 25 '15 at 6:59
7

Relate to this post.

\documentclass{article}
\usepackage{chemfig}
\begin{document}
    \def\RED{\gdef\printatom##1{\color{red}\ensuremath{\mathrm{##1}}}}
    \def\BLACK{\gdef\printatom##1{\color{black}\ensuremath{\mathrm{##1}}}}
    \noindent
    \chemfig{H-C(-[2]H)(-[6]H)-C(-[2]H)(-[6]H)(-[,,,,draw=none]\RED)([,,,,,red]-[,,,,black]C(=[1]O)(-[7]OH))}\BLACK \\[1cm]
    \chemfig{H-C(-[2]H)(-[6]H)-C(-[2]H)(-[6]H)(-C(=[1]O)(-[7]OH))}
\end{document}

Where

  • [,,,,red] is to set the color of bonds within COOH.
  • [,,,,black] is to set the color of bond between C2H5 and COOH.
  • (-[,,,,draw=none]\RED) is to create an ad hoc atom to change the color.
    • you can change the color anywhere, but sometimes it may cause bad spacing/positioning.
    • (e.g. chemfig reads \RED COOH as five atoms, with the first atom having zero width.)

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.