# uncover chemfig molecule (in beamer)

I teach chemistry and wish to demonstrate drawing and naming of chemical structures I use chemfig to draw my molecules and I can do that fine. However, in for example the molecule 4-ethyl-3,3-dimethylhexane there are a number of components to the molecule which I want to uncover bit by bit to illustrate the naming/drawing of such a molecule.

We have a hexane root - draw this

Below is the code to build the molecule bit by bit as individual figures but I would like to be able to uncover on one molecule and highlight the pieces as they are added.

I had tried to use \onslide<n->in the parts I wanted to uncover but just got goobleydegook. I would be grateful if anyone has any ideas...

Below is fine but I would prefer to add to the parent molecule bit by bit...

\documentclass[t]{beamer}
\usepackage{chemfig}
\begin{document}

\begin{frame}{}
4-ethyl-3,3-dimethylhexane
\chemfig[][scale=0.5]{CH_3%6 these numbers are the carbon positions in the chain for ease of reading
-[::30]%5
-[::-60]%4
(-[::-60]-[::60]CH_3)%ethyl
-[::60]%3
(-[::30]CH_3)
(-[::90]CH_3)%methyl
-[::-60]%2
-[::60]CH_3%1
}
\begin{itemize}
\item root is hexane; hex=6; 6 carbon alkane
\onslide<2->{
\chemfig[][scale=0.5]{CH_3%6 these numbers are the carbon positions in the chain for ease of reading
-[::30]%5
-[::-60]%4
%(-[::-60]-[::60]CH_3)%ethyl
-[::60]%3
%(-[::30]CH_3)%methyl
%(-[::90]CH_3)%methyl
-[::-60]%2
-[::60]CH_3%1
}
}
\onslide<3->{\item 4-ethyl: ethyl substituent = 2 carbon alkyl group; on carbon 4}
\onslide<4->{
\chemfig[][scale=0.5]{CH_3%6 these numbers are the carbon positions in the chain for ease of reading
-[::30]%5
-[::-60]%4
(-[::-60]-[::60]CH_3)%ethyl}
-[::60]%3
%(-[::30]CH_3)%methyl
%(-[::90]CH_3)%methyl
-[::-60]%2
-[::60]CH_3%1
}
}
\onslide<5->{\item 3,3-dimethyl: di= 2, methyl substituents = 1 carbon alkyl group; on carbons 3 and 3}

\onslide<6->{\chemfig[][scale=0.5]{CH_3%6 these numbers are the carbon positions in the chain for ease of reading
-[::30]%5
-[::-60]%4
(-[::-60]-[::60]CH_3)%ethyl
-[::60]%3
(-[::30]CH_3)%methyl
(-[::90]CH_3)%methyl
-[::-60]%2
-[::60]CH_3%1
}
}
\end{itemize}
\end{frame}
\end{document}

-

We can set the opacity of the bonds we do want to hide to zero and make them visible again with \pgfkeysalso.

Sadly, I have not found a simple way to pass TikZ options to the nodes (the atoms like C and H_3) otherwise it would work like the same with visible on=<slides>.

I propose two solutions:

1. Redefinition of \printatom with checks a set of integers against the current node number.

To see the numbers one has to use \qrrDebugNodes which needs to enclosed in { } if it shouldn’t be global or one reset at the end of the to analyzed \chemfig with

\qrrDebugNodes[false]


The bonds can still be placed on different atoms (whatever the syntax).

2. A simply \unc macro that is built upon \uncover which hides its argument on the specified slides. This has the disadvantages that CH_3 is seen as one atom and not as two (C and H_3), which makes the placements of the bonds defunct, even if we use more than one \unc (for every atom one).

# Solution with a re-defined \printatom

## Code

