View Single Post
Old 01-25-2010, 04:04 AM   #1
Sweetpea
Grand Sorcerer
Sweetpea ought to be getting tired of karma fortunes by now.Sweetpea ought to be getting tired of karma fortunes by now.Sweetpea ought to be getting tired of karma fortunes by now.Sweetpea ought to be getting tired of karma fortunes by now.Sweetpea ought to be getting tired of karma fortunes by now.Sweetpea ought to be getting tired of karma fortunes by now.Sweetpea ought to be getting tired of karma fortunes by now.Sweetpea ought to be getting tired of karma fortunes by now.Sweetpea ought to be getting tired of karma fortunes by now.Sweetpea ought to be getting tired of karma fortunes by now.Sweetpea ought to be getting tired of karma fortunes by now.
 
Sweetpea's Avatar
 
Posts: 9,707
Karma: 32763414
Join Date: Dec 2008
Location: Krewerd
Device: Pocketbook Inkpad 4 Color; Samsung Galaxy Tab S6
LaTeX, a way to PDF

I'm always looking for new (for me) formats to create my books in. I have two readers that I use regularly. One is the JE-100, an LCD, WinCE based reader, on which I use Mobipocket (for various reasons) and on which I'll probably keep on using this format. But my other reader is a BeBook Mini (Hanlin v5) on which I now also read Mobipocket format. I don't like the Epub implementation (lack of top/bottom margin, weird page number in the right margin, no progress bar visible and other irriations...). And while the Mobipocket format has less irritations, it still isn't exactly what I want (the main irritation with that format is the fact that it doesn't remember what font you've used last, I always have to select the font I want to use, every time I open the book again).

So, I was thinking about transforming my HTML sources (which I use to generate both Mobipocket and Epub books) into PDF. I first used a HTML2PDF converter, but found the lack of hyphenation in HTML (which doesn't bother me with the Mobipocket implementation) generated some highly irritating sentences in PDF (in Mobipocket, I might get a few words on a sentence with large spacings between the words, in the PDF document, I got large spacings between the letters of the words...). I finally decided to give up on the HTML2PDF directly and started looking into LaTeX.

The learning curve of LaTeX is rather steep. At least, if you have to figure out everything from scratch (with a little help from knowledgeable people). But, I won't be stopped by a few setbacks, so, I got to work.

I first installed MikTex 2.8 and then TexNicCenter (1.0 Stable RC1). And then it was time for my first LaTeX PDF document.

Code:
\documentclass[12pt,openany,oneside]{book}
\begin{document}

Hello World!

\end{document}
It's that easy! Except... I've only a Mini, which is a 5" screen. Don't even want to try this document on it! Those huge margins would eat away my screen and don't even mention the A4 document... So, after a tip from Jellby, I found out about the memoir and geometry classes. After some reading, I thought I had figured it out:

Code:
\documentclass[12pt,openany,oneside]{memoir}

\usepackage[papersize={4in,5in},margin=3mm]{geometry}

\begin{document}

Hello World!

\end{document}
I have a 5" device, so, figuring out the width and height of it wasn't too difficult (ever heard of the Pythagorean theorem?). I also did want a small margin (one of my larger irritations with epub is the lack of top and bottom margin). So, after I created the PDF again (very easily done, I must say!), I saw a nice 4x5" page, with my Hello World on it. So, my next step was to put some real text in it. Ipsum.com to the rescue. Oh, and I'll also need a chapter. So far, I've only read one book that is completely chapterless. So, chapters are kind of mandatory.

Code:
\documentclass[12pt,openany,oneside]{memoir}

\usepackage[papersize={4in,5in},margin=3mm]{geometry}

\begin{document}

\chapter{1. Lorem}
\label{sec:1. Lorem}
Lorem ipsum dolor ...

<snip>

...velit tellus vitae velit.

\end{document}
Hmm, not exactly what I wanted, It shows a huge "Chapter 1" above my "1. Lorem". I had by now also downloaded the source of a book Jellby had made ("Fábulas literarias"). While I can't read Spanish for the life of me, it did have some nice examples for chapters and such (hey, better well stolen than badly made yourself!).

Code:
\documentclass[12pt,openany,oneside]{memoir}

\usepackage[papersize={4in,5in},margin=3mm]{geometry}

\makechapterstyle{normal}{%
  \setlength{\beforechapskip}{0pt}
  \setlength{\midchapskip}{0pt}
  \setlength{\afterchapskip}{20pt}
  \renewcommand{\printchaptername}{}
  \renewcommand{\printchapternum}{}
  \renewcommand{\chaptitlefont}{\centering\LARGE\clfamily}
  \setsecnumdepth{chapter}
}
\makechapterstyle{simple}{%
  \setlength{\beforechapskip}{50pt}
  \setlength{\midchapskip}{10pt}
  \setlength{\afterchapskip}{20pt}
  \renewcommand{\chapnamefont}{\normalfont\Large\bfseries}
  \renewcommand{\chapnumfont}{}
  \renewcommand{\chaptitlefont}{\chapnamefont}
  \setsecnumdepth{part}
}
\chapterstyle{simple}

\begin{document}

\chapter{1. Lorem}
\label{sec:1. Lorem}
Lorem ipsum dolor ...

<snip>

...velit tellus vitae velit.

\end{document}
I've no idea what it exactly do (actually, I do have an inkling, but don't ask me to explain it). For example, if I remove the {normal} template, it won't shop the {simple} correctly. Don't ask me why. It works, so I'm happy with it. I did change one thing from Jellby's example. I like to waste some space in front of my chapter, so I enlarged the size of the \beforechapskip. I also understand why the chapter doesn't get a number anymore. In LaTeX a document is divided in various elements. "Chapter" is one, and that one gets a number. "Part" is another, and that one won't get a number. So, the \setsecnumdepth{part}, tells that the chapter should get the numbering of a "part", which is none... Remember, I want to use my existing HTML files, and they already have a chapter number for chapters if required, I will have to change the format of the HTML files (replacing HTML format with LaTeX format), but I don't want to change the content of the files.

