Debian Codegraph @

texmf-dist/tex/latex/tikzorbital/tikzorbital.sty

raw
1 % -------------------
2 % Package tikzorbital
3 % -------------------
4 %
5 % This package provides several commands in order to draw atomic orbitals and
6 % molecular diagrams.
7 %
8 % Germain Vallverdu <[email protected]>
9 % 05 decembre 2012
10 % http://gvallver.perso.univ-pau.fr/
11 %
12 % Licence : LaTeX Project Public Licence
13 % http://www.latex-project.org/lppl.txt
14 %
15 % Feel free to contact me if you have any ideas, suggestions or bugs report !
16 %
17 % Change
18 % ------
19 % 27/02/2013 : add -px, -py, -pz orbital type
20 % 05/03/2015 : add satom macro, with scaling options for each lobe
21 %
22 % -----------------------------------------------------------------------------
23 \NeedsTeXFormat{LaTeX2e}
24 \ProvidesPackage{tikzorbital}[2012/12/05 draw atomic orbitals and molecular diagrams with tikz]
25 % -----------------------------------------------------------------------------
26
27 \RequirePackage{tikz}
28 \usetikzlibrary{shapes}
29 \RequirePackage{ifthen}
30
31 \pgfdeclarelayer{background}
32 \pgfdeclarelayer{main}
33 \pgfdeclarelayer{foreground}
34 \pgfsetlayers{background,main,foreground}
35
36 % -----------------------------------------------------------------------------
37 % keys in order to submit tikz command to macro
38 % -----------------------------------------------------------------------------
39 \pgfkeys{/tikz/.cd,
40 execute style/.style = {#1},
41 execute macro/.style = {execute style/.expand once=#1}
42 }
43
44 % -----------------------------------------------------------------------------
45 % commande \drawLevel[key = value]{name}
46 % -----------------------------------------------------------------------------
47 % draw a level with a given name in order to draw molecular diagrams
48 %
49 % argument
50 % name : base name of level anchor.
51 %
52 % options
53 % elec : Number of electrons : up, down, updown or pair
54 % pos : left position of the level
55 % width : level widht
56 % style : level style (a tikzstyle)
57 % spinstyle : style of arrows which represents electrons (a tikzstyle)
58 % spinlength : length of spin arrows
59 % -----------------------------------------------------------------------------
60
61 \pgfkeys{/tikzorbital/drawLevel/.cd,
62 % number of electron in the level : up, down, updown or pair
63 elec/.store in = \drawLevel@elec,
64 elec/.default = no,
65 % position of the left anchor of the level
66 pos/.store in = \drawLevel@pos,
67 pos/.default = {(0,0)},
68 % width of levels
69 width/.store in = \drawLevel@width,
70 width/.default = 2,
71 % style of levels
72 style/.store in = \drawLevel@style,
73 style/.default = {line width = 2pt, color = black!80, line cap = round},
74 % style of arrows
75 spinstyle/.store in = \drawLevel@spinstyle,
76 spinstyle/.default = {very thick, color = red!80, -stealth},
77 % length of spin arrows
78 spinlength/.store in = \drawLevel@spinlength,
79 spinlength/.default = 1,
80 % execute options
81 elec, pos, width, style, spinstyle, spinlength
82 }
83
84 % the drawLevel command
85 \newcommand{\drawLevel}[2][]{%
86 \begingroup
87 \pgfkeys{/tikzorbital/drawLevel/.cd, #1}
88 \def\drawLevel@name{#2}
89
90 \draw[execute macro = \drawLevel@style]
91 \drawLevel@pos
92 node (left \drawLevel@name) {}
93 -- ++ (\drawLevel@width, 0)
94 node (right \drawLevel@name) {}
95 node[pos=0.5] (middle \drawLevel@name) {}
96 node[pos=0.3] (pos1) {}
97 node[pos=0.7] (pos2) {};
98
99 \ifthenelse{\equal{\drawLevel@elec}{updown} \or \equal{\drawLevel@elec}{pair}}{
100 \draw[execute macro = \drawLevel@spinstyle]
101 (pos1.center) ++ (0,-\drawLevel@spinlength/2) --
102 ++ (0,\drawLevel@spinlength);
103 \draw[execute macro = \drawLevel@spinstyle]
104 (pos2.center) ++ (0, \drawLevel@spinlength/2) --
105 ++ (0,-\drawLevel@spinlength);
106 }{
107 \ifthenelse{\equal{\drawLevel@elec}{up}}{
108 \draw[execute macro = \drawLevel@spinstyle]
109 (middle #2.center) ++ (0,-\drawLevel@spinlength/2) --
110 ++ (0,\drawLevel@spinlength);
111 }{
112 \ifthenelse{\equal{\drawLevel@elec}{down}}{
113 \draw[execute macro = \drawLevel@spinstyle]
114 (middle #2.center) ++(0,\drawLevel@spinlength/2) --
115 ++ (0,-\drawLevel@spinlength);
116 }{
117 }
118 }
119 }
120 \endgroup
121 }
122
123 % -----------------------------------------------------------------------------
124 % some customization of orbital
125 % -----------------------------------------------------------------------------
126
127 % inner color for orbital filling
128 \colorlet{innerColor}{black!10}
129
130 % color for orbital drawing
131 \colorlet{drawColor}{black!80}
132
133 % more style for lobe orbital drawing
134 \newcommand{\setOrbitalDrawing}[1]{\def\orbitalDrawing{#1}}
135 \setOrbitalDrawing{thick}
136
137 % -----------------------------------------------------------------------------
138 % inner \@alobe command
139 % -----------------------------------------------------------------------------
140 % Draw one lobe of a p or d atomic orbital, at a given position with a given scale,
141 % color, rotation and opacity. Draw zero, one or two balls which represent electrons.
142 %
143 % arguments
144 % #1 : position
145 % #2 : rotation
146 % #3 : scale
147 % #4 : color
148 % #5 : number of electron
149 % #6 : opacity
150 % -----------------------------------------------------------------------------
151 \newcommand{\@alobe}[6]{
152 \begin{scope}[rotate around = {#2:#1}]
153 % draw orbital lobe
154 \begin{pgfonlayer}{background}
155 \draw[draw = drawColor, outer color = #4, inner color = innerColor,
156 opacity = #6, execute macro = \orbitalDrawing]
157 #1 .. controls ++ (#3, #3) and ++ (#3, - #3) .. #1;
158 \end{pgfonlayer}
159
160 %Coordinates of the electrons
161 \path #1 ++ (0.50 * #3, 0) node (e1) {};
162 \path #1 ++ (0.25 * #3, 0) node (e2) {};
163 \end{scope}
164
165 % Draw the electrons
166 \ifnum#5>0
167 \foreach \n in {1,...,#5} {
168 \shade[ball color = #4] (e\n) circle (1mm);
169 }
170 \fi
171 }
172
173 % -----------------------------------------------------------------------------
174 % commande \orbital[key = value]{type}
175 % -----------------------------------------------------------------------------
176 % draw an atomic orbital of a given type.
177 %
178 % argument
179 % type : lobe, s, px, py, pz, dxz, dyz, dxy, dz2, dx2y2
180 %
181 % options
182 % pos : left position of the level
183 % pcolor : color of the positive lobe
184 % ncolor : color of the negative lobe
185 % scale : scaling factor
186 % opacity : opacity of the orbital
187 % rotate : rotate of the AO (lobe type only)
188 % nelec : number of electron (lobe type only)
189 % -----------------------------------------------------------------------------
190
191 % define options
192 \pgfkeys{/tikzorbital/orbital/.cd,
193 % position of the orbital
194 pos/.store in = \orbital@pos,
195 pos/.default = {(0,0)},
196 % color of the positive lobe
197 pcolor/.store in = \orbital@pcolor,
198 pcolor/.default = blue,
199 % color of the negative lobe
200 ncolor/.store in = \orbital@ncolor,
201 ncolor/.default = black!30,
202 % color for s type
203 color/.store in = \orbital@color,
204 color/.default = empty,
205 % scale factor
206 scale/.store in = \orbital@scale,
207 scale/.default = 1,
208 % opacity of the orbital
209 opacity/.store in = \orbital@opacity,
210 opacity/.default = 1.,
211 % lobe type options
212 % rotation of the orbital
213 rotate/.store in = \orbital@rotate,
214 rotate/.default = 0,
215 % number of electrons
216 nelec/.store in = \orbital@nelec,
217 nelec/.default = 0,
218 % execute options
219 pos, pcolor, ncolor, scale, opacity, rotate, nelec, color
220 }
221
222 % orbital command
223 \newcommand{\orbital}[2][]{
224 \begingroup
225 \pgfkeys{/tikzorbital/orbital/.cd, #1}
226
227 % orbital type
228 \def\orbital@type{#2}
229
230 % general style
231 \tikzstyle{base} = [draw = drawColor, thick, inner color = innerColor,
232 circle, opacity = \orbital@opacity,
233 execute macro = \orbitalDrawing]
234
235 % check if color was setted
236 \ifthenelse{\equal{\orbital@color}{empty}}{
237 \pgfkeys{/tikzorbital/orbital/.cd, color = \orbital@pcolor}
238 }{}
239
240 % draw the whished orbital
241 \ifthenelse{\equal{\orbital@type}{lobe}}{
242 \@alobe{\orbital@pos}{\orbital@rotate}{\orbital@scale}{\orbital@color}{\orbital@nelec}{\orbital@opacity}
243 }{
244 \ifthenelse{\equal{\orbital@type}{py}}{
245 \@alobe{\orbital@pos}{0}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
246 \@alobe{\orbital@pos}{180}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
247 }{
248 \ifthenelse{\equal{\orbital@type}{-py}}{
249 \@alobe{\orbital@pos}{180}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
250 \@alobe{\orbital@pos}{0}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
251 }{
252 \ifthenelse{\equal{\orbital@type}{pz}}{
253 \@alobe{\orbital@pos}{90}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
254 \@alobe{\orbital@pos}{270}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
255 }{
256 \ifthenelse{\equal{\orbital@type}{-pz}}{
257 \@alobe{\orbital@pos}{270}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
258 \@alobe{\orbital@pos}{90}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
259 }{
260 \ifthenelse{\equal{\orbital@type}{px}}{
261 \node[base, outer color = \orbital@ncolor, scale = \orbital@scale * 1.8,
262 xshift = 2pt, yshift = 2pt] at \orbital@pos {};
263 \node[base, outer color = \orbital@pcolor, scale = \orbital@scale * 1.8]
264 at \orbital@pos {};
265 }{
266 \ifthenelse{\equal{\orbital@type}{-px}}{
267 \node[base, outer color = \orbital@pcolor, scale = \orbital@scale * 1.8,
268 xshift = 2pt, yshift = 2pt] at \orbital@pos {};
269 \node[base, outer color = \orbital@ncolor, scale = \orbital@scale * 1.8]
270 at \orbital@pos {};
271 }{
272 \ifthenelse{\equal{\orbital@type}{dyz}}{
273 \@alobe{\orbital@pos}{45}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
274 \@alobe{\orbital@pos}{135}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
275 \@alobe{\orbital@pos}{225}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
276 \@alobe{\orbital@pos}{315}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
277 }{
278 \ifthenelse{\equal{\orbital@type}{dxz}}{
279 \@alobe{\orbital@pos}{80}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
280 \@alobe{\orbital@pos}{280}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
281 \@alobe{\orbital@pos}{100}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
282 \@alobe{\orbital@pos}{260}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
283 }{
284 \ifthenelse{\equal{\orbital@type}{dxy}}{
285 \@alobe{\orbital@pos}{10}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
286 \@alobe{\orbital@pos}{170}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
287 \@alobe{\orbital@pos}{350}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
288 \@alobe{\orbital@pos}{190}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
289 }{
290 \ifthenelse{\equal{\orbital@type}{dx2y2}}{
291 \begin{pgfonlayer}{background}
292 \node[base, outer color = \orbital@pcolor, scale = \orbital@scale * 1.8,
293 xshift = 2pt, yshift = 2pt] at \orbital@pos {};
294 \end{pgfonlayer}
295 \@alobe{\orbital@pos}{0}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
296 \@alobe{\orbital@pos}{180}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
297 \node[base, outer color = \orbital@pcolor, scale = \orbital@scale * 1.8]
298 at \orbital@pos {};
299 }{
300 \ifthenelse{\equal{\orbital@type}{dz2}}{
301 \@alobe{\orbital@pos}{270}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
302 \begin{pgfonlayer}{background}
303 \node[ellipse, minimum width = \orbital@scale * .8cm,
304 minimum height = \orbital@scale * .3cm, draw = drawColor,
305 inner color = innerColor, outer color = \orbital@ncolor,
306 execute macro = \orbitalDrawing]
307 at \orbital@pos {};
308 \end{pgfonlayer}
309 \@alobe{\orbital@pos}{90}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
310 }{
311 \ifthenelse{\equal{\orbital@type}{s}}{
312 \node[base, outer color = \orbital@color, scale = \orbital@scale * 1.8]
313 at \orbital@pos {};
314 }{
315 \node[red] at \orbital@pos {orbital type unknown};
316 }}}}}}}}}}}}}
317 \endgroup
318 }
319
320 %
321 % other possibility for dxy and dxz atomic orbital
322 % ------------------------------------------------
323 %
324 % dxz
325 % \begin{scope}[xshift = 2.2pt, yshift = 2pt]
326 % \@alobe{\orbital@pos}{90}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
327 % \@alobe{\orbital@pos}{270}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
328 % \end{scope}
329 % \@alobe{\orbital@pos}{90}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
330 % \@alobe{\orbital@pos}{270}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
331 %
332 % dxy
333 % \begin{scope}[xshift = 2.2pt, yshift = 2pt]
334 % \@alobe{\orbital@pos}{10}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
335 % \@alobe{\orbital@pos}{170}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
336 % \end{scope}
337 % \@alobe{\orbital@pos}{350}{\orbital@scale}{\orbital@pcolor}{0}{\orbital@opacity}
338 % \@alobe{\orbital@pos}{190}{\orbital@scale}{\orbital@ncolor}{0}{\orbital@opacity}
339 %
340 % -------------------------------------------------
341
342 % -----------------------------------------------------------------------------
343 % commande \atom[options]{lobes}
344 % -----------------------------------------------------------------------------
345 % quickly draw an atom with several orbital lobes around it.
346 % DEPRECATED, use satom insteed.
347 %
348 % argument
349 % lobes : A comma separated list lobe definition with
350 % color/rotation-angle/anchor/number of electrons
351 %
352 % options
353 % pos : position of the atom
354 % name : name of the atom, also used to label the node
355 % color : color of the atom
356 % opacity : opacity of the orbital
357 % scale : scaling factor
358 % -----------------------------------------------------------------------------
359
360 % define options
361 \pgfkeys{/tikzorbital/atom/.cd,
362 % position of the atom
363 pos/.store in = \atom@pos,
364 pos/.default = {(0,0)},
365 % atom name
366 name/.store in = \atom@name,
367 name/.default = X,
368 % color of the atom
369 color/.store in = \atom@color,
370 color/.default = green,
371 % opacity of the orbitals
372 opacity/.store in = \atom@opacity,
373 opacity/.default = .8,
374 % scaling factor
375 scale/.store in = \atom@scale,
376 scale/.default = 1.,
377 % execute options
378 pos, name, color, opacity, scale
379 }
380
381 % atom definition
382 \newcommand{\atom}[2][]{
383 \begingroup
384 \pgfkeys{/tikzorbital/atom/.cd, #1}
385 \colorlet{atomColor}{\atom@color}
386 \node[shape = circle, thick, inner sep = 0pt, minimum size = 1.5em,
387 draw = atomColor!40, color = atomColor!70!gray, fill = atomColor!20,
388 scale = \atom@scale]
389 at \atom@pos (\atom@name) {\atom@name};
390 \def\s{1.}
391 \foreach \acolor/\rot/\anchor/\Ne in {#2} {
392 \@alobe{(\atom@name.\anchor)}{\rot}{1.5*\atom@scale}{\acolor}{\Ne}{\atom@opacity}
393 }
394 \endgroup
395 }
396
397 % -----------------------------------------------------------------------------
398 % commande \satom[options]{lobes}
399 % -----------------------------------------------------------------------------
400 % quickly draw an atom with several orbital lobes around it
401 %
402 % argument
403 % lobes : A comma separated list lobe definition with
404 % color/rotation-angle/anchor/number of electrons/scale
405 %
406 % options
407 % pos : position of the atom
408 % name : name of the atom, also used to label the node
409 % color : color of the atom
410 % opacity : opacity of the orbital
411 % scale : global scaling factor
412 % -----------------------------------------------------------------------------
413
414 % define options
415 \pgfkeys{/tikzorbital/satom/.cd,
416 % position of the atom
417 pos/.store in = \satom@pos,
418 pos/.default = {(0,0)},
419 % atom name
420 name/.store in = \satom@name,
421 name/.default = X,
422 % color of the atom
423 color/.store in = \satom@color,
424 color/.default = green,
425 % opacity of the orbitals
426 opacity/.store in = \satom@opacity,
427 opacity/.default = .8,
428 % scaling factor
429 scale/.store in = \satom@scale,
430 scale/.default = 1.,
431 % execute options
432 pos, name, color, opacity, scale
433 }
434
435 % atom definition
436 \newcommand{\satom}[2][]{
437 \begingroup
438 \pgfkeys{/tikzorbital/satom/.cd, #1}
439 \colorlet{atomColor}{\satom@color}
440 \node[shape = circle, thick, inner sep = 0pt, minimum size = 1.5em,
441 draw = atomColor!40, color = atomColor!70!gray, fill = atomColor!20,
442 scale = \satom@scale]
443 at \satom@pos (\satom@name) {\satom@name};
444 \foreach \acolor/\rot/\anchor/\Ne/\s in {#2} {
445 \@alobe{(\satom@name.\anchor)}{\rot}{1.5*\s*\atom@scale}{\acolor}{\Ne}{\satom@opacity}
446 }
447 \endgroup
448 }
449
450 %% end of file %%