## Wednesday, June 2, 2010

### Writing a novel using the memoir class in LaTeX (with notes for uploading to fanfiction.net)

For the last few months, I've been writing Identity using LaTeX, a document typesetting system often for the preparation of technical papers but applicable to the task of writing a novel, too. Why do I do this? For one, I often had conceptual issues with formatting using programs like Microsoft Word. While you can control to a fine degree how things look in LaTeX, often times the engine or class will give you very good results that need no further tinkering. Futhermore, LaTeX allows you to greatly decouple the content of something compared to its presentation. Especially using content in separate sub-files, you can present the same information in different ways depending on the needs of the situation.

The Basics

In addition, if you plan to upload to Fanfiction.net, I advise using a utility such as LaTeX2RTF to convert to something FFN understands. RTF is fine for FFN.

To compile LaTeX, you can simply type "pdflatex blah" in a command line to compile blah.tex. This will generate a PDF for your document...that is, provided there are no errors, anyway. But to start, you need a simple text file that you save as a .tex file.

The Preamble

LaTeX is all about structure, commands, and environments. A document begins with a preamble, which in turn begins with the \documentclass command: this tells LaTeX what class to invoke for the document (there are many; I use memoir) and what options the class allows that are used in this particular document. After one states the class, one can load various packages using the \usepackage command. The part of the preamble I use for class and packages looks something like this:

\documentclass[ebook, 10pt, openany]{memoir}
\usepackage{fourier}
\usepackage{hyperref}
\usepackage{verbatim}
\usepackage[calcwidth,nobottomtitles*]{titlesec}

