3

I'm drawing a molecule (p-anisaldehyde) in which one of the bonds is at a 60 degree angle and the atom at the end is inside \Lewis{}. I would like to have the two electron groups on the end atom to be slightly turned counter-clockwise, but right now my only options are to put them at 45 degree angles. Here's the code that I'm using (the crazy amount of packages is because I was experimenting with other things and now I don't know which ones are important at this point...):

\documentclass[twoside, titlepage]{article}
\usepackage[fleqn]{amsmath}
\usepackage{fancyhdr}
\usepackage{fixltx2e}
\usepackage{fullpage}
\usepackage{fancybox}
\usepackage{hyperref}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{float}
\usepackage[version=4]{mhchem} 
\usepackage{chemfig}
\usepackage{gensymb}
\usepackage{array}
\begin{document}

\begin{center}

\chemfig{\Lewis{4:6:,O}(-[3]C(<[1]H)(<:[3]H)(-[5]H))-C**6(-C(-H)-C(-H)-C(-(=[:60]\Lewis{0:2:,O})-[:-60]H)-C(-H)-C(-H)-)}

\end{center}

\end{document}

And here's the output (the red circle indicates the lewis structure I'm talking about, it's not done by the code): p-anisaldehyde

Basically, my problem is that I can't put any other angle except: 0, 1, 2, 3, 4, 5, 6, and 7 into \Lewis{}.

Sorry if I'm doing something obviously wrong or if I'm kind of vague, it's my first time making a post, so suggestions on posting are also welcome! :-)

Thanks!

5

