Register Guidelines E-Books Search Today's Posts Mark Forums Read

 MobileRead Forums Program listings in ePub

 04-20-2013, 04:11 PM #1 rplantz Member   Posts: 18 Karma: 493474 Join Date: Mar 2011 Location: Northern California Device: Acer A500 Program listings in ePub Has anyone been successful at producing good looking program listings in ePub? I have self-published (at Lulu) one programming book that has over 100 program listings. I wrote that book in LaTeX, and the ebook version is pdf. It looks great. I am starting my second programming book. There is not a good workflow from LaTeX to ePub, so I have started with restructuredtext and processed it with Sphinx. The HTML version is very nice. The ePub version is nice in Calibre, but when I load it onto my Andorid tablet, things are pretty bad. For example, the line numbers do not align with their respective code lines. Line wrapping makes it very difficult to read the code. The equations I use in the book are simple algebra, so I can probably live with the lack of math support in ePub. I converted the vector graphics to svg, and that seems to display nicely. I hope that ePub3 will solve some of these issues, but there are still many ePub2 devices out there. Is pdf still the format of choice for books that have lots of computer programming code?
 04-21-2013, 01:02 PM #2 PageLab Connoisseur     Posts: 70 Karma: 515184 Join Date: Sep 2011 Location: Brasília Device: Kindle3, iPad, Nook, Kobo, Positivo Alfa You could use something like Highlight to generate a beautifully formatted version of your code in SVG or HTML, ready to embed in your ePUB. Here's a simple example of the code generated by Highlight inside an ePUB: http://bit.ly/11tFJly It's a free program, with lots of great customization options.
04-21-2013, 01:24 PM   #3
Tex2002ans
Fanatic

Posts: 558
Karma: 920327
Join Date: Jul 2012
Device: Nook
Quote:
 Originally Posted by rplantz There is not a good workflow from LaTeX to ePub, [...] The HTML version is very nice. The ePub version is nice in Calibre, but when I load it onto my Andorid tablet, things are pretty bad. For example, the line numbers do not align with their respective code lines. Line wrapping makes it very difficult to read the code.
The format is just not suited for this sort of thing (reflowable text + extremely precise layouts for code sections). You say that the HTML version looks fine, but the EPUB looks horrible (on Android)... Mind giving a sample of the HTML code that looks nice? It could be that the way that the HTML is laid out is something that does not work in EPUB at all.

Also, what reader are you using to read it on Android? There are many of them out there, and it could be rendering errors with the reading program itself.

If the lines of code are not too long, perhaps you could take advantage of some negative indents to help clarify seperate lines of code. (See attached test EPUB + image).

Quote:
 Originally Posted by rplantz The equations I use in the book are simple algebra, so I can probably live with the lack of math support in ePub. I converted the vector graphics to svg, and that seems to display nicely.
There is this thing called Mathjax, which you can use to embed in your books:

Although, doing this will limit the programs the users will actually be able to use to read the EPUB... but you will get beautiful scalable/copy/pastable equations.

There was also this topic a while back, you might get some tips out of it:

Quote:
 Originally Posted by rplantz I hope that ePub3 will solve some of these issues, but there are still many ePub2 devices out there. Is pdf still the format of choice for books that have lots of computer programming code?
Yes. PDF would still be your best bet. EPUB is just not well suited for complex documents such as this... BUT, you might be able to make it look ok.

As a last resort, you would be able to create an image of sections of code, and have those throughout the EPUB (very ugly... but compatible). Text in images though does not scale very well on the tiny devices.
Attached Thumbnails

Attached Files
 CodeBlockExample.epub (2.2 KB, 41 views)

 04-21-2013, 02:05 PM #4 Jellby frumious Bandersnatch     Posts: 6,540 Karma: 5000001 Join Date: Jan 2008 Location: Spaniard in Sweden Device: Cybook Orizon, Kobo Aura Note, anyway, that PDF does not mean letter/A4 size pages. You can create a PDF with a page size of 9×12cm and almost no margins, that would be quite readable in most devices.
04-21-2013, 02:35 PM   #5
JSWolf
Resident Curmudgeon

Posts: 39,774
Karma: 21371769
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Aura H2O, Sony PRS-650, Sony PRS-T1, nook STR, iPad 1, iPhone 5
Quote:
 Originally Posted by PageLab You could use something like Highlight to generate a beautifully formatted version of your code in SVG or HTML, ready to embed in your ePUB. Here's a simple example of the code generated by Highlight inside an ePUB: http://bit.ly/11tFJly It's a free program, with lots of great customization options.
