Developing a document class using a Writer2LaTeX-generated document as a starting point

Background

My university is finally starting to become more LaTeX-aware and has developed and published a document class for theses and memoirs. This class is based on the memoir class and is now distributed as part of TeX Live 2012.

In my department, at least one professor uses LaTeX proficiently. I have started to work with this person in order to make LaTeX kown to other members of the department and, in particular, to members of the research center that employs me.

Recently, while editing a thesis, I realized that many authors working with my research center were at a loss when it came to formatting their work according to the provided guidelines. After convincing the department that providing stylesheets and precise instructions would benefit everyone involved in authoring and editing, I was then given the job of developing templates for OpenOffice/LibreOffice and Microsoft Word. I have completed this task, and I would now like to create a corresponding LaTeX document class to promote the use of LaTeX as an alternative to the current word processor > InDesign > PDF workflow.

The main question

I have an OpenDocument Text template containing all the styles that authors are likely to need during the editing process. Because there is much differentiation in terms of text body paragraphs, block quote styles and the like, producing the LaTeX class from the ODT template seems to be most practical solution, as the LaTeX template needs to contain all of the same styles as the ODT template and remain aligned with it.

I already use Writer2LaTeX to typeset all of the documents that I produce for my research center. Writer2LaTeX version 1.2 (currently in beta) generates a suitable preamble when a given document’s formatting has been correctly defined using styles exclusively as oppposed to manual formatting. I have an absolute requirement (which I think is reasonable in this day and age) that XeTeX or LuaTeX be used, because the field I work in (early modern literature) requires true Unicode support and most documents are bound to contain multiple languages (often French, English and Latin and occasionally polytonic Greek and Cyrillic-based languages). That is why I use Writer2LaTeX 1.2 with the engine set to “XeTeX”. Usually, all I need to do is add fontspec commands to the preamble and then I can typeset the document without any further intervention on the generated LaTeX.

However, for a LaTeX-based workflow, copying a Writer2LaTeX preamble just won’t do. I need to develop a genuine class. My intuition is that I could use Writer2LaTeX as a starting point and keep the same command names for my class. These commands often begin with \textStyle and other such prefixes depending on the family of the ODT style from which they were generated. In this way, the class would retain concepts and distinctions that are close to OpenDocument’s style model. What I need to know is: can I just dump a preamble containing all the style commands I need into a class file? I do not know much about class development, but something tells me that it cannot be the best practice. What is the best route? Customizing Writer2LaTeX’s output by “opening the hood” and writing additional XSLT? I admit that I have yet to learn XSLT, for one thing.

Sub-question

How can I make Writer2LaTeX’s generated preamble more readable? In general, I am aware of the fact that LaTeX commands can be split on multiple lines by placing comment characters % at the end of individual statements, but is there a caveat to this approach in some particular cases? My goal is to make the class code as readable as possible: I would like it to look a bit like CSS, which in my opinion is one of the best-looking languages out there and is a charm to write.

To sum it up: should I just make Writer2LaTeX’s output more readable and fine-tune it by adding fontspec definitions and the like, or should I take inspiration from Writer2LaTeX’s output and write my class from scratch using the standard class development tools?

Edit: Here is an example of a preamble created by Writer2LaTeX. This was generated from one of my course assignment documents. The only command I have added myself is \setmainfont{Brill}.

