[Tex/LaTex] How to draw 3d molecules with TikZ


Is it possible to draw molecules in 3d with TikZ like in this picture?

enter image description here

Best Answer

For pseudo 3D, you can define

  • different ball styles for the atoms
  • different bond syles

I wrote a general macro for defining bonds. It simply draws many lines on top of each other to simulate the look of 3D tubes. The bonds are drawn on a background plane so they don't overlap the atoms.


\documentclass[tikz, border=2mm]{standalone}



% start, end, thickness, incolor, outcolor, iterations
{ \begin{pgfonlayer}{background}
        \foreach \I in {#6,...,1}
        {   \pgfmathsetlengthmacro{\r}{#3/#6*\I}
            \draw[InColor!\C!OutColor, line width=\r] (#1.center) -- (#2.center);

% start, end
{   \Bond{#1}{#2}{1mm}{white}{gray!50}{10}

% start, end
{   \Bond{#1}{#2}{1mm}{orange!70}{red!75!black}{10}

% start, end
{   \Bond{#1}{#2}{2mm}{cyan}{blue!50!black}{10}

% start, end
{   \Bond{#1}{#2}{0.7071mm}{green!25}{green!25!black}{10}

[   oxygen/.style={circle, ball color=red, minimum size=6mm, inner sep=0},
    hydrogen/.style={circle, ball color=white, minimum size=2.5mm, inner sep=0},
    carbon/.style={circle, ball color=black!75, minimum size=7mm, inner sep=0}
    \node[oxygen] (O1) at (0,0) {};
    \node[hydrogen] (H1) at (1,-0.5) {};
    \node[hydrogen] (H2) at (-1,-0.5) {};

    \foreach \c in {1,...,6}
    {   \node[carbon] (C-\c) at ($(2,3)+(\c*60:1.5)$) {};
        \node[hydrogen] (H-\c-a) at ($(2,3)+(\c*60-15:2.5)$) {};
        \node[hydrogen] (H-\c-b) at ($(2,3)+(\c*60+15:2.5)$) {};
    \foreach \c [evaluate={\c as \n using int(mod(\c,6)+1)}] in {1,...,6}
    {   \GreenBond{C-\c}{C-\n}



enter image description here

Related Question