The class is memoir, with options ebook (changing the page dimensions), 10pt (font size) and openany (chapters can begin on either left or right pages). The fourier package tells LaTeX to use the Utopia-fourier font. Hyperref allows the table of contents to directly link to the beginning of chapters. Verbatim gives several options for comment environments (which I'll touch on later), and the titlesec package allows me to change how the section titles look.

This is not the only part of the preamble, however: before the \begin{document} command (which actually starts the real document), you can define new commands and environments, redefine old ones, or otherwise tweak some formatting aspects to your liking.

For example, I define several commands that are basically just simple macros. You might notice at the end of my chapters, I have a line plugging the blog: For notes and commentary on this chapter and others, visit my blog at westofarcturus [dot] blogspot [dot] com. This is done (in a very simplistic sort of way) through a command I define in the preamble:

\newcommand{\blogline}{For notes and commentary on this chapter and others, visit my blog at westofarcturus [dot] blogspot [dot] com.}

This way, I don't have to type it out at the end of each chapter: I just type \blogline{} near the end, and it's always the same. That's another advantage of LaTeX: you can freely define commands and automate a lot of stuff that otherwise must be repeated.

You can add stuff to your preamble over time, as you get more and more familiar with LaTeX.

The document body

Your document truly begins with the \begin{document} command and must end with \end{document}. This, in a way, is a top level environment. You can wrap quotations in \begin{quotation} and \end{quotation}, and so on. The only fundamental difference between environments and commands is that environments come with begin and end commands like so, while plain commands begin and end with braces to wrap their arguments.

One of the first things you'll want to do is give your document a title and put your name on it. Identity begins like this:

\title{Identity}
\author{By Muphrid}
\date{Rev. \today}

This actually doesn't write a title or anything like that. For that, you need a final command to say that you're "done" with title stuff and put the title on a page: \maketitle - that will do it. For a novel, with a complex title page, this is woefully insufficient, but for us, it'll pass for now.

Once we're done with that, we can get to more interesting business. If you're like me, the next thing you want to do is have a table of contents. With LaTeX, this is easy: just type \tableofcontents and each time you compile, LaTeX will generate a .aux file with the page numbers of chapter headers and so forth, so that on the next run, it can generate a TOC list. That means whenever you make a change, you should compile the document twice: once for the TOC, once to finish.

After that, you can get writing! Writing a novel in LaTeX has a few quirks, but it's otherwise very easy. Use  (two grave accents) for opening double quotation marks and two apostrophes on the keyboard ('') for closing double quotation marks. Single quotes are just one of each. So no, LaTeX won't auto correct your quotation marks, but it's not like HTML, where you need to type out a five or six character entity to get it right. Other than that, separate new paragraphs with a blank line in between (so two hard returns, not one; if you refuse to word wrap in a text editor, you can use one hard return safely and not start a new paragraph). Use \emph{text} to emphasize (usually italicize) text. I personally define a \thought command so I can denote thoughts differently from run-of-the-mill italics if I choose to--again, this is a great thing about LaTeX, for if I change my mind and want thoughts to be double-underscored with glitter, I can do that by changing the command definition, rather than going back and doing that each time somebody thinks something.

The only other thing you need to know by then for just writing in LaTeX is sectioning commands. The memoir class allows you to use the \chapter command, so if you want a chapter entitled Journey to Jusenkyo, just type \chapter{Journey to Jusenkyo}, make a blank line, and start your first paragraph on the next. The same goes for sections. \section{Night Life of the D\=otonbori} begins act two of chapter three, for example. That's another nice thing about LaTeX: using the macrons needed for some forms of Japanese romanization is easy with the \={} command. Fiancee can be given its proper accent with \'{e} before the final e (as you might notice, you don't strictly need the braces when it's just the one character accented--fianc\'ee will do fine, as will Uky\=o, and so on).

FFN doesn't understand LaTeX (and I doubt they ever will), so again, use something like LaTeX2RTF to get something FFN will understand. A note on this particular utility: it has a couple bugs. First, it's not a true LaTeX parser, and as such, while it ignores ordinary comments in code (denoted by the percent sign, %), it will often simply skip over environments or code that it doesn't understand. I actually use this bug to my advantage. The second bug, however, is that it inserts an extra space between question marks and exclamation points and single or double quotes after them--use find and replace to fix this. Actually, it inserts extra space in a lot of other places, but this is the only point of consequence (FFN strips all the other space anyway).

On using the first bug, I actually put all the header and footer stuff in my chapters in special comment environments that the RTF converter doesn't understand but won't show up in PDF: hence, I can compile LaTeX and get something without teasers and stuff (in other words, I get a book), and I can run LaTeX2RTF and get what I want to upload to FFN.

Sample code

For people's perusal, I provide a basic idea of what the documents I use look like in tex code. Have fun with LaTeX! And to see how it looks in PDF, download Identity from Scribd.

This is the main file, Identity.tex.

\documentclass[ebook, 10pt, openany]{memoir} %comments after percent signs; this is the document class with options
%\documentclass[oneside, openany]{memoir} %I keep this line and uncomment it out (and comment the above) if I want to have the pdf in a one-sided format
\usepackage{fourier} %font package
\usepackage{verbatim} %comment environments

\newcommand{\separator}{\rule{\textwidth}{3pt}} %I use this to denote where I want horizontal rules in FFN uploads

\newenvironment{anote}{}{} %creates a blank environment %the lines below map this environment to a comment environment; you must do it this way (you can't just make the environment identical to comment in one stroke)
\let \anote \comment
\let \endanote \endcomment

\newcommand{\blogline}{For notes and commentary on this chapter and others, visit my blog at westofarcturus [dot] blogspot [dot] com.} %the blog line at the end of a chapter
\newcommand{\summary}{\textbf{\emph{Identity}, by Muphrid.} A tribe of Chinese sorcerers captures Ranma to purge emotions from the hearts of men. A continuation story, set after the end of the manga.} %The macro at the beginning of a chapter
\newcommand{\whatsup}[1]{\textbf{What's going on here?} #1} %The macro after the story summary. \textbf bolds; #1 is provided as the preview of the chapter.
\newcommand{\whatsnext}[3]{\textbf{Next:} #2 \textbf{#1 - Coming #3.}} %the macro at the end of the chapter, with a preview for the next. I originally had the arguments in a different order.

\newcommand{\fftitle}[2]{\begin{center} \textbf{#1} \\ \emph{#2} \end{center}} %this is how I get a title in format of bolded line above an italics line, centered.

\newcommand{\ffend}[1]{\begin{center} \textbf{\emph{Identity} #1 End} \end{center}} %and this is the macro at the end of a chapter chapter, as opposed to an act.

\newcommand{\pbreak}{\pfbreak} %this inserts a blank space in PDF between paragraphs for a scene break; I map it like so in order to do...
%\renewcommand{\pbreak}{\begin{center} \# \end{center}} %this; in FFN uploads, I map it to a single centered line with a pound symbol

\newcommand{\thought}[1]{\emph{#1}} %thoughts are italic
\newcommand{\thtdia}[1]{\emph{#1'}} %dialogue in thought is denoted with single quotes
\newcommand{\ellip}{\ldots} %just in case, I can change how ellipses are presented
\newcommand{\qs}{~} %this is the space between a double and single quote if someone is directly quoting someone else

\chapterstyle{thatcher} %this is for the memoir class; there are several possible chapter styles
\renewcommand \thesection{\Roman{section}} %section numbering in roman numerals

%all the below formats section titles with number on the first line, a line between, and title on a second line \titleformat{\section}[display]
{\Large \fillast}
{\oldstylenums{\thesection} {\titleline[c]{\rule{.5\titlewidth}{1pt}}}}
{-12pt}
{\Large \scshape}

\hyphenation{strike-throughs cinder-blocks o-ko-no-mi-ya-ki} %hyphenation aids for unfamiliar words to latex

%\includeonly{ch01,ch02,ch03} %when I want to upload to Scribd, I include only the chapters that are done and proofread; I keep a pdf on my computer that is up to date with everything I've written
\begin{document} %begins the document

\frontmatter %pages after front matter are numbered differently than main matter

%all the below is the title page \title{Identity}
\author{By Muphrid}
\date{Rev. \today}

\maketitle %makes the title?

\newpage %blank page

\tableofcontents %obvious

\newpage

\mainmatter %this is the main part of the document, starts with page 1 instead of i or ii

\part{Tribe of the Ki Sorcerers} %this first part of the book

\sloppy %with ebook dimensions, sloppy space management between words is preferred over nonsloppy (the default), which overfills lines; sloppy spacing underills lines but is much less noticeable with my page size

% I write the book in separate subdocuments; each ch01.tex etc. is just a dummy for each act; I did this because doing a chapter in a single file was simply too big for Notepad++ to handle without cpu lag, but for files under 100kb you probably don't need to do that. \include{ch01}
\include{ch02}
\include{ch03}
\include{ch04}

\end{document}

The following is a dummy chapter file, like the \include commands at the end of Identity.pdf.

\chapter{Journey to Jusenky\=o} %begins the chapter
%all this stuff is using \input instead of \include because you can't nest include; the only functional difference is that \include inserts newpages before the content included, but with chapters, this is done anyway (and not repeated, so there are no blank pages between chapters)
\input{ch03a0}
\input{ch03a1}
\input{ch03a2}
\input{ch03a3}
\input{ch03a4}
\input{ch03a5}
\input{ch03a6}

And for reference, ch03a6.tex. Compare this against chapter three, act six on FFN.

\section{The Last Right} %act title

\begin{anote} %everything in the anote environment doens't go into PDF but does go into the RTF

\summary{} %the summary macro at the beginning of the chapter, "Identity, by Muphrid..."

\whatsup{The Council of Elders has silenced Shampoo. Mute to all but her family, she watches, helpless to save Ranma, helpless to reclaim lost honor, and the time for battle against the Sorcerers is nigh.} %The preview of the coming chapter at the beginning

\separator{} %I replace this with a horizontal rule in RTF

\fftitle{The Last Right}{Chapter Three Finale} %the title in FFN; included here because LaTeX puts a title with \section, but this title is not well-formatted for upload to FFN

\end{anotetop}
Chapter text goes here.

Separate paragraphs are like so.

\pbreak{} %where I want a scene break

Dialogue,'' I said in \emph{another} paragraph.

\thought{I'm thinking in italics.}

\begin{anote} %end of chapter footer

\ffend{03} %Identity 03 End
\separator{} %hrule

\whatsnext{The war against the Ki Sorcerers begins with chapter four of \emph{Identity} - `Monsters and Demons''}{The Amazons stage a rescue attempt for Ranma, but the battle against the Sorcerers ends in cataclysm for both sides, tainting Jusenky\=o for miles around. The springs may be relatively tame now, but it won't be long before it's an abomination on this earth.}{June 4, 2010}

\blogline{} %Visit my blog at yadda

\end{anote}