[Tex/LaTex] How to modify just some nodes’ styles in chemfig

chemfignodestikz-styles

In the following molecular drawing, I want to only circle the rightmost OH of the first molecule and the leftmost H of the second, to show that they are the ones that are involved in the formation of water:

\documentclass{article}
\usepackage{chemfig} % for typesetting molecules

\begin{document}

\chemfig{CH(-[4]H_2N)(-[6]CH_2(-[6]C(=[5]O)(-[7]OH)))-C(=[2]O)-OH} % I want to circle this last OH
\chemsign{+}
\chemfig{CH(-[4]H_2N)(-[6]CH_2(-[6]*6(=-=-=-)))-C(=[1]O)(-[7]O-CH_3)} % I want to circle the H of the H_2N near the beginning
\chemrel{<>} \\
\chemfig{CH(-[4]H_2N)(-[6]CH_2(-[6]C(=[5]O)(-[7]OH)))-C(=[2]O)-N(-[2]H)-CH(-[6]CH_2(-[6]*6(=-=-=-)))-C(=[1]O)(-[7]O-CH_3)}
\chemsign{+}
\chemfig{H_2O} \
\end{document}

enter image description here

I know that if I put

\setnodestyle{draw,circle}

on the very first line after \begin{document}, then I can make all the nodes (atoms) circled, but I only want to circle the two I mentioned, but I'm not having success inserting a \setnodestyle inside the \chemfig argument. Does anyone know how to custom modify just one node's style in chemfig?

Best Answer

Since you can use tikzpictures inside of tikzpictures you can for example define some \circleatom command and use it inside \chemfig:

\documentclass{article}
\usepackage{chemfig} % for typesetting molecules
\newcommand*\circleatom[1]{\tikz\node[draw,circle]{#1};}
\begin{document}

\chemfig{CH(-[4]H_2N)(-[6]CH_2(-[6]C(=[5]O)(-[7]OH)))-C(=[2]O)-\circleatom{OH}} % I want to circle this last OH
\chemsign{+}
\chemfig{CH(-[4]\circleatom{H}_2N)(-[6]CH_2(-[6]*6(=-=-=-)))-C(=[1]O)(-[7]O-CH_3)} % I want to circle the H of the H_2N near the beginning
\chemrel{<>} \\
\chemfig{CH(-[4]H_2N)(-[6]CH_2(-[6]C(=[5]O)(-[7]OH)))-C(=[2]O)-N(-[2]H)-CH(-[6]CH_2(-[6]*6(=-=-=-)))-C(=[1]O)(-[7]O-CH_3)}
\chemsign{+}
\chemfig{H_2O}

\end{document}

enter image description here

Edited: a better definition of \circleatom would be

\newcommand*\circleatom[1]{\tikz\node[circle,draw]{\printatom{#1}};}

The \printatom will ensure that that aroms are displayed the same if you change it to, say, have sans serif printed atoms.

BTW: I would probably use chemfig's scheming commands \schemestart, \arrow and \schemestop to typeset the reaction.

\documentclass{article}
\usepackage{chemfig}
\newcommand*\circleatom[1]{\tikz\node[circle,fill=green!30]{\printatom{#1}};}
\setatomsep{2em}
\setcompoundsep{7em}
\renewcommand*\printatom[1]{\ensuremath{\mathsf{#1}}}
\begin{document}

\schemestart
 \chemfig{CH(-[4]H_2N)(-[6]CH_2(-[6]C(=[5]O)(-[7]OH)))-C(=[2]O)-\circleatom{OH}} % I want to circle this last OH
 \+
 \chemfig{CH(-[4]\circleatom{H}_2N)(-[6]CH_2(-[6]*6(=-=-=-)))-C(=[1]O)(-[7]O-CH_3)} % I want to circle the H of the H_2N near the beginning
 \arrow{<=>}[-90]
 \chemfig{CH(-[4]H_2N)(-[6]CH_2(-[6]C(=[5]O)(-[7]OH)))-C(=[2]O)-N(-[2]H)-CH(-[6]CH_2(-[6]*6(=-=-=-)))-C(=[1]O)(-[7]O-CH_3)}
 \+
 \chemfig{\circleatom{H_2O}}
\schemestop
\end{document}

enter image description here

Related Question