So, now I have a nice document, with a chapter and text. But I still don't have a page number (so I can see my progress in the book, it's something I really want). Back to the drawing board!

Code:
\documentclass[12pt,openany,oneside]{memoir}

\usepackage[papersize={4in,5in},margin=3mm]{geometry}

\makechapterstyle{normal}{%
  \setlength{\beforechapskip}{0pt}
  \setlength{\midchapskip}{0pt}
  \setlength{\afterchapskip}{20pt}
  \renewcommand{\printchaptername}{}
  \renewcommand{\printchapternum}{}
  \renewcommand{\chaptitlefont}{\centering\LARGE\clfamily}
  \setsecnumdepth{chapter}
}
\makechapterstyle{simple}{%
  \setlength{\beforechapskip}{50pt}
  \setlength{\midchapskip}{10pt}
  \setlength{\afterchapskip}{20pt}
  \renewcommand{\chapnamefont}{\normalfont\Large\bfseries}
  \renewcommand{\chapnumfont}{}
  \renewcommand{\chaptitlefont}{\chapnamefont}
  \setsecnumdepth{part}
}
\chapterstyle{simple}

\makeoddfoot {plain}{}{}{\footnotesize page \thepage\ of \thelastpage}
\makefootrule {plain}{\textwidth}{\normalrulethickness}{\footruleskip}

\makeoddhead {plain} {}{}{\footnotesize\thetitle}
\makeheadrule {plain}{\textwidth}{\normalrulethickness}

\pagestyle{plain}

\title{Book Title}
\author{A. Uthor}

\begin{document}

\chapter{1. Lorem}
\label{sec:1. Lorem}
Lorem ipsum dolor ...

<snip>

...velit tellus vitae velit.

\end{document}
So, I make a footer and header. The footer contains "Page x of y", the header the title of the book. There's a line between the body of the text and the header and footer. I also had to add a title for the book (naturally!) and, to make it complete, I also added the author. Finally, the \pagestyle{plain} writes the header and footer. Or rather, it should! After generating, where did my header and footer go??? If I remove the "geometry" line, I do see them and it does look nice (though, the page is again A4...)! Something needs to be changed with the "geometry" line...

