Org-mode, literate programming in Emacs
Modified 2023-07-21
Session Composition
Languages which support a REPL make it possible to compose code blocks, by running the blocks through one single session (by defining the same session in two different code blocks).
Example
In one code block we could declare a variable...
MY_NAME="Kevin Rangel"And use the previously declared value in the next code block
echo $MY_NAMEKevin Rangel
Block Composition
Embedding previously defined code blocks (through name) inside other code
blocks. Append :noweb yes to enable this code embedding syntax in a block.
Example
function Hello(name) {
console.log("Hello " + name)
}<<hello-from-js>>
Hello("Kevin")Hello Kevin
Complex Example
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main()
{
Matrix3d m = Matrix3d::Random();
m = (m + Matrix3d::Constant(1.2)) * 50;
cout << "m =" << endl << m << endl;
Vector3d v(1,2,3);
cout << "m * v =" << endl << m * v << endl;
}
Tangle Example
The tangle feature of org-mode generates physical files on the disk. https://github.com/niklaseklund/presentations/blob/master/literate_programming/post_presentation.org#our-own-image
Presentation Example
Summary
Org-mode + literate programming
ContextDocumentationEmacsExports
Thanks for listening
Metadata
- Creator(s)
Niklas Eklund
In order to take programming notes, I guess it'd be beneficial to be able to run the code snippets from within org-roam and I still have a shit to learn and org-mode is not a beast I will be taming within the next year or so.