\documentclass[t]{beamer}
\usepackage{chemfig,mathtools}
\tikzset{
invisible/.style={opacity=0,text opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}
},
}
\makeatletter
\newcounter{qrr@nodes}
\newif\if@qrr@debug
\newcommand*{\setNodes}[1]{%
\def\qrr@nodes{#1}%
\setcounter{qrr@nodes}{0}% if \setNodes is used inside a frame the reset happens too!
}
\newcommand*{\qrrDebugNodes}[1][true]{
\csname @qrr@debug#1\endcsname
}
\renewcommand*{\printatom}[1]{%
\def\qrr@cmd{}%
\def\qrr@slides@{}%
\foreach \qrr@number/\qrr@slides in \qrr@nodes {%
\foreach \qrr@number@ in \qrr@number {%
\ifnum\value{qrr@nodes}=\qrr@number@\relax
\global\let\qrr@cmd\uncover
\xdef\qrr@slides@{<\qrr@slides>}%
\breakforeach
\fi
}%
}%
\ensuremath{%
\expandafter\qrr@cmd\qrr@slides@{\mathrm{#1}}%
\if@qrr@debug
\smash{\mathllap{\mathop{}\limits_{\textnormal{\tiny (\the\value{qrr@nodes})}}}}%
\fi
}%
\stepcounter{qrr@nodes}%
}
\makeatother
\begin{document}
\begin{frame}
\setNodes{{5,6}/2-,{8,9}/3-,{10,11}/4-}
%\qrrDebugNodes
\chemfig[][scale=1.5]{CH_3%6 these numbers are the carbon positions in the chain for ease of reading
-[::30]%5
-[::-60]%4
(-[::-60,,,,visible on=<2->]-[::60,,,,visible on=<2->]CH_3)%ethyl
-[::60]%3
(-[::30,,,,visible on=<3->]CH_3)%methyl
(-[::90,,,,visible on=<4->]CH_3)%methyl
-[::-60]%2
-[::60]CH_3%1
}
\end{frame}
\end{document}


# Solution with \unc

The following solution with \unc makes chemfig see CH_3 as one atom and can not split it into C and H_3 to place bonds correctly, even if one uses two \unc.

The upper CH_3 are misplaced compared to the normal output.

## Code

\documentclass[t]{beamer}
\usepackage{chemfig}
\tikzset{
invisible/.style={opacity=0,text opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}
},
}
\newcommand*{\unc}[2]{%
\uncover<#1>{#2}
}
\begin{document}
\begin{frame}
\chemfig[][scale=1.5]{CH_3%6 these numbers are the carbon positions in the chain for ease of reading
-[::30]%5
-[::-60]%4
(-[::-60,,,,visible on=<2->]-[::60,,,,visible on=<2->]\unc{2-}{CH_3})%ethyl
-[::60]%3
(-[::30,,,,visible on=<3->]\unc{3-}{CH_3})%methyl
(-[::90,,,,visible on=<4->]{\unc{4-}{C}}{\unc{4-}{H_3}})%methyl
-[::-60]%2
-[::60]CH_3%1
}
\end{frame}
\end{document}


## Output

-
Nice. The issue with the CH_3 is not too critical as it is assumed the end is CH_3 if not explicitly written in. I'll wait a while before I accept this as an answer in case another solution is presented –  Leeser Feb 16 '13 at 17:32
Can this be expanded to take into account a reaction scheme; ie uncover a reaction step by step, or add electron motion step by step? –  Leeser Mar 20 '13 at 12:47

You may cheat a little with white color :-)

Try this, for example:

\parbox[c][.5\textheight][c]{\columnwidth}{%
\centering\mbox{}%
\def\test{white}%
\only<2>{\def\test{red}}%
\only<3->{\def\test{black}}%
\chemfig[][scale=0.5]{CH_3%6
-[::30]%5
-[::-60]%4
(-[::-60,,,,\test]-[::60,,,,\test]\textcolor{\test}{CH_3})%ethyl
-[::60]%3
(-[::90]CH_3)%methyl
-[::-60]%2
-[::60]CH_3%1
}
}


The above code will display a branch in red at slide 2, then in black.

-
I thought about that but I often put my examples inside an exampleblock or other block environment which have differing background colours so would have to be adapted for this each time. –  Leeser Feb 15 '13 at 13:59
If it is a solid color, you may replace the white color by the background color. –  Lionel MANSUY Feb 15 '13 at 14:01