Formatting ledger output for ledger-cli

Ledger cli

Introduction

Ledger is an double ledger accounting tool. The main advantage
of ledger is that it works with pure-text files. With text files you can
keep your accounting under version control and track all changes, like you
do in coding.

The entries itself are simple to add. Here an example:

2014/12/15 Groceries
    Expenses:Groceries         CHF 87.00
    Liabilities:MasterCard

2014/12/16 Groceries again
    Expenses:Groceries         CHF 12.00
    Liabilities:MasterCard

2014/12/17 Mastercard invoice
    Assets:Bank
    Liabilities:MasterCard         CHF 99.00

Let’s assume we saved this file in the current directory under the
filename ledger-data.dat.

This means you transfer 87 CHF from the Mastercard account to the
Groceries account, then again 12 CHF from Mastercard to Groceries,
and finally, you paid the Mastercard invoice from your bank.
The list of entries can be shown with the registry (reg) command:

ledger -f ledger-data.dat reg

The output then is:

14-Dec-15 Groceries             Expenses:Groceries        CHF 87.00    CHF 87.00
                                Liabilities:MasterCard   CHF -87.00            0
14-Dec-16 Groceries again       Expenses:Groceries        CHF 12.00    CHF 12.00
                                Liabilities:MasterCard   CHF -12.00            0
14-Dec-17 Mastercard invoice    Assets:Bank              CHF -99.00   CHF -99.00
                                Liabilities:MasterCard    CHF 99.00            0

The final balances of all accounts can be seen with the balance (bal) command:

          CHF -99.00  Assets:Bank
           CHF 99.00  Expenses:Groceries
--------------------
                   0

So you see that now your bank account is missing 99 CHF, which have been used for groceries.
The MasterCard account disappeared, because it has a balance of 0.0 CHF.

So far for the introduction. For more information have a look at the extensive
documentation (at the moment of writing version 3).

Formatting ledger-cli’s output for latex tables

In order to have a nicer presentation of the balance and income & expenses, I
tried to use the –format command. However, this command is not easy to
use because documentation on it is hard to find. Here some things I figured out:

Create a latex table with ledger, final result

This is what we would like to achive:

echo ""
echo "\\\\begin{tabular}{lr}"
echo "Amount & Account \\\\\\"
echo "\\\\hline"
 ledger -f /tmp/tmp.dat  bal '^Assets:' 'Expenses:' --format "%(scrub(\"{\\\\hskip \"))%(depth)%(scrub(\"mm}\")) %-(ansify_if(ansify_if(partial_account(options.flat), blue if color), bold if should_bold)) & %(ansify_if(justify(scrub(display_total), 20, 20 + int(prepend_width), true, color), bold if should_bold)) %(!options.flat ? depth_spacer : \"\")%(scrub(\"\\\\\\\\\"))\n%/ %(scrub(\"\\\\\hline\")) \n & %\$5%(scrub(\"\\\\\\\\\"))\n%(scrub(\"\\\\\hline\"))\n%/ %(prepend_width ? \" \" * int(prepend_width) : \"\")"
echo "\\\\end{tabular}"

where the ledger output should be correctly formatted with the –format flag.

The solution I’ve developed is

echo ""
echo "\\\\begin{tabular}{lr}"
echo "Amount & Account \\\\\\"
echo "\\\\hline"
 ledger -f /tmp/tmp.dat  bal '^Assets:' 'Expenses:' --format "%(scrub(\"{\\\\hskip \"))%(depth)%(scrub(\"mm}\")) %-(ansify_if(ansify_if(partial_account(options.flat), blue if color), bold if should_bold)) & %(ansify_if(justify(scrub(display_total), 20, 20 + int(prepend_width), true, color), bold if should_bold)) %(!options.flat ? depth_spacer : \"\")%(scrub(\"\\\\\\\\\"))\n%/ %(scrub(\"\\\\\hline\")) \n & %\$5%(scrub(\"\\\\\\\\\"))\n%(scrub(\"\\\\\hline\"))\n%/ %(prepend_width ? \" \" * int(prepend_width) : \"\")"
echo "\\\\end{tabular}"

As you see, the complicated part is in the –format part. Let us
dissect it. The original balance format is (taken from github):

"%(ansify_if("
" justify(scrub(display_total), 20,"
" 20 + int(prepend_width), true, color),"
" bold if should_bold))"
" %(!options.flat ? depth_spacer : \"\")"
"%-(ansify_if("
" ansify_if(partial_account(options.flat), blue if color),"
" bold if should_bold))\n%/"
"%$1\n%/"
"%(prepend_width ? \" \" * int(prepend_width) : \"\")"
"--------------------\n");

There are a few things to notice here. First, you see several `%(xx)`
commands. These are interpreted by ledger. Then you have the
`%$1` command, which is doing something with the `nth` of
the `%(xx)` to show the final result line, with the balance command,
for instance, sum up all.

Now to print the `\hskip` for the latex, we need to wrap it
into a `%(xx)`, that is `%(scrub(\»{\\\\hskip \»))`.
We would like to have an indent by the level of the account,
that is

Assets:
  Bank:

instead of

Assets:
Bank:

in latex you can do this by adding, for example, `\hskip 3mm` in front of the
row with the asset name. The command in ledger is to get the level of
the account is `%(depth)`. So for the first column of our table we get
the following formatting command:

%(scrub(\\"{\\\\\\\hskip \\"))%(depth)%(scrub(\\"mm}\\"))\` plus the

account name, which is already nicely formatted with:
`%-(ansify_if(ansify_if(partial_account(options.flat), blue if color), bold if should_bold))

The part for the amount can also be left as is:

%(ansify\_if(justify(scrub(display\_total), 20, 20 + int(prepend\_width), true, color), bold if should\_bold)) %(!options.flat ? depth\_spacer : \\"\\")%(scrub(\\"\\\\\\\\\\\\\\\\\\"))\n

Ouf, so we finished the building of a row.

The final row is defined in the formatting starting with
%/.

Before the total, there should be again a `\hline`:

%/ %(scrub(\\"\\\\\\\\\hline\\")) \n

followed by the line with the total:

 & %\\$5%(scrub(\\"\\\\\\\\\\\\\\\\\\"))\n%(scrub(\\"\\\\\\\\\hline\\"))\n%/ %(prepend\_width ? \\" \\" \* int(prepend\_width) : \\"\\")

where %\\%5 means that the fifth %(xx) should be summed up, in our case the amounts.

Summary

I have given you a short overview over ledger and the advantages of it. Then I have shown you
how to use the –format command to create good-looking latex balance sheets. Have fun!