Chemfig documentation has an example, in French, about making a customized lewis macro accepting an arbitrary angle in degrees. I am reproducing it irresponsibly translated to English. (I am a native Brazilian Portuguese speaker, so I don't master neither English nor French). This example appeared on page 45 (46th page as there is a cover):

\example*{Pairs at arbitrary angles}|\makeatletter
\newcommand\mylewis[3][2ex]{% #1=distance, #2=atom #3=list of angles
\tikzpicture[baseline,anchor=base]%
    \node[inner sep=0pt,outer sep=1pt,circle,overlay](atom@@)at(0,0){\phantom{#2}};%
    \node[inner sep=0pt,outer sep=0pt]at(0,0){#2};%
    \def\list@angle{#3,}%
    \loop
        \expandafter\grab@angle\list@angle\@nil
        \pgfextractx\CF@dim{\pgfpointanchor{atom@@}\current@angle}\edef\CF@dimax{\the\CF@dim}%
        \pgfextracty\CF@dim{\pgfpointanchor{atom@@}\current@angle}\edef\CF@dimay{\the\CF@dim}%
        \pgfmathparse{#1*sin(\current@angle)/2}\let\offset@xx\pgfmathresult
        \pgfmathparse{#1*cos(\current@angle)/2}\let\offset@yy\pgfmathresult
        \draw[line width=.4pt,overlay]
        (\CF@dimax-\offset@xx,\CF@dimay+\offset@yy)--(\CF@dimax+\offset@xx,\CF@dimay-\offset@yy);%
        \unless\ifx\@empty\list@angle
    \repeat
\endtikzpicture
}
\def\grab@angle#1,#2\@nil{\def\current@angle{#1}\def\list@angle{#2}}
\makeatother

Once defined, this macro can be used like this:

\mylewis{Cl}{15}\qquad \mylewis[3ex]{Cl}{-60}\qquad \mylewis[1.5ex]{Cl}{60,120,240,300}

In response to comment asking for dots instead of lines

One attempt to provide both mylewis and myLewis:

\makeatletter
\newcommand\mylewis[3][2ex]{% #1=distance, #2=atom #3=list of angles
\tikzpicture[baseline,anchor=base]%
\node[inner sep=0pt,outer sep=1pt,circle,overlay](atom@@)at(0,0){\phantom{#2}};%
\node[inner sep=0pt,outer sep=0pt]at(0,0){#2};%
\def\list@angle{#3,}%
\loop
    \expandafter\grab@angle\list@angle\@nil
    \pgfextractx\CF@dim{\pgfpointanchor{atom@@}\current@angle}\edef\CF@dimax{\the\CF@dim}%
    \pgfextracty\CF@dim{\pgfpointanchor{atom@@}\current@angle}\edef\CF@dimay{\the\CF@dim}%
    \pgfmathparse{#1*sin(\current@angle)/2}\let\offset@xx\pgfmathresult
    \pgfmathparse{#1*cos(\current@angle)/2}\let\offset@yy\pgfmathresult
    \draw[line width=.4pt,overlay]
    (\CF@dimax-\offset@xx,\CF@dimay+\offset@yy)--(\CF@dimax+\offset@xx,\CF@dimay-\offset@yy);%
    \unless\ifx\@empty\list@angle
\repeat
\endtikzpicture
}
\newcommand\myLewis[3][2ex]{% #1=distance, #2=atom #3=list of angles
\tikzpicture[baseline,anchor=base]%
\node[inner sep=0pt,outer sep=1pt,circle,overlay](atom@@)at(0,0){\phantom{#2}};%
\node[inner sep=0pt,outer sep=0pt]at(0,0){#2};%
\def\list@angle{#3,}%
\loop
    \expandafter\grab@angle\list@angle\@nil
    \pgfextractx\CF@dim{\pgfpointanchor{atom@@}\current@angle}\edef\CF@dimax{\the\CF@dim}%
    \pgfextracty\CF@dim{\pgfpointanchor{atom@@}\current@angle}\edef\CF@dimay{\the\CF@dim}%
    \pgfmathparse{#1*sin(\current@angle)/2}\let\offset@xx\pgfmathresult
    \pgfmathparse{#1*cos(\current@angle)/2}\let\offset@yy\pgfmathresult
    \draw[fill,black]
    (\CF@dimax-\offset@xx,\CF@dimay+\offset@yy)circle(0.15ex)%
    (\CF@dimax+\offset@xx,\CF@dimay-\offset@yy)circle(0.15ex);%
    \unless\ifx\@empty\list@angle
\repeat
\endtikzpicture
}
\def\grab@angle#1,#2\@nil{\def\current@angle{#1}\def\list@angle{#2}}
\makeatother

This way, you can use mylewis as before, but you now have also myLewis which appears to be what you really needed.

\mylewis{Cl}{15}\qquad \mylewis[3ex]{Cl}{-60}\qquad \mylewis[1.5ex]{Cl}{60,120,240,300}

\myLewis{Cl}{15}\qquad \myLewis[3ex]{Cl}{-60}\qquad \myLewis[1.5ex]{Cl}{60,120,240,300}

\myLewis[.8ex]{Cl}{15}\qquad \myLewis[.8ex]{Cl}{-60}\qquad \myLewis[.8ex]{Cl}{60,120,240,300}

Please note that I have not delved into much detail over chemfig original Lewis implementation, so I have only made myLewis based on the provided mylewis example. Their difference is simply a change making the end points of the line become the center of little circles.

  • 1
    Thanks, this is great!!! I would have never thought of looking into the French example! Although, do you know of any way of making the lines the function produces two dots (like in my example) instead? – nbingo Dec 27 '15 at 4:19
  • @nbingo I tried to add a new macro myLewis in which the end points of the lines become dots. I hope you can tinker with this idea and get what you need. – Ailton Andrade de Oliveira Dec 30 '15 at 3:38
  • 1
    I would use \newcommand\myLewis[3][\CF@lewis@dist]{ so the dots are drawn at the distance specified by \setlewisdist. – Arch Stanton Jan 4 '16 at 23:08
  • @ArchStanton Great suggestion! I will edit the answer accordingly. – Ailton Andrade de Oliveira Jan 4 '16 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.