In the answer to this post, Symbol 1 (the answerer's nick) generated the following chemical figure with different colors using the code below.
\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}
I believe LaTeX coding are sometimes tough (specially to new users, like me) because some comment with minimal explanation are missing. Understanding code allows us to extend the given solution to other situations.
In the next piece of code below, I put comments where I know how to explain. So, my question here is: what are the best comments to substitute the <etc?!> fields in the following code?
\documentclass{article} %-> Defines the document class
\usepackage{chemfig} %-> A package to generate chemical strucutre formulas
\begin{document} %-> Begins the document
\def\RED{%-> \def is a command to create a new macro named \RED
\gdef%-> <meaning of gdef?!>
\printatom##1{%-> <meaning of \printatom?!>, <meaning of ##1?!>
\color{red}%-> Applies color red to the text
\ensuremath{%-> Verify if it is in math mode. If not, starts math mode
\mathrm{##1}%-> Applies normal font to letters in math mode
}
}
}
\chemfig{%-> Starts drawing a molecular structre
H %-> Adds the atom H
-C %-> Adds the simple bond "-", and the 1st atom C
( %-> Starts a branch at 1st C
-[2]H %-> Adds a simple bond "-" oriented 90 deg and the atom H at its end
%... note that in "[x]", x is a factor of 45 deg counterclockwise
%... therefore [2] = [90 deg]
) %-> Ends the branch at 1st C
( %-> Starts another branch at 1st C
-[6]H %-> Adds a simple bond "-" oriented 270 deg and the atom H
) %-> Ends the other branch at 1st C
-C %-> Adds a simple bond "-" and the 2nd atom C
( %-> Starts a branch at 2nd C
-[2]H %-> Adds a simple bond "-" oriented 90 deg and the atom H
) %-> Ends the branch at 2nd C
( %-> Starts another branch at 2nd C
-[6]H %-> Adds a simple bond "-" oriented 270 deg and the atom H
) %-> Ends the other branch at 2nd C
( %-> <?!>
-[,,,,draw=none]\RED %-> <?!>
) %-> <?!>
( %-> <?!>
[,,,,,red] %-> <?!>
-[,,,,black]C %-> Adds a simple bond "-", applies black to font <how/why?!>, and adds the 3rd atom C
( %-> Starts a branch at 3rd C
=[1]O %-> Adds double bond "=" oriented 45 deg and the atom O
) %-> Ends the branch at 3rd C
( %-> Starts another branch at 3rd C
-[7]OH %-> Adds a simple bond "-" oriented 315 deg and the atoms OH
) %-> Ends the other branch at 3rd C
) %-> <?!>
} %-> Ends the chemical strucutre
\end{document} %-> Ends the document
