2022-04-25 Calculating fuel consumption in Org

While I am a big fan of Org-mode, I admit that I don’t try to use it for everything. Having used it for over ten years now I am aware that it is not the answer to everything – not even everything related to time-management. That said, it has quite a few unique features which make it very well suited to some specific needs.

Some time ago, I decided to make a very simple fuel consumption calculator. A natural choice for most tech-savvy people would be to use a spreadsheet. I thought, why not use Org-mode? So, I came up with this table.

|       Date | Amount refueled | Odometer | Fuel consumption |
|            |        (liters) |    (kms) |  (liters/100 km) |
|------------+-----------------+----------+------------------|
| 2022-01-03 |                 |      550 |             0.00 |
|------------+-----------------+----------+------------------|
| 2022-02-01 |              35 |     1130 |             6.03 |
| 2022-03-04 |              28 |     1530 |             7.00 |
|------------+-----------------+----------+------------------|
|      Total |                 |          |             6.43 |
#+TBLFM: $4=100*$2/($3-@-1$3);%.2f::@6$4=100*vsum(@II$2..@III$2)/(@III-1$3-@II-1$3);%.2f

The important bit is that the entry above the second horizontal line is the initial reading of the odometer, not corresponding to a refuel.

If you are a heavy user of Org spreadsheet, you will recognize a few features I used here. One of them is that I used a column formula for the last column (calculating the fuel consumption since the last refuel) and a cell formula for the total fuel consumption (the bottom-right cell). Another one is the use of the extremely handy Roman numerals for rows: @I means the first horizontal line, @II means the second one, @II-1$3 means the cell above the second horizontal line in the third column etc. This means in particular that you can move the horizontal lines up and down (with M-<up> and M-<down>) and have the fuel consumption in some interval computed. Do that with your regular LibreOfficeCalc or Excel! (One downside is that Org spreadsheet does not automatically recalculate the table after such a change – you need a C-u C-c C-c after the movement. It would be great if this could be remedied by adding org-table-recalculate to org-metaup-hook and org-metadown-hook – unfortunately, for some reason I couldn’t make that work. You could do

(advice-add
 'org-table-move-row
 :after
 (lambda (&rest args) (org-table-recalculate t)))

but it has the drawback of making row movements very slow. One way to make it faster would be for that advice to check for some property and perform the recalculation only if that property is set – but I don’t think it’s that necessary. (Also, keep in mind that using anonymous functions in advice is a bad idea – removing them later is a hassle. This Emacs.SE answer might be useful then.)

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryOrgMode