Math & SciencetikZ | Drawing Molecules

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

tikZ | Drawing Molecules

Post by ghostanime2001 »

I want to draw molecules with tikZ not with chemfig because tikZ has more flexibility of tweaking bond lengths and atomic spacing. I tried to change these parameters with chemfig but there were lots of intricacies that I didn't like (for example really really short bonds). I can create single bonds between atoms, but how do you create double bonds/triple bonds for example between a carbon and oxygen atom or between two nitrogen atoms? Also, how do you orient bonds at different angles?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tikZ | Drawing Molecules

Post by localghost »

ghostanime2001 wrote:I want to draw molecules with tikZ not with chemfig because tikZ has more flexibility of tweaking bond lengths and atomic spacing. […]
It is exactly the other way round.
ghostanime2001 wrote:[…] I tried to change these parameters with chemfig but there were lots of intricacies that I didn't like (for example really really short bonds). I can create single bonds between atoms, but how do you create double bonds/triple bonds for example between a carbon and oxygen atom or between two nitrogen atoms? Also, how do you orient bonds at different angles?
And the chemfig manual didn't help? I'm sure that all your question are answered by just reading a bit in the package manual.

Code: Select all

\documentclass[11pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{chemfig}

\setatomsep{2em}

\begin{document}
  \chemfig{N=[:30]N}
\end{document}
If you expect specific help, a proper, self-contained and minimal example along with an adequate problem description is essential. That is something you should have learned by now.


Thorsten
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

tikZ | Drawing Molecules

Post by cgnieder »

ghostanime2001 wrote:I want to draw molecules with tikZ not with chemfig because tikZ has more flexibility of tweaking bond lengths and atomic spacing.
Of course you can but IMHO you will soon notice that it's also a lot more work.
ghostanime2001 wrote:I tried to change these parameters with chemfig but there were lots of intricacies that I didn't like (for example really really short bonds).
If I had to guess (and apparently I have to) you probably set chemfig's atom sep to a short value but haven't read section 4 Length of a bond of the manual where the impacts are explained.
ghostanime2001 wrote:I can create single bonds between atoms, but how do you create double bonds/triple bonds for example between a carbon and oxygen atom or between two nitrogen atoms
See section 2 Different kinds of bonds.
ghostanime2001 wrote:Also, how do you orient bonds at different angles?
See section 3 Bond angle.

Regards
site moderator & package author
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Re: tikZ | Drawing Molecules

Post by ghostanime2001 »

Thank for all your help. But I already know what you were talking about. In my post I was wondering weather similar effects (bonds, angles, bond length) can be done with tikz. I already know how to do it with chemfig.

Also, I know that between "slimmer" and "thicker" atoms the bond length varies that is why I am trying to change to tikz so that the bond length remains exactly the same length regardless of how thick or thin the width of atoms are.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

tikZ | Drawing Molecules

Post by cgnieder »

Well, of course you can draw molecules with TikZ. The proof: chemfig uses TikZ. There is no chemistry library for TikZ if that's what you mean. You'd have to draw everything “by hand”, i.e., program it yourself.

Regards
site moderator & package author
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Re: tikZ | Drawing Molecules

Post by ghostanime2001 »

That's exactly what I'm after but I how do you draw double bonds and triple bonds with TIKZ ?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Re: tikZ | Drawing Molecules

Post by cgnieder »

I'm unsure what you want really... Do you already have code and are struggling with adding a second line (or third, resp.)? If so, please show what you already have so far.

Regards
site moderator & package author
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tikZ | Drawing Molecules

Post by localghost »

ghostanime2001 wrote:That's exactly what I'm after but I how do you draw double bonds and triple bonds with TIKZ ?
  • Why are you so stubborn in this matter?
  • Why do you insist on this more difficult and more complicated method?
  • Are there any really good reasons against the package?
m(
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

tikZ | Drawing Molecules

Post by ghostanime2001 »

I guess this could work as a MWE example. How do I set the bond length to be the same length for example 1cm?

Code: Select all

\documentclass[fleqn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{chemmacros}
\usepackage{chemfig}
\usepackage{mhchem}
\usepackage{tikz}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
\setlength{\mathindent}{0pt}
\newcommand{\s}{\par\medskip}
\newcommand{\sm}{\par\smallskip}
\newcommand{\bs}{\par\bigskip}
\pagestyle{empty}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri,matrix}
\begin{document}
\begin{tikzpicture}
\node (a) {C};
\node (b) [above=of a] {H};
\node (c) [below=of a] {H};
\node (d) [left=of a] {H};
\node (e) [right=of a] {C};
\node (f) [below right=of e] {O};
\node (g) [right=of f] {H};
\node at (a.north east) {+};
\draw (a.north) to (b.south);
\draw (a.south) to (c.north);
\draw (d.east) to (a.west);
\draw (e.west) to (a.east);
\draw (f.west) to (e.east);
\draw (f.east) to (g.west);
\end{tikzpicture}
\end{document}
Attachments
chemfig-bond-length.png
chemfig-bond-length.png (18.22 KiB) Viewed 11415 times
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tikZ | Drawing Molecules

Post by localghost »

ghostanime2001 wrote:[…] How do I set the bond length to be the same length for example 1cm? […]
Isn't this exactly what chemfig does?

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{chemfig}

\setatomsep{1cm}

\begin{document}
  \chemfig{\chemabove{C}{\hspace{1pc}\scriptstyle +}(-[2]H)(-[4]H)(-[6]H)-C-[:-60]O-H}
\end{document}
Attachments
tmp.png
tmp.png (2.74 KiB) Viewed 11414 times
Post Reply