After some more digging (and especially some errors! as at one point I did see my header and footer, but simply lost part of my text, which were, I presume, "written" behind them...", I found my answer" "includeheadfoot". Almost done, but there's a lot of wasted space between the header and the body and the footer and the body.

Code:
\documentclass[12pt,openany,oneside]{memoir}

\usepackage[papersize={4in,5in},margin=3mm]{geometry}

\makechapterstyle{normal}{%
  \setlength{\beforechapskip}{0pt}
  \setlength{\midchapskip}{0pt}
  \setlength{\afterchapskip}{20pt}
  \renewcommand{\printchaptername}{}
  \renewcommand{\printchapternum}{}
  \renewcommand{\chaptitlefont}{\centering\LARGE\clfamily}
  \setsecnumdepth{chapter}
}
\makechapterstyle{simple}{%
  \setlength{\beforechapskip}{50pt}
  \setlength{\midchapskip}{10pt}
  \setlength{\afterchapskip}{20pt}
  \renewcommand{\chapnamefont}{\normalfont\Large\bfseries}
  \renewcommand{\chapnumfont}{}
  \renewcommand{\chaptitlefont}{\chapnamefont}
  \setsecnumdepth{part}
}
\chapterstyle{simple}

\setlength{\headsep}{1mm}
\setlength{\footskip}{7mm}

\makeoddfoot {plain}{}{}{\footnotesize page \thepage\ of \thelastpage}
\makefootrule {plain}{\textwidth}{\normalrulethickness}{\footruleskip}

\makeoddhead {plain} {}{}{\footnotesize\thetitle}
\makeheadrule {plain}{\textwidth}{\normalrulethickness}

\pagestyle{plain}

\title{Book Title}
\author{A. Uthor}

\begin{document}

\chapter{1. Lorem}
\label{sec:1. Lorem}
Lorem ipsum dolor ...

<snip>

...velit tellus vitae velit.

\end{document}
The \headsep sets the separation of the header and the body. 1mm should be more than enough. The \footskip defines the distance between the footer and the body. 1mm should be enough there too, right? Wrong! Only at 7mm did the line not go over the text of my body at the first page... If anybody could explain, I'm listening.

But another problem has arisen. Now, my bottom margin is rather big. That is, the space between my page number and the bottom of the page. I couldn't figure out that one, until I put the /setlength commands above the "geometry" line. I also changed my margins a bit, so I only had a tiny margin on the top and bottom and a somewhat bigger on the side.


Code:
\documentclass[12pt,openany,oneside]{memoir}

\setlength{\headsep}{1mm}
\setlength{\footskip}{7mm}
\usepackage[papersize={4in,5in},hmargin=3mm,vmargin=1mm]{geometry}

\makechapterstyle{normal}{%
  \setlength{\beforechapskip}{0pt}
  \setlength{\midchapskip}{0pt}
  \setlength{\afterchapskip}{20pt}
  \renewcommand{\printchaptername}{}
  \renewcommand{\printchapternum}{}
  \renewcommand{\chaptitlefont}{\centering\LARGE\clfamily}
  \setsecnumdepth{chapter}
}
\makechapterstyle{simple}{%
  \setlength{\beforechapskip}{50pt}
  \setlength{\midchapskip}{10pt}
  \setlength{\afterchapskip}{20pt}
  \renewcommand{\chapnamefont}{\normalfont\Large\bfseries}
  \renewcommand{\chapnumfont}{}
  \renewcommand{\chaptitlefont}{\chapnamefont}
  \setsecnumdepth{part}
}
\chapterstyle{simple}

\makeoddfoot {plain}{}{}{\footnotesize page \thepage\ of \thelastpage}
\makefootrule {plain}{\textwidth}{\normalrulethickness}{\footruleskip}

\makeoddhead {plain} {}{}{\footnotesize\thetitle}
\makeheadrule {plain}{\textwidth}{\normalrulethickness}

\pagestyle{plain}

\title{Book Title}
\author{A. Uthor}

\begin{document}

\chapter{1. Lorem}
\label{sec:1. Lorem}
Lorem ipsum dolor ...

<snip>

...velit tellus vitae velit.

\end{document}

I now have a nice basic template for my books. I've tried to change the default font, and managed doing that, but the font I really like doesn't seem to work (http://www.tug.dk/FontCatalogue/venturis2/). I did install it, but I get an error when I try to create the PDF. I finally did manage to install the TTF I'm using on my Mini with mobipocket books so, at least that's working (only took me the entire weekend to get it working )

Now, this works on my computer screen, but how does it look on my BeBook Mini?

Well, apparently, a 5" screen doesn't mean my screen is 4" by 3". When I put the ruler beside the screen, it measures 10cm by 7.5cm (more or less). Which is less than 4x5". But when I put those numbers in, it looks horrible. So, after a lot of tweaks, I settled on 4x5.3". That way the page is large enough, the top and bottom margins aren't huge and the font size is comfortable to read (for me).

Now I can write an application which can transform my HTML files into TeX files (not only replacing HTML tags, but also the HTML characters, such as &mdash; and all those accented letters...) But I think that part will be easier than figuring out the LaTeX commands, as my HTML is all custom made and very simple in syntax. After that, I'll need to really figure out how to make new chapter templates, as not all chapters are created equal... Also, how to make links (if that actually works in PDF) for my TOC's...

------------------------

If anybody has some comments, improvements, hints, tips, extras, anything, I'm here, listening! (or rather, watching...)
Sweetpea is offline   Reply With Quote