-
Notifications
You must be signed in to change notification settings - Fork 0
/
feynman.sty
630 lines (538 loc) · 22.2 KB
/
feynman.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
% -*- LaTeX -*-
% -*- coding: utf-8 -*-
%
% Alec Aivazis <[email protected]>
%
% (c) 2014-2015 all rights reserved
%
% A package for drawing feynman diagrams in LaTex
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{feynman}
[2015/02/01 v0.8 LaTeX package for drawing feynman diagrams with an interface similar to the one found at feynman.aivazis.com]
\RequirePackage{xcolor}
\RequirePackage{tikz}
\RequirePackage{xifthen}
\RequirePackage{kvoptions}
\RequirePackage{fp}
\usetikzlibrary{patterns, arrows, decorations.pathreplacing, decorations.markings}
% turn off the fp messages
\FPmessagesfalse
% treat @ like a normal letter
\makeatletter
%------------------------------------------------------------------------------
% configuration parameters
%------------------------------------------------------------------------------
% define the diagram configuration settings family
\SetupKeyvalOptions{family=diagram, prefix=diagram@}
\DeclareStringOption[in]{unit}
\DeclareStringOption[An Example Feynman Diagram]{title}
% define the propagator configuration settings family
\SetupKeyvalOptions{family=style, prefix=style@}
\DeclareStringOption[000000]{color}
\DeclareStringOption[1]{lineWidth}
\DeclareBoolOption[false]{endcaps}
\DeclareStringOption[1/3]{gluonWidth}
\DeclareStringOption[center]{location}
\DeclareStringOption{label}
\DeclareStringOption[0.5]{labelDistance}
\DeclareStringOption[0.5]{labelLocation}
\DeclareBoolOption[false]{showArrow}
\DeclareBoolOption[false]{flip}
%------------------------------------------------------------------------------
% the feynman diagram environment
%------------------------------------------------------------------------------
% define an enviroment to encompass a single diagram
\newenvironment{feynman}[1][]
% before the content
{
% load the diagram configuration settings
\setkeys{diagram}{#1}
% start the tikz environment
\begin{tikzpicture}[x=1\diagram@unit, y=1\diagram@unit]
}
% after the content
{
% close the tikz environment
\end{tikzpicture}
}
%------------------------------------------------------------------------------
% utility macros
%------------------------------------------------------------------------------
% convert a particular length to the designated unit
% i.e. \convertto{mm}{1pt}
\newcommand*{\convertto}[2]{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
% compute the length between two points
\def\calcLength(#1,#2)#3{
% compute the delta between two points
\pgfpointdiff{\pgfpointanchor{#1}{center}}
{\pgfpointanchor{#2}{center}}
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
% cast the deltas in the appropriate unit system
\FPeval\@temp@a{\pgfmath@tonumber{\pgf@xa}/72.27}
\FPeval\@temp@b{\pgfmath@tonumber{\pgf@ya}/72.27}
% store the sum of the squares
\FPeval\@temp@sum{(\@temp@a*\@temp@a+\@temp@b*\@temp@b)}
% take the square root of the result
\FProot{\FPMathLen}{\@temp@sum}{2}
% cut off at 5 decimal places
\FPround\FPMathLen\FPMathLen9\relax
% set the value of the macro we were given
\expandafter\edef\csname #3\endcsname{\FPMathLen}
}
% compute the angle between two points
\def\calcAngle(#1,#2)#3{
% compute the difference between the two points
\pgfpointdiff{\pgfpointanchor{#1}{center}}
{\pgfpointanchor{#2}{center}}
% extract the delta between the two points
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
% cast the deltas as numbers
\FPeval{\deltaX}{\pgfmath@tonumber{\pgf@xa}}
\FPeval{\deltaY}{\pgfmath@tonumber{\pgf@ya}}
% use pgf to compute atan2
\pgfmathparse{atan2(\deltaY, \deltaX)}
% set the angle to the result
\FPeval{\angle}{round(\pgfmathresult , 5)}
% set the value of the macro we were given
\expandafter\edef\csname #3\endcsname{\angle}
}
% shortcut macro to print an expression to the console
\newcommand*{\print}[1]{\typeout{#1}}
%------------------------------------------------------------------------------
% patterns and colors
%------------------------------------------------------------------------------
% declare the parameters for the pattern
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=1pt
}
% define the parton fill pattern
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{parton}
% declare the origin
{\pgfqpoint{0pt}{0pt}}
% define the variables in this coordinate system
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
% set up the pattern
{
\pgfsetcolor{\tikz@pattern@color}
\pgfsetlinewidth{\hatchthickness}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
\pgfusepath{stroke}
}
% define the arrow style
\tikzset{
% style to apply some styles to each segment of a path
on each segment/.style={
decorate,
decoration={
show path construction,
moveto code={},
lineto code={
\path [#1]
(\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
},
curveto code={
\path [#1] (\tikzinputsegmentfirst)
.. controls
(\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
..
(\tikzinputsegmentlast);
},
closepath code={
\path [#1]
(\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
},
},
},
% style to add an arrow in the middle of a path
mid arrow/.style={postaction={decorate,decoration={
markings,
mark=at position .5 with {\arrow[#1, scale=1.5]{stealth}}
}}},
}
% define a light gray to be used as a fill color for partons
\definecolor{light-gray}{gray}{.7}
%------------------------------------------------------------------------------
% text macros
%------------------------------------------------------------------------------
% overline with variable width (the second brace)
\newcommand*\anti[2][1.0]{
\overline{#2}
}
% overline with variable width (the second brace)
\newcommand*\txt[1]{
\textrm{#1}
}
%------------------------------------------------------------------------------
% propagator styles
%------------------------------------------------------------------------------
% fermions
\newcommand*{\fermion}[3][]{
% scope the configuration options
\begingroup
% load the configuration parameters
\setkeys{style}{#1}
% parse the color of the line
\definecolor{lineColor}{HTML}{\style@color}
% if they asked to draw the arrow
\ifstyle@showArrow
% draw the line with an arrow
\draw[color=lineColor, line width = \style@lineWidth, postaction={on each segment={mid arrow=lineColor}}] (#2) -- (#3);
% otherwise they did not ask to draw the arrow
\else
% draw the line
\draw[color=lineColor, line width = \style@lineWidth] (#2) -- (#3);
\fi
% draw the label
\drawLabel{#2}{#3}
\endgroup
}
% dashed propagators
\newcommand*{\dashed}[3][]{
% scope the configuration options
\begingroup
% load the configuration parameters
\setkeys{style}{#1}
% parse the color of the line
\definecolor{lineColor}{HTML}{\style@color}
% if they asked to draw the arrow
\ifstyle@showArrow
% draw the line with an arrow
\draw[dashed, color=lineColor, line width = \style@lineWidth, postaction={on each segment={mid arrow=lineColor}}] (#2) -- (#3);
% otherwise they did not ask to draw the arrow
\else
% draw the line
\draw[dashed, color=lineColor, line width = \style@lineWidth] (#2) -- (#3);
\fi
% draw the label
\drawLabel{#2}{#3}
\endgroup
}
% gluons
\newcommand*{\gluon}[3][]{
% scope the configuration options
\begingroup
% load the configuration parameters
\setkeys{style}{#1}
% create TikZ coordinates at the end points
\coordinate (start) at (#2);
\coordinate (finish) at (#3);
% store the distance between the two points
\calcLength(start,finish){length} % stores it in a macro called \length
% store the angle between the two points
\calcAngle(start,finish){angle} % stores it in a macro called \angle
% turn the start coordinate into a pgf point
\pgfpointanchor{start}{center}
\FPeval{\startX}{\pgfmath@tonumber{\pgf@x}}
\FPeval{\startY}{\pgfmath@tonumber{\pgf@y}}
\FPeval{\gluonWidth}{\style@gluonWidth}
% store a value for half of the gluonWidth
\FPeval{\halfWidth}{\gluonWidth/2}
% store the closest whole number of full periods
\FPeval{\nLoops}{round((\length / \gluonWidth ) - 1, 0)}
\FPeval{\totalLength}{(\nLoops+1) * \gluonWidth}
\FPeval{\scale}{\length / \totalLength}
% parse the color of the line
\definecolor{lineColor}{HTML}{\style@color}
% if we dont fit a whole loop between the two points
\ifthenelse{\equal{\nLoops}{0}}{
% use a fermion line instead
\fermion{#2}{#3}
% we fit at least one loop between the two points
}{
% if they want the end caps (to draw the right kind of line)
\ifstyle@endcaps
% if they asked to flip the loops
\ifstyle@flip
% set the maximum value to be negative
\FPeval{\ymax}{0-\halfWidth}
\FPeval{\ymin}{\halfWidth}
% otherwise draw the loops right side up
\else
% set the maximum value to be positive
\FPeval{\ymax}{\halfWidth}
\FPeval{\ymin}{0-\halfWidth}
\fi
% style the opening end cap
\draw[color=lineColor, line width = \style@lineWidth, yshift=\startY, xshift=\startX,
rotate=\angle, xscale=\scale, yscale=\scale]
% draw the opening end cap
(0,0) .. controls (\halfWidth, 0) and (0, \ymin) .. (\halfWidth, \ymin);
% for each loop that we have to draw
\foreach \loopNumber in {1,...,\nLoops}{
% compute the starting location of the loop
\FPeval{\x}{\loopNumber * \gluonWidth - \halfWidth}
% compute the half way point of the loop
\FPeval{\xMid}{\halfWidth + \x}
% compute the endpoint of the loop
\FPeval{\xFinal}{\gluonWidth + \x}
% style the first half of the loop
\draw[color=lineColor, line width = \style@lineWidth, yshift=\startY, xshift=\startX,
rotate=\angle, xscale=\scale, yscale=\scale]
% draw the first half of the loop
(\x, \ymin) .. controls (\xFinal, \ymin) and (\xFinal, \ymax) .. (\xMid, \ymax);
% style the second half of the loop
\draw[color=lineColor, line width = \style@lineWidth, yshift=\startY, xshift=\startX,
rotate=\angle, xscale=\scale, yscale=\scale]
% draw the second half of the loop
(\xMid, \ymax) .. controls (\x, \ymax) and (\x, \ymin) .. (\xFinal, \ymin);
}
% compute the locations for the closing end cap
\FPeval{\finalStart}{(\nLoops+1) * \gluonWidth - \halfWidth}
\FPeval{\finalHalf}{\finalStart + \halfWidth}
\FPeval{\finalStop}{\finalStart + \halfWidth}
% style the closing end cap
\draw[color=lineColor, line width = \style@lineWidth, yshift=\startY, xshift=\startX,
rotate=\angle, xscale=\scale, yscale=\scale]
% draw the closing end cap
(\finalStart, \ymin) .. controls (\finalStop, \ymin) and (\finalStart, 0) .. (\finalStop, 0);
% they did not want end caps
\else
% if they asked for the gluons to be flipped
\ifstyle@flip
% store a value for the height of the gluon loop
\FPeval{\height}{0-\gluonWidth}
\else
% store a value for the height of the gluon loop
\FPeval{\height}{\gluonWidth}
\fi
% for each loop that we have to draw
\foreach \loopNumber in {0,...,\nLoops}{
% compute the starting location of the loop
\FPeval{\x}{\loopNumber * \gluonWidth}
% compute the midpoint of the loop
\FPeval{\xMid}{\halfWidth + \x}
% compute the endpoint of the loop
\FPeval{\xFinal}{\gluonWidth + \x}
% style the first half of the loop
\draw[color=lineColor, line width = \style@lineWidth, yshift=\startY, xshift=\startX,
rotate=\angle, xscale=\scale, yscale=\scale]
% draw the first half of the loop
(\x,0) .. controls (\xFinal, 0) and (\xFinal, \height) .. (\xMid, \height);
% style the second half of the loop
\draw[color=lineColor, line width = \style@lineWidth, yshift=\startY, xshift=\startX,
rotate=\angle, xscale=\scale, yscale=\scale]
% draw the second half of the loop
(\xMid,\height) .. controls (\x, \height) and (\x, 0) .. (\xFinal, 0);
}
\fi
}
% draw the label
\drawLabel{#2}{#3}
\endgroup
}
% electroweak lines
\newcommand*{\electroweak}[3][]{
% scope the configuration options
\begingroup
% load the configuration parameters
\setkeys{style}{#1}
% create TikZ coordinates at the end points
\coordinate (start) at (#2);
\coordinate (finish) at (#3);
% store the distance between the two points
\calcLength(start,finish){length} % stores it in a macro called \length
% store the angle between the two points
\calcAngle(start,finish){angle} % stores it in a macro called \angle
% turn the start coordinate into a pgf point
\pgfpointanchor{start}{center}
\FPeval{\startX}{\pgfmath@tonumber{\pgf@x}}
\FPeval{\startY}{\pgfmath@tonumber{\pgf@y}}
\FPeval{\period}{\style@gluonWidth}
% store a value for half of the gluonWidth
\FPeval{\halfPeriod}{\period/2}
% store the closest whole number of full periods
\FPeval{\nLoops}{round((\length / \period ) - 1, 0)}
\FPeval{\totalLength}{(\nLoops+1) * \period}
\FPeval{\scale}{\length / \totalLength}
% parse the color of the line
\definecolor{lineColor}{HTML}{\style@color}
% if we dont fit a whole loop between the two points
\ifthenelse{\equal{\nLoops}{0}}{
% use a fermion line instead
\fermion{#2}{#3}
% we fit at least one loop between the two points
}{
% store the amplitude of the curve
\FPeval{\amplitude}{\period * 3 / 2}
% save the maximum height of the curve
\FPeval{\ymax}{\amplitude}
% save the minimum value of the curve
\FPeval{\ymin}{0-\amplitude}
% store the minimum height
% for each period that we have to draw
\foreach \loopNumber in {0,...,\nLoops}{
% compute the starting location
\FPeval{\x}{\loopNumber * \period}
% compute the midpoint
\FPeval{\xMid}{\halfPeriod + \x}
% compute the endpoint
\FPeval{\xFinal}{\period + \x}
% style the period
\draw[color=lineColor, line width = \style@lineWidth, yshift=\startY, xshift=\startX,
rotate=\angle, xscale=\scale, yscale=\scale]
% place the period
(\x,0) .. controls (\xMid, \ymin) and (\xMid, \ymax) .. (\xFinal, 0);
}
}
% draw the label
\drawLabel{#2}{#3}
\endgroup
}
% gluinos
\newcommand*{\gluino}[3][]{
% scope the configuration options
\begingroup
% load the configuration parameters
\setkeys{style}{#1}
% enforce that endcaps is true
\setkeys{style}{endcaps=true}
% draw a fermion between the two points
\fermion{#2}{#3}
% draw a gluon between the two points
\gluon{#2}{#3}
% draw the label
\drawLabel{#2}{#3}
\endgroup
}
% sfermions
\newcommand*{\sfermion}[3][]{
% scope the configuration options
\begingroup
% load the configuration parameters
\setkeys{style}{#1}
% enforce that endcaps is true
\setkeys{style}{endcaps=true}
% draw a fermion between the two points
\fermion{#2}{#3}
% draw a gluon between the two points
\electroweak{#2}{#3}
% draw the label
\drawLabel{#2}{#3}
\endgroup
}
%------------------------------------------------------------------------------
% other objects
%------------------------------------------------------------------------------
% circle centered at #2 with radius #3
\newcommand*{\parton}[3][]{
% scope the configuration options
\begingroup
% load the configuration parameters
\setkeys{style}{#1}
% parse the color of the line
\definecolor{lineColor}{HTML}{\style@color}
% style the circle
\draw[color=lineColor, line width = \style@lineWidth, pattern=parton,
pattern color=light-gray]
% draw the circle
(#2) circle (#3 \diagram@unit);
\endgroup
}
% text a located at #2 with content #3
\newcommand*{\floatingText}[3][]{
% scope the configuration options
\begingroup
% load the configuration parameters
\setkeys{style}{#1}
% parse the color of the line
\definecolor{color}{HTML}{\style@color}
% create a node at the location with the given contents
\draw[text=color, anchor=\style@location] (#2) node {#3};
\endgroup
}
% draw a label using the coordinate system along the line joining #1 and #2
\newcommand*{\drawLabel}[2]{
% if they provided a value for the label
\ifthenelse{\equal{\style@label}{}}{}{
% create TikZ coordinates at the end points
\coordinate (start) at (#1);
\coordinate (finish) at (#2);
% compute the difference between the two points
\pgfpointdiff{\pgfpointanchor{start}{center}}
{\pgfpointanchor{finish}{center}}
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
% cast the deltas in the appropriate unit system
\FPeval\dX{\pgfmath@tonumber{\pgf@xa}/72.27}
\FPeval\dY{\pgfmath@tonumber{\pgf@ya}/72.27}
% compute the location of the starting coordinate
\pgfpointanchor{start}{center}
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
\FPeval{\@temp@x1}{round(\pgfmath@tonumber{\pgf@xa}/72.27, 5)}
\FPeval{\@temp@y1}{round(\pgfmath@tonumber{\pgf@ya}/72.27, 5)}
% store local copies of the target position
\FPeval{\labelLocation}{\style@labelLocation}
\FPeval{\labelDistance}{0+\style@labelDistance}
% compute the location along the line where we belong
\FPeval{\@temp@locX}{round(\@temp@x1 + \labelLocation * \dX, 5)}
\FPeval{\@temp@locY}{round(\@temp@y1 + \labelLocation * \dY, 5)}
% if the change in y is zero (infinite slope)
\ifthenelse{\equal{\dY}{0}}{
\FPeval{\labelX}{\@temp@locX}
\FPeval{\labelY}{\@temp@locY+\labelDistance}
\def\loc{right}
% otherwise we are safe to compute the slope
}{
% compute the slope of the perpendicular line
\FPeval{\perpSlope}{0 - \dX/\dY}
% the points that are perpedicular to the line a distance r away satisfy
% y = mx
% x^2 + y^2 = r
% compute the x that satisfies this equation
\FPeval{\x2}{(0+\labelDistance) * (0+\labelDistance) / (1 + (\perpSlope * \perpSlope))}
\FProot{\x}{\x2}{2}
% compute the y that satisfies this equation
\FPeval{\y}{\x * \perpSlope}
\newdimen \distance
\distance = \labelDistance pt
\newdimen \slope
\slope = \perpSlope pt
% if the perpendicular line has positive slope
\ifthenelse{\slope > 0 pt \OR \slope = 0 pt}{
% if they are a positive distance away
\ifthenelse{\distance > 0 pt}{
\FPeval{\labelX}{\@temp@locX + \x}
\FPeval{\labelY}{\@temp@locY + \y}
\def\loc{right}
% otherwise they are a negative distance away
}{
\FPeval{\labelX}{\@temp@locX - \x}
\FPeval{\labelY}{\@temp@locY - \y}
\def\loc{right}
}
% otherwise the perpendicular line as negative slope
}{
% if the label is a positive distance away
\ifthenelse{\distance > 0 pt}{
\FPeval{\labelX}{\@temp@locX - \x}
% note: the y is negative so we subtract even though we want to add
\FPeval{\labelY}{\@temp@locY - \y}
\def\loc{right}
% otherwise the label is a negative distance from the line
}{
\FPeval{\labelX}{\@temp@locX + \x}
% note: the y is negative so we add even though we want to subtract
\FPeval{\labelY}{\@temp@locY + \y}
\def\loc{right}
}
}
}
% draw the label
\draw[\loc] (\labelX,\labelY) node {$\style@label$};
}
}
%------------------------------------------------------------------------------
% cleanup
%------------------------------------------------------------------------------
% turn @ back into a special character
\makeatother
\endinput
% end of file