Authors and publishers have written on this topic extensively. Blog posts about it periodically gets posted to HN.
Most of the cost to publish a book is upfront. Editing, laying, cover art, diagrams, etc.
It costs less than $2 to print a book in bulk quantities.
Margins suck. That is why publishers go out of business all the time. Margins aren't any better on ebooks. It costs money to maintain a digital store front.
For an example of the costs of digital distribution in another field, look at Good Old Games. They are a digital distribution platform and they barely break even / turn a profit. (A bit more complicated since they also do some of the porting work)
Digital distribution has super low per unit costs but huge upfront costs.