I'm betting the that code inside the ePub uses a <pre></pre> and that causes one of the lines to be too long and not wrap. That just doesn't work.

04-21-2013, 06:49 PM   #6
PageLab
Connoisseur

Posts: 70
Karma: 515184
Join Date: Sep 2011
Location: Brasília
Device: Kindle3, iPad, Nook, Kobo, Positivo Alfa
Quote:
 Originally Posted by JSWolf I'm betting the that code inside the ePub uses a
and that causes one of the lines to be too long and not wrap. That just doesn't work.
Well, Highlight can do that, but there's an option (which is shown in the example ePub file) to use ordered lists instead of <pre> tags. Works nicely – but the code is crowded.

04-21-2013, 11:13 PM   #7
JSWolf
Resident Curmudgeon

Posts: 39,774
Karma: 21371769
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Aura H2O, Sony PRS-650, Sony PRS-T1, nook STR, iPad 1, iPhone 5
Quote:
 Originally Posted by PageLab Well, Highlight can do that, but there's an option (which is shown in the example ePub file) to use ordered lists instead of
tags. Works nicely – but the code is crowded.
All I can say (without looking at the code) is one of the lines did go past the edge of the ADE window. So whatever was done doesn't work.

04-22-2013, 08:31 AM   #8
PageLab
Connoisseur

Posts: 70
Karma: 515184
Join Date: Sep 2011
Location: Brasília
Device: Kindle3, iPad, Nook, Kobo, Positivo Alfa
Quote:
 Originally Posted by JSWolf All I can say (without looking at the code) is one of the lines did go past the edge of the ADE window. So whatever was done doesn't work.
Did you look at the page 2? There it is.

 04-23-2013, 08:54 PM #9 DaleDe Grand Sorcerer     Posts: 9,923 Karma: 5235628 Join Date: Aug 2007 Location: Grass Valley, CA Device: EB 1150, EZ Reader, Literati, iPad 2 You can use CSS to prewrap a pre section where your code resides. You should also use monospaced fonts for best results. This will usually result in readable code but expect some lines to wrap and that is usually ok in code. Dale
04-27-2013, 03:13 PM   #10
rplantz
Member

Posts: 18
Karma: 493474
Join Date: Mar 2011
Location: Northern California
Device: Acer A500
Quote:
 Originally Posted by Tex2002ans The format is just not suited for this sort of thing (reflowable text + extremely precise layouts for code sections). You say that the HTML version looks fine, but the EPUB looks horrible (on Android)... Mind giving a sample of the HTML code that looks nice? It could be that the way that the HTML is laid out is something that does not work in EPUB at all.
Ah, I see what it's doing, creating a table for the line numbering:
Code:
<div class="highlight-c"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41</pre></div></td><td class="code"><div class="highlight"><pre><span class="cm">/*</span>
<span class="cm"> * convertHex.c</span>
<span class="cm"> * then echoes it in hexadecimal and in decimal.</span>
<span class="cm"> * Assumes that user does not make mistakes.</span>
<span class="cm"> * Bob Plantz - 4 June 2009</span>
<span class="cm"> */</span>

<span class="cp">#include &lt;stdio.h&gt;</span>
<span class="cp">#include &lt;unistd.h&gt;</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">aChar</span><span class="p">;</span>

<span class="n">printf</span><span class="p">(</span><span class="s">&quot;Enter an integer in hexadecimal: &quot;</span><span class="p">);</span>
<span class="n">fflush</span><span class="p">(</span><span class="n">stdout</span><span class="p">);</span>
...

04-27-2013, 03:20 PM   #11
rplantz
Member

Posts: 18
Karma: 493474
Join Date: Mar 2011
Location: Northern California
Device: Acer A500
Quote:
 Originally Posted by Jellby Note, anyway, that PDF does not mean letter/A4 size pages. You can create a PDF with a page size of 9×12cm and almost no margins, that would be quite readable in most devices.
My current book, written with LaTeX, was designed for 8-1/2 x 11, double-sided printing. When I first made the pdf available, students pointed out that the margins were too wide, and the binding gutter was a hassle. With a little arithmetic, I gave the pdf version 0.1" uniform margins but kept the pagination the same as with the print version. So the professor can refer to a page number, and that works for both the pdf and paperback versions.

--Bob

 Tags code, listings, programming