org.org (37935B)
1 # -*- org-html-link-up: "http://home.fnal.gov/~neilsen/"; org-html-link-home: "http://home.fnal.gov/~neilsen" -*- 2 #+TITLE: Emacs org-mode examples and cookbook 3 #+AUTHOR: Eric H. Neilsen, Jr. 4 #+EMAIL: neilsen@fnal.gov 5 #+DATE: 6 #+LANGUAGE: en 7 #+INFOJS_OPT: view:showall toc:t ltoc:t mouse:underline path:http://orgmode.org/org-info.js 8 #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../css/notebook.css" /> 9 #+LaTeX_CLASS: ehnaas2col 10 #+EXPORT_SELECT_TAGS: export 11 #+EXPORT_EXCLUDE_TAGS: noexport 12 #+OPTIONS: H:4 num:4 toc:nil \n:nil @:t ::t |:t ^:{} _:{} *:t 13 #+STARTUP: showall 14 15 * Introduction 16 17 This document provides examples of different things that can be done 18 in =emacs= =org-mode= files. This is *not* intended to be a 19 tutorial. The examples should provide a clue of what you need to look 20 up in the [[http://orgmode.org/#docs][org-mode manual]]. 21 22 * Header 23 24 The first set of lines of an =org-mode= file, each starting with =#+=, 25 configure =org-mode='s interpretation of the remainder of the file. 26 27 ** General metadata 28 29 An initial group sets the metadata used in any title pages, headers, 30 footers, etc. used by the various exporters: 31 32 #+NAME: orgmode-header-metadata 33 #+BEGIN_SRC org 34 #+TITLE: Emacs org-mode examples 35 #+AUTHOR: Eric H. Neilsen, Jr. 36 #+EMAIL: neilsen@fnal.gov 37 #+END_SRC 38 39 ** Common export parameters 40 41 Configure the =org-mode= tags for forcing inclusion of exclusion of 42 sections in exported documents 43 44 #+NAME: orgmode-header-exclude 45 #+BEGIN_SRC org 46 #+EXPORT_SELECT_TAGS: export 47 #+EXPORT_EXCLUDE_TAGS: noexport 48 #+END_SRC 49 50 Additional options handle interpretation of special characters in the 51 buffer, numbering of headings, etc. 52 53 #+NAME: orgmode-header-options 54 #+BEGIN_SRC org 55 #+OPTIONS: H:2 num:nil toc:nil \n:nil @:t ::t |:t ^:{} _:{} *:t TeX:t LaTeX:t 56 #+END_SRC 57 58 ** =emacs= options 59 60 THE =STARTUP= keyword sets how the buffer is displayed when the file 61 is opened in =emacs=: 62 63 #+NAME: orgmode-header-emacs 64 #+BEGIN_SRC org 65 #+STARTUP: showall 66 #+END_SRC 67 68 69 ** HTML export options 70 71 A few other are used exclusively by the =html= exporter: 72 73 #+NAME: orgmode-header-html 74 #+BEGIN_SRC org 75 #+LANGUAGE: en 76 #+INFOJS_OPT: view:showall toc:t ltoc:t mouse:underline path:http://orgmode.org/org-info.js 77 #+LINK_HOME: http://home.fnal.gov/~neilsen 78 #+LINK_UP: http://home.fnal.gov/~neilsen/notebook 79 #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../css/notebook.css" /> 80 #+END_SRC 81 82 The =LANGUAGE= keyword sets the =lang= option in the =html= 83 declaration. 84 85 The =INFOJS_OPT= keyword configures the [[http://orgmode.org/manual/JavaScript-support.html][org-info.js]], javascript used 86 to assist navigation of =org-mode= generated pages. 87 88 ** LaTeX export options 89 90 The LaTeX class and any LaTeX commands to be included at the head of 91 exported LaTeX files. For example, on my laptop the header looks like this: 92 93 #+NAME: orgmode-header-latex 94 #+BEGIN_SRC org 95 #+LaTeX_CLASS: smarticle 96 #+LaTeX_HEADER: \pdfmapfile{/home/neilsen/texmf/fonts/map/dvips/libertine/libertine.map} 97 #+LaTeX_HEADER: \usepackage[ttscale=.875]{libertine} 98 #+LaTeX_HEADER: \usepackage{sectsty} 99 #+LaTeX_HEADER: \sectionfont{\normalfont\scshape} 100 #+LaTeX_HEADER: \subsectionfont{\normalfont\itshape} 101 #+END_SRC 102 103 It looks a little different in my account on the DES cluster: 104 105 #+BEGIN_SRC org 106 #+LaTeX_CLASS: smarticle 107 #+LaTeX_HEADER: \pdfmapfile{/home/s1/neilsen/texmf/fonts/map/dvips/libertine/libertine.map} 108 #+LaTeX_HEADER: \usepackage{sectsty} 109 #+LaTeX_HEADER: \usepackage{libertine} 110 #+LaTeX_HEADER: \usepackage[T1]{fontenc} 111 #+LaTeX_HEADER: \sectionfont{\normalfont\scshape} 112 #+LaTeX_HEADER: \subsectionfont{\normalfont\itshape} 113 #+END_SRC 114 115 Of course, LaTeX should be installed, and for the above to work, so 116 must the libertine package, and the pdfmapfile must be set. 117 118 LaTeX installaction instruction can be found [[http://en.wikibooks.org/wiki/LaTeX/Installing_Extra_Packages][here]]. 119 120 ** An example full header 121 122 A typical header that I use for =org-mode= files: 123 124 #+BEGIN_SRC org :noweb yes 125 <<orgmode-header-metadata>> 126 <<orgmode-header-html>> 127 <<orgmode-header-latex>> 128 <<orgmode-header-exclude>> 129 <<orgmode-header-options>> 130 <<orgmode-header-emacs>> 131 #+END_SRC 132 133 * Setting per-file =emacs= variables 134 135 Follow the [[http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html][instructions in the emacs manual]]; begin the file with a 136 line of the form: 137 138 # -*- org-html-link-up: "http://decam03.fnal.gov:8080/notes/neilsen/"; org-html-link-home: "http://home.fnal.gov/~neilsen" -*- 139 140 #+BEGIN_SRC org 141 # -*- foo: "bar"; baz: "ham" -*- 142 #+END_SRC 143 144 For example, to set the "Up" and "Home" links for an org-mode file, 145 begin it with: 146 #+BEGIN_SRC org 147 # -*- org-html-link-up: "http://decam03.fnal.gov:8080/notes/neilsen/"; org-html-link-home: "http://home.fnal.gov/~neilsen" -*- 148 #+END_SRC 149 150 * Subversion headers and introduction 151 152 If you wish to have subversion keyword substitution, it can be done like this: 153 154 #+BEGIN_SRC org 155 - Revision :: $Revision: 1.3 $ 156 - Date :: $Date: 2013/05/17 15:19:53 $ 157 - Source :: $Source: /Users/neilsen/Documents/CTIOTime/RCS/ctio_time.org,v $ 158 #+END_SRC 159 160 The result looks like this: 161 162 - Revision :: $Revision: 1.3 $ 163 - Date :: $Date: 2013/05/17 15:19:53 $ 164 - Source :: $Source: /Users/neilsen/Documents/CTIOTime/RCS/ctio_time.org,v $ 165 166 * =ditaa= figures 167 168 ** About =ditaa= 169 170 =dataa= generates figures from ASCII "art". Examples of =ditaa= syntax 171 can be found [[http://ditaa.sourceforge.net/][here]]. 172 173 ** Simple boxes 174 175 #+BEGIN_SRC org 176 #+begin_src ditaa :file ditaa-simpleboxes.png 177 +---------+ 178 | | 179 | Foo | 180 | | 181 +----+----+---+ 182 |Bar |Baz | 183 | | | 184 +----+--------+ 185 ,#+end_src 186 #+END_SRC 187 188 #+begin_src ditaa :file ditaa-simpleboxes.png 189 +---------+ 190 | | 191 | Foo | 192 | | 193 +----+----+---+ 194 |Bar |Baz | 195 | | | 196 +----+--------+ 197 #+end_src 198 199 #+RESULTS: 200 [[file:ditaa-simpleboxes.png]] 201 202 ** Unseparated boxes 203 204 #+BEGIN_SRC org 205 #+begin_src ditaa :file ditaa-simpleboxes-unsep.png :cmdline -E 206 +---------+ 207 | | 208 | Foo | 209 | | 210 +----+----+---+ 211 |Bar |Baz | 212 | | | 213 +----+--------+ 214 ,#+end_src 215 #+END_SRC 216 217 #+begin_src ditaa :file ditaa-simpleboxes-unsep.png :cmdline -E 218 +---------+ 219 | | 220 | Foo | 221 | | 222 +----+----+---+ 223 |Bar |Baz | 224 | | | 225 +----+--------+ 226 #+end_src 227 228 #+RESULTS: 229 [[file:ditaa-simpleboxes-unsep.png]] 230 231 ** Connected elements with colors 232 233 #+BEGIN_SRC org 234 #+begin_src ditaa :file ditaa-seqboxes.png 235 +------+ +-----+ +-----+ +-----+ 236 |{io} | |{d} | |{s} | |cBLU | 237 | Foo +---+ Bar +---+ Baz +---+ Moo | 238 | | | | | | | | 239 +------+ +-----+ +--+--+ +-----+ 240 | 241 /-----\ | +------+ 242 | | | | c1AB | 243 | Goo +------+---=--+ Shoo | 244 \-----/ | | 245 +------+ 246 ,#+end_src 247 #+END_SRC 248 249 #+begin_src ditaa :file ditaa-seqboxes.png 250 +------+ +-----+ +-----+ +-----+ 251 |{io} | |{d} | |{s} | |cBLU | 252 | Foo +---+ Bar +---+ Baz +---+ Moo | 253 | | | | | | | | 254 +------+ +-----+ +--+--+ +-----+ 255 | 256 /-----\ | +------+ 257 | | | | c1AB | 258 | Goo +------+---=--+ Shoo | 259 \-----/ | | 260 +------+ 261 #+end_src 262 263 #+RESULTS: 264 [[file:ditaa-seqboxes.png]] 265 266 * UML diagrams with =PlantUML= 267 268 ** Class diagrams 269 270 This: 271 #+BEGIN_SRC org 272 #+begin_src plantuml :file class_diagram.png 273 skinparam monochrome true 274 FitsHdu <|-- PrimaryHdu 275 FitsHdu <|-- ExtensionHdu 276 277 FitsHdu : header 278 FitsHdu : getHeaderKeyword() 279 280 ExtensionHdu <|-- ImageHdu 281 ImageHdu : image 282 ImageHdu : getPixel(row, column) 283 284 ExtensionHdu <|-- BinaryTableHdu 285 BinaryTableHdu : table 286 BinaryTableHdu : getRow(row) 287 BinaryTableHdu : getColumn(column) 288 ,#+end_src 289 #+END_SRC 290 291 gives this: 292 #+begin_src plantuml :file class_diagram.png 293 skinparam monochrome true 294 FitsHdu <|-- PrimaryHdu 295 FitsHdu <|-- ExtensionHdu 296 297 FitsHdu : header 298 FitsHdu : getHeaderKeyword() 299 300 ExtensionHdu <|-- ImageHdu 301 ImageHdu : image 302 ImageHdu : getPixel(row, column) 303 304 ExtensionHdu <|-- BinaryTableHdu 305 BinaryTableHdu : table 306 BinaryTableHdu : getRow(row) 307 BinaryTableHdu : getColumn(column) 308 #+end_src 309 310 #+RESULTS: 311 [[file:class_diagram.png]] 312 313 314 ** Sequences diagrams 315 316 This: 317 318 #+BEGIN_SRC org 319 #+begin_src plantuml :file sequence_diagram.png 320 skinparam monochrome true 321 Foo -> Bar: synchronous call 322 Foo ->> Bar: asynchronous call 323 ,#+end_src 324 #+END_SRC 325 326 #+RESULTS: 327 [[file:sequence_diagram.png]] 328 329 results in this: 330 331 #+begin_src plantuml :file sequence_diagram.png 332 skinparam monochrome true 333 Foo -> Bar: synchronous call 334 Foo ->> Bar: asynchronous call 335 #+end_src 336 337 #+RESULTS: 338 [[file:sequence_diagram.png]] 339 340 * Symbolic algebra with =GNU calc= 341 342 Full documentation on how to use =GNU calc= can be found 343 [[http://www.gnu.org/software/emacs/manual/html_node/calc/Algebra.html][here]]. Same examples: 344 345 ** Calculation using a formula 346 347 Starting with this: 348 349 #+BEGIN_SRC org 350 #+BEGIN_SRC calc :var x=5 :var y=2 351 2+a*x**y 352 ,#+END_SRC 353 #+END_SRC 354 355 If you place the cursor on the =#+BEGIN_SRC= and hit ctrl-c /twice/, 356 it will produce a "results" section thus: 357 358 #+BEGIN_SRC org 359 #+BEGIN_SRC calc :var x=5 :var y=2 360 2+a*x**y 361 ,#+END_SRC 362 363 #+RESULTS: 364 : 25 a + 2 365 #+END_SRC 366 367 Which results in this if the exported document 368 369 #+BEGIN_SRC calc :var x=5 :var y=2 370 2+a*x**y 371 #+END_SRC 372 373 #+RESULTS: 374 : 25 a + 2 375 376 ** Exporting GNU calc input as well as output 377 378 If you want the original formula in the exported document, you need to 379 add an =:exports both= flag, thus: 380 381 #+BEGIN_SRC org 382 #+BEGIN_SRC calc :exports both 383 x*2+x=4 384 ,#+END_SRC 385 386 #+results: 387 : 3 x = 4 388 #+END_SRC 389 390 Which results in this: 391 392 #+BEGIN_SRC calc :exports both 393 x*2+x=4 394 #+END_SRC 395 396 #+results: 397 : 3 x = 4 398 399 ** Solving formula 400 401 =GNU calc= has many additional capabilities. It can be used to solve formula: 402 403 #+BEGIN_SRC org 404 #+BEGIN_SRC calc :exports both 405 fsolve(x*2+x=4,x) 406 ,#+END_SRC 407 408 #+results: 409 : x = 1.33333333333 410 411 #+END_SRC 412 413 which exports to: 414 415 #+BEGIN_SRC calc :exports both 416 fsolve(x*2+x=4,x) 417 #+END_SRC 418 419 #+results: 420 : x = 1.33333333333 421 422 ** Solving systems of equations 423 424 #+BEGIN_SRC org 425 426 #+BEGIN_SRC calc 427 fsolve([x + y = a, x - y = b],[x,y]) 428 ,#+END_SRC 429 430 #+RESULTS: 431 : [x = a + (b - a) / 2, y = (a - b) / 2] 432 433 #+END_SRC 434 435 ** Inverting equations 436 437 #+BEGIN_SRC org 438 439 #+BEGIN_SRC calc :exports both 440 finv(sqrt(x),x) 441 ,#+END_SRC 442 443 #+results: 444 : x^2 445 446 #+END_SRC 447 448 ** Differentials 449 450 #+BEGIN_SRC org 451 452 #+BEGIN_SRC calc :exports both 453 deriv(sqrt(x),x) 454 ,#+END_SRC 455 456 #+RESULTS: 457 : 0.5 / sqrt(x) 458 459 #+END_SRC 460 461 ** Integration 462 463 #+BEGIN_SRC org 464 465 #+BEGIN_SRC calc :exports both 466 integ(x**2,x) 467 ,#+END_SRC 468 469 #+RESULTS: 470 : x^3 / 3 471 #+END_SRC 472 473 ** Taylor series 474 475 #+BEGIN_SRC org 476 #+BEGIN_SRC calc :exports both 477 taylor(sin(x),x,6) 478 ,#+END_SRC 479 480 #+RESULTS: 481 : 0.0174532925199 x - 8.86096155693e-7 x^3 + 1.34960162314e-11 x^5 482 #+END_SRC 483 484 ** Applying a formula repeatedly in =org-mode= 485 486 #+BEGIN_SRC org 487 #+name: myformula 488 #+BEGIN_SRC calc 489 2+a*x**y 490 ,#+END_SRC 491 492 #+BEGIN_SRC calc :noweb yes :var x=5 :var y=2 493 <<myformula>> 494 ,#+END_SRC 495 496 #+RESULTS: 497 : 25 a + 2 498 499 #+BEGIN_SRC calc :noweb yes :var x=10 :var y=2 500 <<myformula>> 501 ,#+END_SRC 502 503 #+RESULTS: 504 : 100 a + 2 505 #+END_SRC 506 507 You can accomplish roughly the same thing like this: 508 509 #+BEGIN_SRC org 510 511 #+NAME: mynewformula 512 ,#+BEGIN_SRC calc 513 2+a*x**y 514 ,#+END_SRC 515 516 ,#+CALL: mynewformula(x=10,y=2) 517 518 #+RESULTS: 519 : 100 a + 2 520 #+END_SRC 521 522 #+NAME: mynewformula 523 #+BEGIN_SRC calc 524 2+a*x**y 525 #+END_SRC 526 527 #+CALL: mynewformula(x=10,y=2) 528 529 #+RESULTS: 530 : 100 a + 2 531 532 The first mechanism is somewhat more versatile, as you can combine 533 multiple code blocks. 534 535 * Using =org-mode= as a spread sheet 536 ** Define one column using a formula in terms of others 537 538 #+BEGIN_SRC org 539 | airmass | zenith_seeing | delivered_seeing | 540 |---------+---------------+------------------| 541 | 1.3 | 0.95 | 1.1119612 | 542 | 1.3 | 1.0 | 1.1704854 | 543 | 1.3 | 1.1 | 1.2875340 | 544 | 1.3 | 1.2 | 1.4045825 | 545 | 1.3 | 1.25 | 1.4631068 | 546 | 1.3 | 1.3 | 1.5216311 | 547 | 1.3 | 1.5 | 1.7557281 | 548 | 1.3 | 1.8 | 2.1068738 | 549 | 1.2 | 1.8 | 2.0080811 | 550 | 1.3 | 2.0 | 2.3409709 | 551 #+TBLFM: $3=$2*($1**0.6) 552 #+END_SRC 553 554 results in this in the output: 555 556 | airmass | zenith_seeing | delivered_seeing | 557 |---------+---------------+------------------| 558 | 1.3 | 0.95 | 1.1119612 | 559 | 1.3 | 1.0 | 1.1704854 | 560 | 1.3 | 1.1 | 1.2875340 | 561 | 1.3 | 1.2 | 1.4045825 | 562 | 1.3 | 1.25 | 1.4631068 | 563 | 1.3 | 1.3 | 1.5216311 | 564 | 1.3 | 1.5 | 1.7557281 | 565 | 1.3 | 1.8 | 2.1068738 | 566 | 1.2 | 1.8 | 2.0080811 | 567 | 1.3 | 2.1 | 2.4580194 | 568 #+TBLFM: $3=$2*($1**0.6) 569 570 To recalculate the column, put the cursor on the =#+TBLFM= column and 571 hit ctrl-c /twice/. 572 ** Using an arbitrary code block as a table formula 573 574 This: 575 576 #+BEGIN_SRC org 577 ,#+NAME: sampformula 578 ,#+BEGIN_SRC python :var angle=90 :var r=2 :exports none 579 from math import radians, cos 580 result = r*cos(radians(angle)) 581 return result 582 ,#+END_SRC 583 584 | angle | r | x | 585 |-------+----+---------------| 586 | 30 | 10 | 8.66025403784 | 587 | 45 | 10 | 7.07106781187 | 588 | 60 | 10 | 5.0 | 589 ,#+TBLFM: $3='(org-sbe "sampformula" (angle $1) (r $2)) 590 #+END_SRC 591 592 Results in this: 593 594 #+NAME: sampformula 595 #+BEGIN_SRC python :var angle=90 :var r=2 :exports none 596 from math import radians, cos 597 result = r*cos(radians(angle)) 598 return result 599 #+END_SRC 600 601 | angle | r | x | 602 |-------+----+---------------| 603 | 30 | 10 | 8.66025403784 | 604 | 45 | 10 | 7.07106781187 | 605 | 60 | 10 | 5.0 | 606 #+TBLFM: $3='(org-sbe "sampformula" (angle $1) (r $2)) 607 608 * LaTeX equations 609 ** Inline equations 610 611 This: 612 #+BEGIN_SRC org 613 Foo bar \(f(x) = \frac{x^3}{n}\) chicken checken. 614 #+END_SRC 615 616 renders as this: 617 618 Foo bar \(f(x) = \frac{x^3}{n}\) chicken checken. 619 620 ** Simple equations 621 622 This: 623 #+BEGIN_SRC org 624 Our best estimate of F(\nu) will be 625 \[ 626 \hat{F}(\nu) = \frac{G(\nu)}{H(\nu)}. 627 \] 628 #+END_SRC 629 630 renders as this: 631 632 Our best estimate of F(\nu) will be 633 \[ 634 \hat{F}(\nu) = \frac{G(\nu)}{H(\nu)}. 635 \] 636 637 ** Aligned sets of equations 638 639 This: 640 #+BEGIN_SRC org 641 \begin{eqnarray*} 642 \hat{f}(x) & \propto & \sum_{\nu} \frac{|F(\nu)H(\nu)|^2}{|N(\nu)|^2} 643 \frac{G(\nu)}{H(\nu)} e^{\frac{2 \pi i \nu x}{N}}\\ 644 & \propto & \sum_{\nu} \frac{|F(\nu)|^2}{|N(\nu)|^2} H(\nu) H^*(\nu) 645 \frac{G(\nu)}{H(\nu)} e^{\frac{2 \pi i \nu x}{N}}\\ 646 & \propto & \sum_{\nu} H^*(\nu) G(\nu) e^{\frac{2 \pi i \nu x}{N}} 647 \end{eqnarray*} 648 #+END_SRC 649 650 renders as this: 651 \begin{eqnarray*} 652 \hat{f}(x) & \propto & \sum_{\nu} \frac{|F(\nu)H(\nu)|^2}{|N(\nu)|^2} 653 \frac{G(\nu)}{H(\nu)} e^{\frac{2 \pi i \nu x}{N}}\\ 654 & \propto & \sum_{\nu} \frac{|F(\nu)|^2}{|N(\nu)|^2} H(\nu) H^*(\nu) 655 \frac{G(\nu)}{H(\nu)} e^{\frac{2 \pi i \nu x}{N}}\\ 656 & \propto & \sum_{\nu} H^*(\nu) G(\nu) e^{\frac{2 \pi i \nu x}{N}} 657 \end{eqnarray*} 658 659 * Inline formula 660 661 =org-mode= can have automatically calculated inline formula. For 662 example, this: 663 664 #+BEGIN_SRC org 665 The scaling for 1.3 airmasses is src_R{format(1.3**(3.0/5.0),digits=3)} =1.17= 666 667 The scaling for 1.3 airmasses is src_calc{round(1.3**(3.0/5.0),4)} =1.1705= 668 669 The scaling for 1.3 airmasses is src_python{return "%4.1f" % (1.3**(3.0/5.0))} =1.2= 670 #+END_SRC 671 672 produces this: 673 674 The scaling for 1.3 airmasses is src_R{format(1.3**(3.0/5.0),digits=3)} =1.17= 675 676 The scaling for 1.3 airmasses is src_calc{round(1.3**(3.0/5.0),4)} =1.1705= 677 678 The scaling for 1.3 airmasses is src_python{return "%4.1f" % (1.3**(3.0/5.0))} =1.2= 679 680 Calculations can be repeated by putting the cursor on the formula and 681 hitting ctrl-c twice. 682 683 * Figures and tables with captions and labels 684 685 #+BEGIN_SRC org 686 #+CAPTION: This was the ditaa example 687 #+LABEL: fig:ditaaex 688 #+ATTR_LaTeX: width=5cm,angle=90 689 [[file:ditaa-simpleboxes.png]] 690 691 This is some sample text in which I reference \ref{fig:ditaaex}. 692 #+END_SRC 693 694 #+CAPTION: This was the ditaa example 695 #+LABEL: fig:ditaaex 696 #+ATTR_LaTeX: width=5cm,angle=90 697 [[file:ditaa-simpleboxes.png]] 698 699 This is some sample text in which I reference \ref{fig:ditaaex}. 700 701 (The reference works in LaTeX, but not html export.) 702 703 More elaborate LaTeX attributes can be used: 704 #+BEGIN_SRC org 705 ,#+ATTR_LaTeX: width=0.38\textwidth wrap placement={r}{0.4\textwidth} 706 #+END_SRC 707 708 Captions and references can also be applied to tables. 709 710 * Figures and tables spanning multiple text columns 711 712 Images, plots, code listings, and tables often need to span multiple 713 text columns to fit when exporting to multi-column latex styles. This 714 can be done by preceding the relevant block with 715 a =#+ATTR_LATEX: :float multicolumn= line, for example: 716 717 #+BEGIN_SRC org 718 ,#+CAPTION: This is a wide table 719 ,#+ATTR_LATEX: :float multicolumn 720 | A | B | C | D | E | F | G | H | 721 |---------+------------+---------+------------+---------+------------+---------+------------| 722 | foo bar | baz boggle | foo bar | baz boggle | foo bar | baz boggle | foo bar | baz boggle | 723 | foo bar | baz boggle | foo bar | baz boggle | foo bar | baz boggle | foo bar | baz boggle | 724 | foo bar | baz boggle | foo bar | baz boggle | foo bar | baz boggle | foo bar | baz boggle | 725 #+END_SRC 726 727 or 728 729 #+BEGIN_SRC org 730 ,#+CAPTION: Here is my python code. 731 ,#+ATTR_LATEX: :float multicolumn 732 ,#+BEGIN_SRC python 733 print "This is a longish line of code that needs to span multiple columns in a latex export" 734 ,#+END_SRC 735 #+END_SRC 736 737 * Verbatim examples 738 739 Verbatim example code can be marked. For example, this: 740 741 #+BEGIN_SRC org 742 #+BEGIN_EXAMPLE 743 Last login: Mon Dec 2 08:44:25 on ttys000 744 argos:~ neilsen$ echo "foo" 745 foo 746 argos:~ neilsen$ 747 #+END_EXAMPLE 748 #+END_SRC 749 750 results in this: 751 752 #+BEGIN_EXAMPLE 753 Last login: Mon Dec 2 08:44:25 on ttys000 754 argos:~ neilsen$ echo "foo" 755 foo 756 argos:~ neilsen$ 757 #+END_EXAMPLE 758 759 * Code examples 760 761 Source code can be displayed using the native modes in =emacs=. For 762 example, this: 763 764 #+BEGIN_SRC org 765 #+BEGIN_SRC python 766 def times_two(x): 767 y = x*2 768 return y 769 770 print times_two(5) 771 ,#+END_SRC 772 #+END_SRC 773 774 produces this: 775 #+BEGIN_SRC python 776 def times_two(x): 777 y = x*2 778 return y 779 780 print times_two(5) 781 #+END_SRC 782 783 * Running code, returning raw output 784 785 This: 786 787 #+BEGIN_SRC org 788 #+BEGIN_SRC python :results output :exports both 789 def times_two(x): 790 y = x*2 791 return y 792 793 print times_two(5) 794 ,#+END_SRC 795 796 #+RESULTS: 797 : 10 798 #+END_SRC 799 800 produces this: 801 802 #+BEGIN_SRC python :results output :exports both 803 def times_two(x): 804 y = x*2 805 return y 806 807 print times_two(5) 808 #+END_SRC 809 810 #+RESULTS: 811 : 10 812 813 * Running code, return =org-mode= tables 814 815 This: 816 817 #+BEGIN_SRC org 818 #+BEGIN_SRC python :exports both 819 a = ('b', 200) 820 b = ('x', 10) 821 c = ('q', -42) 822 return (a, b, c) 823 ,#+END_SRC 824 825 #+RESULTS: 826 | b | 200 | 827 | x | 10 | 828 | q | -42 | 829 #+END_SRC 830 831 produces this: 832 833 #+BEGIN_SRC python :exports both 834 a = ('b', 200) 835 b = ('x', 10) 836 c = ('q', -42) 837 return (a, b, c) 838 #+END_SRC 839 840 #+RESULTS: 841 | b | 200 | 842 | x | 10 | 843 | q | -42 | 844 845 By removing the =:exports both=, you can export just the code and not 846 the output. By replaceing it with =:exports results=, you can export 847 the output without the source. 848 849 * Running code remotely 850 851 Adding appropriate =:dir= parameters runs the code in other working 852 directories, or even on remote machines: 853 854 #+BEGIN_SRC org 855 #+BEGIN_SRC sh :results output :exports both 856 echo $PWD 857 echo $HOSTNAME 858 ,#+END_SRC 859 860 #+RESULTS: 861 : /Users/neilsen/Notebook/org/orgExamples 862 : argos.dhcp.fnal.gov 863 864 #+BEGIN_SRC sh :results output :exports both :dir /tmp 865 echo $PWD 866 echo $HOSTNAME 867 ,#+END_SRC 868 869 #+RESULTS: 870 : /private/tmp 871 : argos.dhcp.fnal.gov 872 873 #+BEGIN_SRC sh :results output :exports both :dir :dir /ssh:neilsen@decam03.fnal.gov:/home/neilsen 874 echo $PWD 875 echo $HOSTNAME 876 ,#+END_SRC 877 878 #+RESULTS: 879 : /home/neilsen 880 : decam03.fnal.gov 881 #+END_SRC 882 883 * Running C code 884 C code is handled a little differently, as it must be compiled and run. 885 886 This block: 887 888 #+BEGIN_SRC org 889 ,#+HEADERS: :includes <math.h> :flags -lm 890 ,#+HEADERS: :var x=1.0 :var y=4.0 :var z=10.0 891 ,#+BEGIN_SRC C :exports both 892 double pi = 4*atan(1); 893 double r, theta, phi; 894 r = sqrt(x*x+y*y+z*z); 895 theta = acos(z/r) * 180.0/pi; 896 phi = atan2(y,x) * 180.0/pi; 897 printf("%f %f %f", r, theta, phi); 898 ,#+END_SRC 899 #+END_SRC 900 901 Generates, compiles, and runs this C code: 902 903 #+BEGIN_SRC C 904 #include <math.h> 905 906 double x = 1.000000; 907 double y = 4.000000; 908 double z = 10.000000; 909 int main() { 910 double pi = 4*atan(1); 911 double r, theta, phi; 912 r = sqrt(x*x+y*y+z*z); 913 theta = acos(z/r) * 180.0/pi; 914 phi = atan2(y,x) * 180.0/pi; 915 printf("%f %f %f", r, theta, phi); 916 return 0; 917 } 918 #+END_SRC 919 920 which results in: 921 922 #+BEGIN_SRC org 923 ,#+RESULTS: 924 : 10.816654 22.406871 75.963757 925 #+END_SRC 926 927 So the final result looks like this when evaluated and exported: 928 929 #+HEADERS: :includes <math.h> :flags -lm 930 #+HEADERS: :var x=1.0 :var y=4.0 :var z=10.0 931 #+BEGIN_SRC C :exports both 932 double pi = 4*atan(1); 933 double r, theta, phi; 934 r = sqrt(x*x+y*y+z*z); 935 theta = acos(z/r) * 180.0/pi; 936 phi = atan2(y,x) * 180.0/pi; 937 printf("%f %f %f", r, theta, phi); 938 #+END_SRC 939 940 #+RESULTS: 941 : 10.816654 22.406871 75.963757 942 943 There is a trick to multiple includes: they must be passed as elisp lists, for example: 944 945 #+BEGIN_SRC org 946 ,#+BEGIN_SRC C :includes '(<math.h> <time.h>) 947 #+END_SRC 948 949 * Running java code 950 951 Java code can be evaluated as well, for example: 952 953 #+BEGIN_SRC org 954 ,#+HEADERS: :classname HelloWorld :cmdline "-cp ." 955 ,#+begin_src java :results output :exports both 956 public class HelloWorld { 957 public static void main(String[] args) { 958 System.out.println("Hello, World"); 959 } 960 } 961 ,#+end_src 962 963 ,#+RESULTS: 964 : Hello, World 965 #+END_SRC 966 967 This exports to: 968 969 #+HEADERS: :classname HelloWorld :cmdline "-cp ." 970 #+begin_src java :results output :exports both 971 public class HelloWorld { 972 public static void main(String[] args) { 973 System.out.println("Hello, World"); 974 } 975 } 976 #+end_src 977 978 #+RESULTS: 979 : Hello, World 980 981 * Margin notes in LaTeX 982 983 Margin notes can be generated for the latex export, but not in a way 984 portable to other export methods (like html): 985 986 #+BEGIN_SRC org 987 #+BEGIN_LaTeX 988 \marginpar{\color{blue} \tiny \raggedright 989 \vspace{18pt} 990 In the Molly 23 layout, not all tilings have the same numbers of 991 hexes (pointings); the offsets for each tiling can push different hexes into or 992 out of the footprint.} 993 #+END_LaTeX 994 #+END_SRC 995 996 The vspace help tweak the placement to put it next the text you want 997 it next to. 998 999 Note that you can use the same trick with figure. If you use the 1000 =capt-of= latex package, you can even get the figure numbered 1001 correctly. For example, 1002 1003 #+BEGIN_SRC org 1004 #+BEGIN_LATEX 1005 \marginpar{ 1006 \includegraphics[width=\marginparwidth]{test_img.png} 1007 \captionof{figure}{This is a test figure}\label{testimg} 1008 } 1009 #+END_LATEX 1010 #+END_SRC 1011 1012 If you have fiddled with the margins using the LaTeX =geometry= 1013 package, be sure to set the =marginparwidth= parameter in your 1014 =geometry= statement. 1015 1016 * Querying a =PostgreSQL= database 1017 1018 Provided your account is configured with appropriate passwords, this: 1019 #+BEGIN_SRC org 1020 #+BEGIN_SRC sql :engine postgresql :exports results :cmdline -p 5443 -h des20.fnal.gov -U decam_reader -d decam_prd 1021 SELECT date, ra, declination FROM exposure.exposure LIMIT 10 1022 ,#+END_SRC 1023 #+END_SRC 1024 1025 Results in this: 1026 #+BEGIN_SRC sql :engine postgresql :exports results :cmdline -p 5443 -h des20.fnal.gov -U decam_reader -d decam_prd 1027 SELECT date, ra, declination FROM exposure.exposure LIMIT 10 1028 #+END_SRC 1029 1030 #+RESULTS: 1031 | date | ra | declination | 1032 |-------------------------------+------------+-------------| 1033 | 2013-06-04 21:48:01.54791+00 | 271.125446 | -31.316167 | 1034 | 2013-06-04 21:48:38.329063+00 | 271.125446 | -31.316167 | 1035 | 2013-04-25 00:09:21.976324+00 | 144.404229 | 15.058917 | 1036 | 2013-01-11 03:16:40.700054+00 | 111.02375 | -1.490556 | 1037 | 2013-03-17 19:36:44.482928+00 | 200.013333 | -20.65 | 1038 | 2013-06-24 07:12:00.531216+00 | 9.5 | -43.998 | 1039 | 2013-06-12 01:42:20.851991+00 | 269.261287 | -27.892739 | 1040 | 2013-06-24 07:15:49.054427+00 | 9.5 | -43.998 | 1041 | 2013-09-02 20:25:33.523124+00 | 50 | 0 | 1042 | 2013-09-02 20:26:24.503093+00 | 50 | 0 | 1043 1044 * Interacting with =R= 1045 ** Using an =org-mode= table as an R data frame 1046 1047 If you have an =org-mode= table with a name: 1048 1049 #+BEGIN_SRC org 1050 #+tblname: delsee 1051 | airmass | zenith_seeing | delivered_seeing | 1052 |---------+---------------+------------------| 1053 | 1.3 | 0.95 | 1.1119612 | 1054 | 1.3 | 1.0 | 1.1704854 | 1055 | 1.3 | 1.1 | 1.2875340 | 1056 | 1.3 | 1.2 | 1.4045825 | 1057 #+TBLFM: $3=$2*($1**0.6) 1058 #+END_SRC 1059 1060 you can use it from within =R= code as a data frame: 1061 1062 #+BEGIN_SRC org 1063 #+begin_src R :results output :var delsee=delsee 1064 summary(delsee) 1065 ,#+end_src 1066 1067 #+RESULTS: 1068 : airmass zenith_seeing delivered_seeing 1069 : Min. :1.3 Min. :0.9500 Min. :1.112 1070 : 1st Qu.:1.3 1st Qu.:0.9875 1st Qu.:1.156 1071 : Median :1.3 Median :1.0500 Median :1.229 1072 : Mean :1.3 Mean :1.0625 Mean :1.244 1073 : 3rd Qu.:1.3 3rd Qu.:1.1250 3rd Qu.:1.317 1074 : Max. :1.3 Max. :1.2000 Max. :1.405 1075 #+END_SRC 1076 1077 ** Generate a plot in your document using =R= 1078 1079 This: 1080 #+BEGIN_SRC org 1081 1082 #+tblname: delsee 1083 | airmass | zenith_seeing | delivered_seeing | 1084 |---------+---------------+------------------| 1085 | 1.3 | 0.95 | 1.1119612 | 1086 | 1.3 | 1.0 | 1.1704854 | 1087 | 1.3 | 1.1 | 1.2875340 | 1088 | 1.3 | 1.2 | 1.4045825 | 1089 #+TBLFM: $3=$2*($1**0.6) 1090 1091 #+begin_src R :exports both :results output graphics :var delsee=delsee :file delsee-r.png :width 400 :height 300 1092 library(ggplot2) 1093 p <- ggplot(delsee, aes(zenith_seeing, delivered_seeing)) 1094 p <- p + geom_point() 1095 p 1096 ,#+end_src 1097 1098 #+RESULTS: 1099 [[file:delsee-r.png]] 1100 #+END_SRC 1101 1102 Results in this: 1103 #+tblname: delsee 1104 | airmass | zenith_seeing | delivered_seeing | 1105 |---------+---------------+------------------| 1106 | 1.3 | 0.95 | 1.1119612 | 1107 | 1.3 | 1.0 | 1.1704854 | 1108 | 1.3 | 1.1 | 1.2875340 | 1109 | 1.3 | 1.2 | 1.4045825 | 1110 #+TBLFM: $3=$2*($1**0.6) 1111 1112 #+begin_src R :exports both :results output graphics :var delsee=delsee :file delsee-r.png :width 400 :height 300 1113 library(ggplot2) 1114 p <- ggplot(delsee, aes(zenith_seeing, delivered_seeing)) 1115 p <- p + geom_point() 1116 p 1117 #+end_src 1118 1119 #+RESULTS: 1120 [[file:delsee-r.png]] 1121 1122 ** Generating an =org-mode= table from an =R= data frame 1123 1124 The simple way is just to return the value of the data frame: 1125 1126 #+BEGIN_SRC org 1127 #+BEGIN_SRC R :colnames yes 1128 d <- data.frame(foo=c('a','b','n'), bar=c(1.0/3.0,22,32)) 1129 d 1130 ,#+END_SRC 1131 1132 #+RESULTS: 1133 | foo | bar | 1134 |-----+-------------------| 1135 | a | 0.333333333333333 | 1136 | b | 22 | 1137 | n | 32 | 1138 #+END_SRC 1139 1140 To limit significant figures, use the =ascii= =R= package. For 1141 example, this: 1142 1143 #+BEGIN_SRC org 1144 #+BEGIN_SRC R :results output raw :exports both 1145 d <- data.frame(foo=c('a','b','n'), bar=c(1.0/3.0,22,32)) 1146 1147 library(ascii) 1148 options(asciiType="org") 1149 ascii(d,format=c('s','f'),digits=c(5,4),include.rownames=FALSE) 1150 ,#+END_SRC 1151 1152 #+RESULTS: 1153 | foo | bar | 1154 |-----+---------| 1155 | a | 0.3333 | 1156 | b | 22.0000 | 1157 | n | 32.0000 | 1158 #+END_SRC 1159 1160 produces this: 1161 1162 #+BEGIN_SRC R :results output raw :exports both 1163 d <- data.frame(foo=c('a','b','n'), bar=c(1.0/3.0,22,32)) 1164 1165 library(ascii) 1166 options(asciiType="org") 1167 ascii(d,format=c('s','f'),digits=c(5,4),include.rownames=FALSE) 1168 #+END_SRC 1169 1170 #+RESULTS: 1171 | foo | bar | 1172 |-----+---------| 1173 | a | 0.3333 | 1174 | b | 22.0000 | 1175 | n | 32.0000 | 1176 1177 * Interacting with =python= 1178 1179 ** Using an =org-mode= table in python 1180 1181 #+BEGIN_SRC org 1182 #+tblname: delsee 1183 | airmass | zenith_seeing | delivered_seeing | 1184 |---------+---------------+------------------| 1185 | 1.3 | 0.95 | 1.1119612 | 1186 | 1.3 | 1.0 | 1.1704854 | 1187 | 1.3 | 1.1 | 1.2875340 | 1188 | 1.3 | 1.2 | 1.4045825 | 1189 #+TBLFM: $3=$2*($1**0.6) 1190 1191 #+BEGIN_SRC python :var delsee=delsee :results output 1192 print delsee 1193 ,#+END_SRC 1194 1195 #+RESULTS: 1196 : [[1.3, 0.95, 1.1119612], [1.3, 1.0, 1.1704854], [1.3, 1.1, 1.287534], [1.3, 1.2, 1.4045825]] 1197 #+END_SRC 1198 1199 ** Plotting with python 1200 1201 This: 1202 1203 #+BEGIN_SRC org 1204 #+tblname: delsee 1205 | airmass | zenith_seeing | delivered_seeing | 1206 |---------+---------------+------------------| 1207 | 1.3 | 0.95 | 1.1119612 | 1208 | 1.3 | 1.0 | 1.1704854 | 1209 | 1.3 | 1.1 | 1.2875340 | 1210 | 1.3 | 1.2 | 1.4045825 | 1211 #+TBLFM: $3=$2*($1**0.6) 1212 1213 #+BEGIN_SRC python :var fname="delseepy.png" :var delsee=delsee :results file 1214 import matplotlib.pyplot as plt 1215 1216 x, y, z = zip(*delsee) 1217 1218 fig = plt.figure() 1219 axes = fig.add_subplot(1,1,1) 1220 axes.plot(y, z, marker='o') 1221 fig.savefig(fname) 1222 1223 return fname 1224 ,#+END_SRC 1225 1226 #+RESULTS: 1227 [[file:delseepy.png]] 1228 #+END_SRC 1229 1230 Results in this: 1231 1232 #+RESULTS: 1233 [[file:delseepy.png]] 1234 * Setting environment variables (like =PYTHONPATH=) 1235 1236 Create an =emacs-lisp= code block that looks like this: 1237 1238 #+BEGIN_SRC org 1239 #+BEGIN_SRC emacs-lisp 1240 (setenv "PYTHONPATH" "/Users/neilsen/Development/obswatch-trunk/common/python") 1241 ,#+END_SRC 1242 #+END_SRC 1243 1244 Execute it, and it changes the environment accordingly. 1245 1246 Note that you can also append to environment variables like this: 1247 1248 #+BEGIN_SRC org 1249 #+BEGIN_SRC emacs-lisp 1250 (setenv "PYTHONPATH" (concat (getenv "PYTHONPATH") ":" (getenv "DQSTATS_DIR"))) 1251 ,#+END_SRC 1252 #+END_SRC 1253 1254 * Writing literate =python= code 1255 ** Creating the high level structure of the file 1256 1257 Following the structure outlined in [[http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#module-structure][Code Like a Pythonista]], construct 1258 the python source file in sections: 1259 1260 #+BEGIN_SRC org 1261 #+BEGIN_SRC python :noweb yes :tangle HelloWorld.py :exports none 1262 """This is a hello world example document""" 1263 1264 # imports 1265 import sys 1266 <<helloworld-main-imports>> 1267 1268 # constants 1269 1270 # exception classes 1271 1272 # interface functions 1273 1274 # classes 1275 <<HelloWorld-defn>> 1276 1277 # internal functions & classes 1278 1279 <<helloworld-main>> 1280 1281 if __name__ == '__main__': 1282 status = main() 1283 sys.exit(status) 1284 ,#+END_SRC 1285 #+END_SRC 1286 1287 When =M-x org-babel-tangle= is run within =emacs=, the 1288 =:tangle HelloWorld.py= line will cause it to generate a the file 1289 =HelloWorld.py= from the contents of the code blocks. 1290 1291 The bracketed lines (=helloworld-classes=, for example) are code 1292 fragments that will be defined later. =org-mode= will automatically 1293 substitute these blocks when creating the =HelloWorld.py= file. 1294 1295 #+BEGIN_SRC python :noweb yes :tangle HelloWorld.py :exports none 1296 """This is a hello world example document""" 1297 1298 # imports 1299 import sys 1300 <<helloworld-main-imports>> 1301 1302 # constants 1303 1304 # exception classes 1305 1306 # interface functions 1307 1308 # classes 1309 <<HelloWorld-defn>> 1310 1311 # internal functions & classes 1312 1313 <<helloworld-main>> 1314 1315 if __name__ == '__main__': 1316 status = main() 1317 sys.exit(status) 1318 #+END_SRC 1319 1320 ** Generating functionality for =HelloWorld.py= 1321 1322 Define the =HelloWorld= class thus: 1323 1324 #+BEGIN_SRC org :noweb yes 1325 ,#+NAME: HelloWorld-defn 1326 ,#+BEGIN_SRC python 1327 <<HelloWorld-defn>> 1328 ,#+END_SRC 1329 #+END_SRC 1330 1331 In the org-mode document, it will look like this: 1332 1333 #+NAME: HelloWorld-defn 1334 #+BEGIN_SRC python 1335 class HelloWorld(object): 1336 def __init__(self, who): 1337 self.who = who 1338 1339 def say_hello(self): 1340 print "Hello %s" % self.who 1341 #+END_SRC 1342 1343 ** Generating a =main= function for =HelloWorld= 1344 1345 It's usually a good idea to have an argument parser in =main=. Start 1346 by creating a code block the performs the required imports: 1347 1348 #+BEGIN_SRC org :noweb yes 1349 ,#+NAME: helloworld-main-imports 1350 ,#+BEGIN_SRC python 1351 <<helloworld-main-imports>> 1352 ,#+END_SRC 1353 #+END_SRC 1354 1355 which comes out like this in the document: 1356 1357 #+NAME: helloworld-main-imports 1358 #+BEGIN_SRC python 1359 from argparse import ArgumentParser 1360 #+END_SRC 1361 1362 Then, define the =main= function itself: 1363 1364 #+BEGIN_SRC org :noweb yes 1365 ,#+NAME: helloworld-main 1366 ,#+BEGIN_SRC python 1367 <<helloworld-main>> 1368 ,#+END_SRC 1369 #+END_SRC 1370 1371 which comes out like this: 1372 1373 #+NAME: helloworld-main 1374 #+BEGIN_SRC python 1375 def main(): 1376 parser = ArgumentParser(description="Say hi") 1377 parser.add_argument("-w", "--who", 1378 type=str, 1379 default="world", 1380 help="Who to say hello to") 1381 args = parser.parse_args() 1382 1383 who = args.who 1384 1385 greeter = HelloWorld(who) 1386 greeter.say_hello() 1387 1388 return 0 1389 #+END_SRC 1390 1391 ** Running main from bash 1392 1393 Create a section to make it easy to run the generated code from within 1394 the orgmode document: 1395 1396 #+BEGIN_SRC org :noweb yes 1397 ,#+NAME: bashrun-helloworld 1398 ,#+BEGIN_SRC sh :results output :exports none 1399 <<bashrun-helloworld>> 1400 ,#+END_SRC 1401 #+END_SRC 1402 1403 The "true" command at the end of this shell script makes sure that the 1404 output gets incorportated into the =org-mode= buffer even if the code 1405 crashes. 1406 1407 The output looks like this in your orgmode buffer: 1408 1409 #+NAME: bashrun-helloworld 1410 #+BEGIN_SRC sh :results output :exports both 1411 python HelloWorld.py --w Eric 2>&1 1412 true 1413 #+END_SRC 1414 1415 #+RESULTS: bashrun-helloworld 1416 : Hello Eric 1417 1418 * Doing automated testing of literate =python= programs 1419 1420 ** Making =test_HelloWorld.txt= 1421 1422 Create interactive tests. It's a good idea to use the restructured 1423 text mode in emacs, so that the result can be a ReStructuredText test 1424 document, traditional to =python=. 1425 1426 Here is one, for example: 1427 1428 #+BEGIN_SRC org :noweb yes 1429 ,#+NAME: doctest-foo 1430 ,#+BEGIN_SRC rst 1431 <<doctest-foo>> 1432 ,#+END_SRC 1433 #+END_SRC 1434 1435 #+NAME: doctest-foo 1436 #+BEGIN_SRC rst :exports none 1437 example foo:: 1438 >>> from HelloWorld import * 1439 >>> 1440 >>> foo = HelloWorld('foo') 1441 >>> foo.say_hello() 1442 Hello foo 1443 1444 #+END_SRC 1445 1446 and another: 1447 1448 #+BEGIN_SRC org :noweb yes 1449 ,#+NAME: doctest-bar 1450 ,#+BEGIN_SRC rst 1451 <<doctest-bar>> 1452 ,#+END_SRC 1453 #+END_SRC 1454 1455 #+NAME: doctest-bar 1456 #+BEGIN_SRC rst :exports none 1457 example bar:: 1458 >>> from HelloWorld import * 1459 >>> 1460 >>> bar = HelloWorld('bar') 1461 >>> bar.say_hello() 1462 Hello bar 1463 1464 #+END_SRC 1465 1466 Create a document to "tangle" them into 1467 1468 #+BEGIN_SRC org :noweb no 1469 ,#+BEGIN_SRC text :noweb yes :tangle test_HelloWorld.txt :exports none 1470 <<doctest-foo>> 1471 <<doctest-bar>> 1472 ,#+END_SRC 1473 #+END_SRC 1474 1475 #+BEGIN_SRC text :noweb yes :tangle test_HelloWorld.txt :exports none 1476 <<doctest-foo>> 1477 <<doctest-bar>> 1478 #+END_SRC 1479 1480 ** Running just the doctests 1481 1482 You can run the doctests from with =org-mode= with this bash code snippet: 1483 1484 #+BEGIN_SRC org 1485 ,#+NAME: bashrun-helloworld-doctest 1486 ,#+BEGIN_SRC sh :results output :exports both 1487 python -m doctest test_HelloWorld.txt 2>&1 1488 true 1489 ,#+END_SRC 1490 #+END_SRC 1491 1492 If the test succeeds, it will produce no output 1493 1494 ** Defining =unittest= tests 1495 1496 Define the unit test like any other piece of =python= code: 1497 1498 #+BEGIN_SRC org :noweb yes 1499 ,#+NAME: unittest-foo 1500 ,#+BEGIN_SRC python 1501 <<unittest-foo>> 1502 ,#+END_SRC 1503 #+END_SRC 1504 1505 #+NAME: unittest-foo 1506 #+BEGIN_SRC python :exports none 1507 class TestFoo(unittest.TestCase): 1508 def test_foo(self): 1509 greeter = HelloWorld('foo') 1510 self.assertEqual(greeter.who, 'foo') 1511 #+END_SRC 1512 1513 ** Making =TestHelloWorld.py= 1514 1515 Define the main testing module like this: 1516 1517 #+BEGIN_SRC org 1518 ,#+BEGIN_SRC python :noweb yes :tangle TestHelloWorld.py :exports none 1519 import sys 1520 import unittest 1521 from doctest import DocFileSuite 1522 from HelloWorld import * 1523 1524 <<unittest-foo>> 1525 1526 def main(): 1527 suite = unittest.TestSuite() 1528 suite.addTests( DocFileSuite('test_HelloWorld.txt') ) 1529 suite.addTests( 1530 unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])) 1531 unittest.TextTestRunner(verbosity=2).run(suite) 1532 return 0 1533 1534 if __name__ == '__main__': 1535 status = main() 1536 sys.exit(status) 1537 ,#+END_SRC 1538 #+END_SRC 1539 1540 #+BEGIN_SRC python :noweb yes :tangle TestHelloWorld.py :exports none 1541 import sys 1542 import unittest 1543 from doctest import DocFileSuite 1544 from HelloWorld import * 1545 1546 <<unittest-foo>> 1547 1548 def main(): 1549 suite = unittest.TestSuite() 1550 suite.addTests( DocFileSuite('test_HelloWorld.txt') ) 1551 suite.addTests( 1552 unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])) 1553 unittest.TextTestRunner(verbosity=2).run(suite) 1554 return 0 1555 1556 if __name__ == '__main__': 1557 status = main() 1558 sys.exit(status) 1559 #+END_SRC 1560 1561 ** Running all tests 1562 1563 Use this =bash= source block to run all tests: 1564 1565 #+BEGIN_SRC org 1566 ,#+NAME: bashrun-helloworld-alltest 1567 ,#+BEGIN_SRC sh :results output :exports both 1568 python -m doctest test_HelloWorld.py 2>&1 1569 ,#+END_SRC 1570 #+END_SRC 1571 1572 The output looks like this: 1573 1574 #+NAME: bashrun-helloworld-alltest 1575 #+BEGIN_SRC sh :results output :exports both 1576 python TestHelloWorld.py 2>&1 1577 #+END_SRC 1578 1579 #+RESULTS: bashrun-helloworld-alltest 1580 : test_HelloWorld.txt 1581 : Doctest: test_HelloWorld.txt ... ok 1582 : test_foo (__main__.TestFoo) ... ok 1583 : 1584 : ---------------------------------------------------------------------- 1585 : Ran 2 tests in 0.004s 1586 : 1587 : OK 1588 1589 * Generating an =org-mode= source block within an =org-mode= document 1590 1591 This document often needs to quote org-mode code within org-mode, 1592 which is slightly tricky, because you need to escape the =#+END_SRC= 1593 block. Do this using a comma in the first line. So to get this: 1594 1595 #+BEGIN_SRC org 1596 ,#+BEGIN_SRC python 1597 print "foo" 1598 ,#+END_SRC 1599 #+END_SRC 1600 1601 Do this: 1602 1603 1604 #+BEGIN_SRC org 1605 ,#+BEGIN_SRC org 1606 ,#+BEGIN_SRC python 1607 print "foo" 1608 ,,#+END_SRC 1609 ,#+END_SRC 1610 #+END_SRC 1611 1612 Sometimes additional elements (particularly lines with special meaning 1613 in org-mode, like those starting with =#= or =*=) need escaping with a 1614 comma as well, but not always. 1615 1616 * LaTeX presentations with beamer 1617 1618 To generate a presentation PDF file using the beamer mode in LaTeX, do 1619 something like this: 1620 1621 #+BEGIN_SRC org 1622 ,#+TITLE: 1623 ,#+AUTHOR: 1624 ,#+OPTIONS: H:1 toc:nil \n:nil @:t ::t |:t ^:t *:t TeX:t LaTeX:t 1625 ,#+LATEX_CLASS: beamer 1626 ,#+LATEX_CLASS_OPTIONS: [presentation] 1627 ,#+BEAMER_THEME: default 1628 ,#+BEAMER_FONT_THEME: default 1629 ,#+BEAMER_COLOR_THEME: dove 1630 ,#+COLUMNS: %45ITEM %10BEAMER_ENV(Env) %10BEAMER_ACT(Act) %4BEAMER_COL(Col) %8BEAMER_OPT(Opt) 1631 ,#+STARTUP: beamer 1632 1633 ,* Slide one 1634 1635 - Foo 1636 + baz 1637 + qux 1638 - Bar 1639 1640 1641 ,* Next slide foo 1642 1643 - Foo 1644 + baz 1645 + qux 1646 - Bar 1647 1648 #+END_SRC 1649 1650 The present =#+TITLE:= and =#+AUTHOR:= lines without values prevent 1651 the generation of a title page. If these have values, a title pages is 1652 generated.