% This file was converted to LaTeX by Writer2LaTeX ver. 1.2
\documentclass[letterpaper,12pt]{article}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{fontspec}
\setmainfont{Brill}
\usepackage{xunicode}
\usepackage{xltxtra}
\usepackage{polyglossia}
\setdefaultlanguage{french}
\setotherlanguage{english}
\usepackage{color}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{hhline}
\usepackage{hyperref}
% footnotes configuration
\makeatletter
\renewcommand\thefootnote{\arabic{footnote}}
\renewcommand\@makefnmark{\mbox{\textstyleFootnoteanchor{\@thefnmark}}}
\makeatother
% Text styles
\newcommand\textstyleEmphasis[1]{\textfrench{\textrm{\textit{#1}}}}
\newcommand\textstyleSuperscript[1]{\textsuperscript{#1}}
\newcommand\textstyleNumberingSymbols[1]{#1}
\newcommand\textstyleStephanusnum[1]{\textrm{\textsuperscript{#1}}}
\newcommand\textstyleFootnoteanchor[1]{\textrm{\textsuperscript{#1}}}
\makeatletter
\newcommand\cssection[1]{\textfrench{#1}}
\renewcommand\section{\@startsection{section}{1}{0.0cm}{0.559cm}{0.559cm}{\normalfont\normalsize\normalcolor\fontsize{16pt}{19.2pt}\selectfont\rmfamily\mdseries\cssection}}
\newcommand\cssubsection[1]{\textfrench{#1}}
\renewcommand\subsection{\@startsection{subsection}{2}{0.0cm}{0.28cm}{0.28cm}{\normalfont\normalsize\normalcolor\fontsize{12pt}{14.4pt}\selectfont\rmfamily\mdseries\itshape\cssubsection}}
\renewcommand\@seccntformat[1]{\csname @textstyle#1\endcsname{\csname the#1\endcsname}\csname @distance#1\endcsname}
\setcounter{secnumdepth}{0}
\newcommand\@distancesection{}
\newcommand\@textstylesection[1]{#1}
\newcommand\@distancesubsection{}
\newcommand\@textstylesubsection[1]{#1}
\makeatother
\raggedbottom
% Paragraph styles
\renewcommand\familydefault{\rmdefault}
\newenvironment{stylePii}{\clearpage\setcounter{page}{1}\pagestyle{Standard}
\thispagestyle{FirstPage}
\setlength\leftskip{0cm plus 1fil}\setlength\rightskip{0cm plus 1fil}\setlength\parindent{0cm}\setlength\parfillskip{0pt}\setlength\parskip{0cm plus 1pt}\writerlistparindent\writerlistleftskip\begin{french}\leavevmode\normalfont\normalsize\normalcolor\fontsize{24pt}{28.8pt}\selectfont\writerlistlabel\ignorespaces}{\end{french}\unskip\vspace{0.559cm plus 0.0559cm}\par}
\newenvironment{styleAuthor}{\renewcommand\baselinestretch{1.25}\setlength\leftskip{0cm plus 1fil}\setlength\rightskip{0cm plus 1fil}\setlength\parindent{0cm}\setlength\parfillskip{0pt}\setlength\parskip{0cm plus 1pt}\writerlistparindent\writerlistleftskip\begin{french}\leavevmode\normalfont\normalsize\normalcolor\upshape\writerlistlabel\ignorespaces}{\end{french}\unskip\vspace{0cm plus 1pt}\par}
\newenvironment{styleDate}{\renewcommand\baselinestretch{1.25}\setlength\leftskip{0cm plus 1fil}\setlength\rightskip{0cm plus 1fil}\setlength\parindent{0cm}\setlength\parfillskip{0pt}\setlength\parskip{0cm plus 1pt}\writerlistparindent\writerlistleftskip\begin{french}\leavevmode\normalfont\normalsize\normalcolor\upshape\writerlistlabel\ignorespaces}{\end{french}\unskip\vspace{0cm plus 1pt}\par}
\newenvironment{styleTextbody}{\renewcommand\baselinestretch{1.0}\setlength\leftskip{0cm}\setlength\rightskip{0cm}\setlength\parindent{0.559cm}\setlength\parfillskip{0pt plus 1fil}\setlength\parskip{0cm plus 1pt}\writerlistparindent\writerlistleftskip\begin{french}\leavevmode\normalfont\normalsize\normalcolor\writerlistlabel\ignorespaces}{\end{french}\unskip\vspace{0cm plus 1pt}\par}
\newenvironment{styleQuotations}{\setlength\leftskip{1.131cm}\setlength\rightskip{1.131cm}\setlength\parindent{0cm}\setlength\parfillskip{0pt plus 1fil}\setlength\parskip{0.559cm plus 0.0559cm}\writerlistparindent\writerlistleftskip\begin{french}\leavevmode\normalfont\normalsize\normalcolor\writerlistlabel\ignorespaces}{\end{french}\unskip\vspace{0.559cm plus 0.0559cm}\par}
\newenvironment{stylePv}{\setlength\leftskip{0cm}\setlength\rightskip{0cm}\setlength\parindent{0cm}\setlength\parfillskip{0pt plus 1fil}\setlength\parskip{0cm plus 1pt}\writerlistparindent\writerlistleftskip\begin{french}\leavevmode\normalfont\normalsize\normalcolor\writerlistlabel\ignorespaces}{\end{french}\unskip\vspace{0cm plus 1pt}\par}
\newenvironment{stylePi}{\setlength\leftskip{1.131cm}\setlength\rightskip{1.131cm}\setlength\parindent{0cm}\setlength\parfillskip{0pt plus 1fil}\setlength\parskip{0.559cm plus 0.0559cm}\writerlistparindent\writerlistleftskip\begin{french}\leavevmode\normalfont\normalsize\normalcolor\writerlistlabel\ignorespaces}{\end{french}\unskip\vspace{0.559cm plus 0.0559cm}\par}
\newenvironment{styleCitationanglais}{\setlength\leftskip{1.131cm}\setlength\rightskip{1.131cm}\setlength\parindent{0cm}\setlength\parfillskip{0pt plus 1fil}\setlength\parskip{0.559cm plus 0.0559cm}\writerlistparindent\writerlistleftskip\begin{english}\leavevmode\normalfont\normalsize\normalcolor\writerlistlabel\ignorespaces}{\end{english}\unskip\vspace{0.559cm plus 0.0559cm}\par}
\newenvironment{styleCitationsuivant}{\setlength\leftskip{1.131cm}\setlength\rightskip{1.131cm}\setlength\parindent{0cm}\setlength\parfillskip{0pt plus 1fil}\setlength\parskip{0cm plus 1pt}\writerlistparindent\writerlistleftskip\begin{french}\leavevmode\normalfont\normalsize\normalcolor\writerlistlabel\ignorespaces}{\end{french}\unskip\vspace{0.559cm plus 0.0559cm}\par}
\newenvironment{stylePiv}{\renewcommand\baselinestretch{1.0}\setlength\leftskip{0cm}\setlength\rightskip{0cm}\setlength\parindent{0cm}\setlength\parfillskip{0pt plus 1fil}\setlength\parskip{0cm plus 1pt}\writerlistparindent\writerlistleftskip\begin{french}\leavevmode\normalfont\normalsize\normalcolor\writerlistlabel\ignorespaces}{\end{french}\unskip\vspace{0cm plus 1pt}\par}
% List styles
\newcommand\writerlistleftskip{}
\newcommand\writerlistparindent{}
\newcommand\writerlistlabel{}
\newcommand\writerlistremovelabel{\aftergroup\let\aftergroup\writerlistparindent\aftergroup\relax\aftergroup\let\aftergroup\writerlistlabel\aftergroup\relax}
\newcommand\labellistListileveli{\textstyleNumberingSymbols{•}}
\newcommand\labellistListilevelii{\textstyleNumberingSymbols{•}}
\newcommand\labellistListileveliii{\textstyleNumberingSymbols{•}}
\newcommand\labellistListileveliv{\textstyleNumberingSymbols{•}}
% Footnote rule
\setlength{\skip\footins}{0.119cm}
\renewcommand\footnoterule{\vspace*{-0.018cm}\setlength\leftskip{0pt}\setlength\rightskip{0pt plus 1fil}\noindent\textcolor{black}{\rule{0.25\columnwidth}{0.018cm}}\vspace*{0.101cm}}
% Pages styles
\fancypagestyle{Standard}{\fancyhf{}
\fancyfoot[C]{\thepage{}}
\renewcommand\footrulewidth{0pt}
\renewcommand\thepage{\arabic{page}}
}
\fancypagestyle{FirstPage}{\fancyhf{}
\fancyfoot[L]{}
\renewcommand\footrulewidth{0pt}
\renewcommand\thepage{\arabic{page}}
}
\pagestyle{Standard}
\sloppy
\title{Le rôle de l’harmonie et de la musique dans le Timée de Platon}
\author{}
\date{2013-02-01}


Not very clean-looking, as you can see, but I am sure that something good can come of it. The template I am working on for my research center has a similar set of styles.

-
You have to change \documentclass{...} to \LoadClassWithOptions{<options>}{<class>} and instead of \usepackage{<package>}, use \RequirePackage{<package>}. Also your friend will be \PassOptionsToPackage{<options>}{<package>} –  Harish Kumar Feb 5 '13 at 2:17
Welcome to TeX.SX. –  Harish Kumar Feb 5 '13 at 2:18
Thanks @HarishKumar. I had been lurking for a long time before finally subscribing! The above-mentioned commands could definitely get me on the right track. Guess I’ll give it a try; the worst that can happen is that this first attempt could turn out to be a crappy class that lets me learn the basics and test stuff. –  Christian Gagné Feb 5 '13 at 2:36
@ChristianGagné Can you please somewhere on the web a sample output of Writer2LaTeX or alternatively post the first 20 lines? –  Yiannis Lazarides Feb 5 '13 at 2:48
@YiannisLazarides I have added the preamble from one of my personal documents. The class that I am working on would have a very similar set of commands (but there would be more heading styles and such). –  Christian Gagné Feb 5 '13 at 2:57

What I need to know is: can I just dump a preamble containing all the style commands I need into a class file?

Please don't and also don't take any inspiration from Writer2LaTeX’s computer generated preamble. My suggestion is that you strengthen your knowledge of LaTeX and you develop your own class as follows.

Ensure the requirements are written down in a Style Guide.

Try to first write a Style Guide or to extend an existing one to cover all the typographical requirements of the Department. The Guide should cover such items as paragraph indentation, fonts, font sizing, bibliography and citations, footnotes, captions for figures and the like. This should act as a written specification for the development of the class.

Familiarize yourself with class writing techniques.

Second if you unfamiliar with class writing, review some existing classes. I suggest you select the standard classes which is available at ctan as classes.dtx. I also suggest you review the tufte-book class, which was written without using .dtx file. My own preference is to use the doc, docstrip method of generating the class, as this can be used to provide a user manual, as well as describe the class in a literate manner to ease future maintenance and versioning releases. The .dtx system is also flexible enough to provide ancillary classes, such for articles or in-house report. You can also have the class generate templates for beamer and handout presentations.

Start the development

Once the above are completed, you ready to start programming the class but for one last activity. You will need to define what part of a the thesis typography the users will be allowed to change. These will have to be provided as options to the class. For a thesis, you will probably want to limit this flexibility as I presume consistency is of paramount importance. You will also need to examine carefully your requirements to decide what packages you will load with the class. Once you have collected all this information, my own preference would have been to modify the existing book class rather than go for one of the more framework classes such as memoir. I find the basic book class very well structured and a great way to learn more about LaTeX class development. Your decision to go with XeLaTeX or LuaTeX is good,a although personally I would have gone for XeLaTeX. However, make your class universal by checking for the underlying engine and having fall-back code for all the different engines (see the tufte-book code for inspiration).

Versioning and Back-ups

Use Git and Github or a similar service for managing revisions and dropbox for back-ups or whatever other system you familiar with. As you will be using XeLaTeX or LuaTeX it is important to also create a repository for some of the packages that are under active development such as fontspec and its dependencies and polyglossia.

How Far to Go? Many of the framework like classes offer too much flexibility for what you need. In your case you have O N E very specific style to implement. Make your class as bullet proof as possible. I wish you good luck and leave you with Knuth's words:

First there was one user and I took a lot of time to satisfy myself. Then I had 10 users, and a whole new level of difficulties arose. Then I had a hundred users and another level of things happened. I had a thousand users, I had ten thousand each of those were special phases in the development, important. I couldn't have gone with ten thousand until I'd done it with a thousand. But each time a new wave of changes came along, the idea was to have TeX get better, and not get more diverse as it needed to handle new things.

Note: I will come back later and add a few links on some of the more technical aspects.

-
The only point of disagreement for me is in whether to use a framework class. For me, knowing that memoir was designed for flexibility meant there was an excellent chance I could satisfy my university's sometimes esoteric requirements (and so far, it has). Having a framework that already includes the function of tocloft and similar packages means I don't have to drop to lower-level macros to do the same work. –  Mike Renfro Feb 5 '13 at 12:10
@MikeRenfro It depends on the time frame. If you do not have the time, load memoir it is an excellent class. Why I wouldn't use it for a thesis is that I wouldn't want people to tinker too much and memoir offers too much flexibility. Either way you are on the right track. –  Yiannis Lazarides Feb 5 '13 at 12:14
I understand. I do have a style guide for the templates. I shall augment it. In fact, I think I will write a separate one specifically for the LaTeX class, as the one I currently have contains word processor-specific instructions. I reckon it is better to take more time and develop a genuine, well-specified class. I will put the advice to good use. As for the level of customization allowed, it is indeed to be very low. The editors want documents that basically all look the same. I will definitely check out the tufte-book class. As for the engine, I also think XeTeX is the saner choice for now. –  Christian Gagné Feb 5 '13 at 14:10