| July 13, 2010 6:38 pm

In the past few months, I’ve written several articles about the ways LyX can be customized.  These included a brief introduction to layout files and how they relate to document classes in addition to a tour of character styles and how you can add them to the LyX local layout.  What I have not written about, though, is one of the most important ways of extending LyX, the module.

Ever since version 1.6 of LyX, the core LyX developers have worked very hard to make LyX a modular system.  Instead of hard coding values and options – as they had done previously – they’ve instead placed all of the needed information in configuration files that can be edited and modified by users.  This is a Very Good Thng.  It means that (given enough motivation), you can customize LyX to behave exactly was you wish.  You can change the default document classes, styles, fonts and anything else that your little heart desires.  It also means that any extensions you create will exist as first-class citizens.  They have access to all of the internal wizardry as the default document classes and features.

In order to make this transition work, it was necessary to have certain chunks of code that could be switched on when needed and switched off when not required, and that’s how “modules” were born.  In a lot of ways, you might think of modules as document plugins.  They let you do stuff that, while not required, can greatly improve your editing experience.  (Modules are switched on and off from the “Document Settings” pane.)

They let you do things like run Sweave code, add additional theorem environments, or apply a different style to your text.  But unlike a layout file, they are not tied to any particular document class or type; which makes them very powerful.  In fact, nearly all of them they can be used with any document type that LyX supports.

There are two main types of modules:

  1. Modules that contain all of the code needed to implement a particular feature (or modify an existing one) and do not reference external packages.
  2. Modules that load existing LaTeX styles/packages and translate their features into insets, commands and headings.

In both cases, modules serve as a container for the code.  You can use a module to package code that would normally live in a document layout file, or you can use code that might have been placed in the local layout.  The code may be a document header, a new LaTeX command, a character style, a custom inset, or any other type of user customization that LyX supports.

The biggest difference between using a module instead of a layout, of course, is that when the code is placed in a module, you can then add it to any document on your system.  This makes modules a tremendously flexible way to reuse customizations that you’ve created.  In fact, in nearly all cases, they are the best way to package your customizations for use on your own system or for sharing with others.

In this article, I’d like to introduce the module.  I will explain its position in the larger LyX/LaTeX “big picture”, its internal anatomy and provide a few examples of how it can be used to add new features.

The Big Picture: A Refresher Course

Before jumping right into code and detailed specifics, however, it might be valuable to provide a brief refresher on how LyX and LaTeX work.  Specific examples are always better once you understand the theory.

LaTeX uses several important files.  These are document classes (ending with the extension, .cls) and styles/packages (which end with the extension, .sty).  Classes and styles are what tell LaTeX whether a block of text is a chapter, figure legend, table or footnote.

Likewise, LyX also uses  two main types of configuration files.  These are layout files (which correspond to LaTeX document classes) and modules (which correspond to LaTeX packages/styles).

A particular document may only use one document class/layout file, but many packages/styles/modules.  As a result, packages/styles/modules become an important way to modify the appearance of a document class or to add additional features such as new float/inset types or headings.

Note: Additional Information about how LaTeX and LyX work together can be found in the article, Customizing LyX: Create an NIH Grant Proposal Template.  See the section entitled, “Understanding the Big Picture.”

Anatomy of a Module

There are two items that many LyX modules have in common.  These are:

  • the module declaration and description (mandatory), which give the module a unique name and provide a summary of what the module does, and
  • the “Requires” statement (optional), that specifies which LaTeX packages are needed by the module and prompts for LyX to look for them.

After the “Requires” statement, the modules can look wildly different.  This is because they can include any valid LyX configuration code such as header definitions, character styles, or custom insets.

Module Declaration

The module declaration appears at the very beginning of the module and includes the \DeclareLyXModule command and the module name.  The description then follows, offset by the #DescriptionBegin and #DescriptionEnd tags.  A simple example might have the form:

#\DeclareLyXModule(Module Name)
#This module provides several unique commands to aid with the typesetting of complex documents.

