Monday, November 28, 2011

The Battle over Assembly Comments

I've been working toward having a single file, asm64.tex, to serve as the source for the printed book (pdf format), the Kindle e-book (mobi format) and the Nook e-book (epub format).  My adventures with htlatex are starting to gel into a reasonable plan, except for assembly language comments.

Assembly code is normally divided into columns like this:

label:   instruction   operands      ; comments

My habit is to start the instructions in column 9 with operands starting in column 17.  This looks pretty good, but many of my lines of code are a little long.  I originally shrank them somewhat to fit the width of the printed book (about 6 inches).  Then I ran into the Kindle.

The Kindle is limited to 47 characters of monospaced text per line or 65 characters in landscape mode.  I was originally fairly happy when I discovered the landscape mode.  Nearly all my comments were 65 or fewer characters.

Then I borrowed my son's Nook.  The Nook apparently did not have a landscape mode.  I opted from the start to make the Nook version be the same as the Kindle version, so I have to live with the portrait mode.

I have gone through the code once (or twice) shrinking comments and spreading comments out onto 2 lines to make the book look fairly good on the Kindle with 47 characters, but I was not too happy with short lines for the printed version of the book.  So after some thought I decided to dive in and solve the problem with a bash/awk script.

I wrote a fairly good script which tries to break long comments near the middle and apparently break one line into 2 lines is sufficient for my needs.  I was afraid that I would need more robust code, but I'll leave it as is for now.

My script does allow a command-line parameter for the number of columns.  I need to borrow the Nook again and study it in detail.  Perhaps it has a smaller monospaced font which will allow wider lines.  I also don't know precisely the number of characters across the Nook screen.  I will be prepared for updating the Kindle and Nook books fairly soon.  I have also found a moderate number of typos, so I might update the printed book on Create Space as well.

Hopefully I will get around to doing some Windows assembly language during the Christmas break...

Friday, November 11, 2011

Getting Better Math from htlatex

I am happy to report that I have solved one more issue involved with producing good ebooks from a LaTeX source.  So far I have discovered how to use a configuration file to force htlatex to produce png images for displayed math and tables.  This can also be done to render arrays and eqnarrays as images.  Unfortunately when I view my equations and tables on the Kindle they look a little small and grayer than normal text.

The solution to these problems is to modify /etc/tex4ht/tex4ht.env changing the default parameters for the dvipng command.  The original line looks like this

    Gdvipng -T tight -x 1400 -D 72 -bg Transparent -pp %%2:%%2 %%1 -o %%3

My updated file has this line

    Gdvipng -T tight --gamma 1.5 -x 1600 -D 72 -bg Transparent -pp %%2:%%2 %%1 -o %%3

The -x option is a magnification so my output is about 14% larger.  The --gamma 1.5 option sets the gamma value (default 1) to 1.5 to force the text to appear darker.

Here is what I would normally get:


Here is what I get with the revised parameters for dvipng:


This is darker without being harsh and is large enough to make my rendered math a little more readable on Kindles, Nooks and other e-readers.

Thursday, November 10, 2011

The Winner is --- htlatex!

I have finally run into the right choices for using htlatex to produce a good file for the Kindle.  It turns out that Eitan Gurari (developer of tex4ht) took care of my needs after all.  You can supply the name of a configuration file on the command line for htlatex:

    htlatex asm64.tex asm64

In my case the latex file is "asm64.tex" and the configuration file is "asm64.cfg", which contains

    \Preamble{pic-tabular}

    \begin{document}

    \EndPreamble

The first line contains the magic incantation "pic-tabular" which transforms all latex tabular sections (tables) into pictures automatically.  There are also "pic-tabbing",  "pic-array", "pic-eqnarray",  "pic-fbox" and "pic-m" options which I may eventually need.

After changing all my \includegraphics commands to refer to .jpg files, I had a nice asm64.html file which looked fine with firefox.  I tried using kindlegen which did a poor job of generating a .mobi file.  Then I used calibre to create a .mobi and this was nearly perfect.  I have a few images made from xfig which have lines above and below them which I would prefer to eliminate and I need to include a cover image.

This is quite a good solution.  All the equations and tables looked great.  I would have liked larger rendering which I can probably arrange for, but overall this is better than my hand-written html code with very little additional effort after producing the original version.

Thanks to Eitan Gurari for his excellent work!

Friday, November 04, 2011

Portable Book Format

Now that I have published Kindle and Nook versions of my assembly book I have some experience with book formatting.  LaTeX produced an excellent printed book, but it required a huge amount of changes to convert the book into HTML for conversion to Kindle (mobi) format.  Fortunately the Nook format (epub) is similar enough to mobi that the Calibre program produced an acceptable epub book automatically from the mobi book.

However I now have 2 versions of the book to maintain which is not desirable.  In the long run it will be better to find or create a single format for a book which can be converted automatically to multiple format