5

I have a lot of molecules to draw.

For teaching purposes I need to color certain atoms or group of atoms.

enter image description here

Due to alignment of the main chain, I'm using a | symbol after each atom on group, combined with textcolor , to change color them, the same way as used by unbonpetit.

But actually this is tedious, and I would like a more easy/simple/short mode to do that.

That is my code:

\documentclass{article}
\usepackage{chemfig}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{brace/.style = { decorate, decoration={brace, amplitude=5pt} }}

\setchemfig{%
    atom sep=17pt, 
    bond offset=1pt
}

\newcommand{\blue}[1]{\textcolor{blue}{#1}}
\newcommand{\red}[1]{\textcolor{red}{#1}}

\begin{document}
    \schemestart
    \chemfig{%
        [:-90]CH_2OH
        -[@{bond1},,1,3]\blue{H}|\blue{O}|\blue{C}|\blue{H}
        -[,,3,2]\blue{H}|\blue{C}|\blue{O}|\blue{H}
        -[,,2,3]\blue{H}|\blue{O}|\blue{C}|\blue{H}
        -[,,3]C(=[::90]O)
        -[,,1,3]\blue{H}|\blue{O}|\blue{C}|\blue{H}
        -[@{bond2},,3,3]\red{H}|\red{O}|\red{C}|\red{H}
        -[,,3,3]\red{H}|\red{O}|\red{C}|\red{H}
        -[@{bond3},,3,1]CH_2OH
    }
    %   
    \schemestop
    \chemmove{
        \draw[-,brace]
        ([xshift=30pt, yshift=-1pt]bond1) -- 
        ([xshift=30pt, yshift= 1pt]bond2)
        node[midway, right=5pt]{\blue{\textsc{l}-\textit{gluco}-}};
        %
        \draw[-,brace]
        ([xshift=30pt, yshift=0]bond2) -- 
        ([xshift=30pt, yshift=0]bond3)
        node[midway, right=5pt]{\red{\textsc{l}-\textit{erythro}-}};
    }   
\end{document}

I would appreciate any help!

0

1 Answer 1

7
\documentclass{article}
\usepackage{chemfig}
\let\printatomsaved\printatom
\def\print#1{\gdef\printatom##1{\printatomsaved{\color{#1}##1}}}
\def\restoreprint{\global\let\printatom\printatomsaved}
\setchemfig{atom sep=17pt,bond offset=1pt}
\begin{document}
\chemfig{[:-90]
    CH_2OH\print{blue}
    -[@{bond1},,1,3]HOCH
    -[,,3,2]HCOH
    -[,,2,3]H\print{black}OCH
    -[,,3]C(=[::90]O\print{blue})
    -[,,1,3]H\print{red}OCH
    -[@{bond2},,3,3]HOCH
    -[,,3,3]H\print{black}OCH
    -[@{bond3},,3,1]CH_2OH\restoreprint
}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .