LaTeX forum ⇒ Math & SciencetikZ | Drawing Molecules

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
User avatar
cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Re: tikZ | Drawing Molecules

Postby cgnieder » Fri Oct 26, 2012 11:13 am

localghost wrote:
ghostanime2001 wrote:[…] How do I set the bond length to be the same length for example 1cm? […]

Isn't this exactly what chemfig does?


Actually not, and I guess this is what ghostanime refers to:
chemfig manual wrote:Rather than speaking of length of a bond, we should use the term interatomic spacing. If effect, only the interatomic spacing is adjustable with \setatomsep as we have seen on page 11. Once this parameter is set, the length of a bond depends on the content of atoms and, to a lesser extent, the angle the bond makes with the horizontal. It should be obvious that two “slimmer” atoms will have larger edge separations than two which are larger. This can be seen easily in the following example where an “I” atom is narrower than an “M” atom, which means that the bond between the “I” atoms is longer than that between the “M” atoms.


In my eyes chemfig's behaviour most times is a good thing, though.

@ghostanime: you need to specify the node distance property, see 16.5.3 Advanced Placement Options of the pgfmanual for details.

  1. \documentclass{article}
  2. \usepackage[margin=1in]{geometry}
  3. \usepackage{tikz}
  4. \usetikzlibrary{positioning}
  5. \begin{document}
  6.  
  7. \def\bondlength{7mm}
  8. \begin{tikzpicture}[node distance=\bondlength]
  9. \node (a) {C};
  10. \node (b) [above=of a] {H};
  11. \node (c) [below=of a] {H};
  12. \node (d) [left=of a] {H};
  13. \node (e) [right=of a] {C};
  14. \node (f) [below right=\bondlength of e] {O};
  15. \node (g) [right=of f] {H};
  16. \node at (a.north east) {+};
  17. \draw (a.north) to (b.south);
  18. \draw (a.south) to (c.north);
  19. \draw (d.east) to (a.west);
  20. \draw (e.west) to (a.east);
  21. \draw (f.north west) to (e.south east);
  22. \draw (f.east) to (g.west);
  23. \end{tikzpicture}
  24.  
  25. \end{document}


Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

Tags:

ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Postby ghostanime2001 » Fri Oct 26, 2012 5:00 pm

How to set the distance between the bond and the atom in this example ? I have tried \setatomsep but it gives no difference. If you don't mind me asking, can the above example's syntax be simplified ?
Last edited by cgnieder on Fri Oct 26, 2012 6:00 pm, edited 1 time in total.
Reason: added inline code markup

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

Postby cgnieder » Fri Oct 26, 2012 6:38 pm

ghostanime2001 wrote:How to set the distance between the bond and the atom in this example ? I have tried \setatomsep but it gives no difference.

Are you refering to the example I've given? \setatomsep is a »chemfig« command and not a »TikZ« command! I defined the parameter

  1. \def\bondlength{7mm}


in my example which can be adapted to a suitable value...

ghostanime2001 wrote:If you don't mind me asking, can the above example's syntax be simplified ?

Well, it never will be as simple as the »chemfig« syntax. You have two choices (or three, actually):
  1. use chemfig with easy syntax an live with the fact that there is no exact bond length.
  2. use TikZ directly and live with the fact that the syntax isn't as easy.
  3. don't use LaTeX for the molecules but a program like chemdraw.

You can, however, simplify it a bit by defining suitable commands:
  1. \documentclass{article}
  2. \usepackage[margin=1in]{geometry}
  3. \usepackage{tikz}
  4. \usetikzlibrary{positioning}
  5.  
  6. \newcommand*\bondlength{7mm}
  7. \newcommand*\bond[2]{\draw (#1) edge (#2);}
  8.  
  9. \newcommand*\carbon[2][] {\node[#1] (C#2) {C};}
  10. \newcommand*\hydrogen[2][]{\node[#1] (H#2) {H};}
  11. \newcommand*\oxygen[2][] {\node[#1] (O#2) {O};}
  12. \newcommand*\plus[2][45] {\node at (#2.#1) {+};}
  13.  
  14. \begin{document}
  15.  
  16. \begin{tikzpicture}[node distance=\bondlength]
  17. \carbon{1}
  18. \carbon[right=of C1]{2}
  19. \hydrogen[above=of C1]{1}
  20. \hydrogen[below=of C1]{2}
  21. \hydrogen[left=of C1]{3}
  22. \oxygen[below right=\bondlength of C2]{1}
  23. \hydrogen[right=of O1]{4}
  24. \plus {C1}
  25. \bond {C1} {C2}
  26. \bond {C1} {H1}
  27. \bond {C1} {H2}
  28. \bond {C1} {H3}
  29. \bond {C2} {O1}
  30. \bond {O1} {H4}
  31. \end{tikzpicture}
  32.  
  33. \end{document}


Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Fri Oct 26, 2012 7:22 pm

ghostanime2001 wrote:How to set the distance between the bond and the atom in this example ? I have tried \setatomsep but it gives no difference. If you don't mind me asking, can the above example's syntax be simplified ?

In which example? Mine? What about just reading Section 2 of the chemfig manual and using \setbondoffset? m(
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1


Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 2 guests