了解如何在 chemfig 中应用不同的颜色

了解如何在 chemfig 中应用不同的颜色

在回答这个帖子,符号 1(答题者的昵称)使用下面的代码生成了以下不同颜色的化学图形。

在此处输入图片描述

\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}

我认为 LaTeX 编码有时很难(特别是对于像我这样的新用户而言),因为缺少一些注释和最少的解释。理解代码使我们能够将给定的解决方案扩展到其他情况。

在下面的下一段代码中,我在知道如何解释的地方添加了注释。所以,我的问题是:用什么注释最好来替代<etc?!>以下代码中的字段?

\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

相关内容