All of the commands and commentary in the Module declaration should be preceded by the numeral sign (#).  Any line breaks in the description should also be preceded by the numeral sign.

The “Requires” Statement

The requires statement directly follows the module declaration and description.  In it, you list any LaTeX pacakges required by that particular module.  The example (which is from the drop caps module discussed below), requires that both the lettrine and the stringstrings packages be installed:

Requires     lettrine,stringstrings


With the basic anatomy of a module out of the way, it may be helpful to look at two real-world examples.

The first example shows how to create a custom inset and LaTeX command.  It does not require any specialized packages other than those installed in a typical LaTeX distribution.  The second example, however, is slightly more complex.  It shows how to reference external packages, have LyX check whether they are installed and creates a new LaTeX command for automatically generating drop-cap letters.

Longtable Floats

One of the most useful features of LaTeX is the ability to position text, figures and other document objects automatically through the use of floats.  When used correctly, floats can help you to avoid a large number of formatting complications while ensuring that figures are placed in a consistent manner.

But even though they are tremendously useful, floats suffer from one major limitation.  They can’t contain any content that is over a page in length.  While this limitation is very easy to work around for figures, it presents serious complications for tables.  In many types of publications, it is common to include tables that are more than a single page in length.  To include this type of material in LaTeX document, therefore, means that you must position it manually (usually after the document has been completely written).

Excessive manual formatting (sometimes referred to as “finger painting”) is something that you want to avoid when working with LaTeX for both philosophical and practical reasons.  In the first place, it’s completely anathema to the idea of semantic markup.  In the second, trying to manually specify the location of document elements is a great deal of work.

Luckily, it is possible to create a custom LaTeX command that, when used in conjunction with long tables, can  cause them to behave in a manner similar to that of a float.  This command is \afterpage, from the “afterpage” package.  The \afterpage command causes any content in its argument to be expanded after the current page has finished its output.  As a result, \afterpage can be used to float the table to the top of the next page without disrupting the text flow.

Module Code

The longtable module defines a new command that then allows you to use \afterpage within a LyX document.  To make its behavior more consistent with the other floats in LyX (figure, table), it defines a custom inset.  This is what the code looks like:

InsetLayout Custom:Longtable

LyXType                                 custom
Labelstring                             longtable
Decoration                             classic

Color                                  collapsable
Size                                    Small

MultiPar                                 true

LatexName                            longtablepage
LatexType                              command
Requires                                afterpage

\afterpage{\clearpage #1}}


This particular inset code has several distinct sections.  First, is the InsetLayout declaration and the inset’s category and name (Custom:Longtable).  Second, is a configuration block that describes the type (custom) and what label should be used, in addition to its appearance.  In this case, the label will be “longtable” and it will appear as a “classic”, collapsible inset.  These options were chosen to match the style of the “figure” and “table” floats already in LyX.  The third block tells LyX that this particular inset should allow line breaks and more  than a single paragraph of content (MultiPar = true).

The final block of information gives the name of a new LaTeX operation, states that it is a command, and lists any packages that are needed for the command to be executed.  The preamble section is the code that will be listed in the document preamble and gives the LaTeX definition of \longtablepage.

Drop Case Letters Using lettrine and stringstrings

Drop Cap Initials - QUntil the 18th century and for brief periods in the 19th and early 20th century, books were often decorated with initials and ornaments.  The earliest books were decorated by hand and some of the most beautiful artwork of the middle ages can be found in these tomes, such as the Book of Kells.  Later on, the books were decorated through the use of woodcuts and copper engravings.

Many of the initials and ornaments are abstract, but many also include pictures that provide insights into how those of the past thought and felt.  In the case of the Book of Kells, it combines the religious iconography of the Romans with the motifs and designs of the British Isles in the early medieval ages, showing how Roman Christianity was adapted to mesh with Irish life.


Because of their decorative value and historical interest, there has been a great deal of interest in adding initials and other textual decorations to LaTeX documents. And as a result, there are many packages which allow you to do so.  One of the most popular of these is called lettrine.

Lettrine specializes in a particular type of decorated initial known as a drop-cap.  With a drop cap, the initial sits within the margins and runs several lines deep into the paragraph, pushing some of the normal-sized text off of these lines.  This allows for the top and left margins to remain flush, thereby maintaining the layout grid of the page.

When working directly with LaTeX, lettrine makes it very easy to create drop cap letters.  For a basic drop-cap, you first add the package to your document and then make use of the \lettrine command:


\lettrine{L}{orem ipsum} dolor sit amet …

Drop Cap Initials - Llettrine takes two arguments.  The first is the letter that is to be dropped and the second includes additional text to be typeset in a bridge font that helps to transition from the dropped case to the normal paragraph typeface.  (The default setting for the bridge font is a small caps version of the document’s primary font.)  But while lettrine is easy to use from LaTeX, there is no good way to make use of it from LyX without specifying the the lettrine arguments with LaTeX code (in LyX parlance, referred to as evil red text).


The best way to use lettrine in LyX would be to create a custom character style.  However, because the character style inset can only accept a single argument, it is necessary to create an “interpretive” command that can detect the first letter of the text, and split it into two separate chunks of text that can then be fed to \lettrine.

This is where the stringstrings package comes in.  Stringstrings provides a large number of routines for the manipulation of strings.  These not only cover cosmetic application, such as the changing of letter cases or removal of character classes, but also provide functions for programming.  Unfortunately, stringstrings is not always included as part of a standard LaTeX installation and you may need to install it separately.

Note: For instructions for installing LaTeX packages, see this article.

Module Code

The dropcaps module, like in the case of the longtable module defines a custom character style and inset command.  Here is what the code looks like:

InsetLayout CharStyle:DropCap

LyxType                              charstyle
LabelString                         dropcap

LatexType                           command
LatexName                         dropcap
Family                             Serif
Shape                              Smallcaps

\newcommand{\dropcap}[1] {%
\substring[q]{#1}{2}{$} %


Again, there is several different blocks of code.  First, the type of inset is defined and given a name (dropcap).  Next, a LaTeX operation is defined, given a name (again, dropcap) and its appearance is specified (the character style will appear in small caps serif font).  Finally, the LaTeX commands are defined in the Preamble block.


No Responses to “Customizing LyX: An Introduction to Modules”

Care to comment?