Xelatex Vs. Lualatex: Key Differences In Preamble Setup

Choosing Between XeLaTeX and LuaLaTeX

When preparing to create a LaTeX document, one of the first decisions is which LaTeX engine to use for compiling the document. For professional document preparation and advanced typesetting, the two main choices are XeLaTeX and LuaLaTeX.

Both XeLaTeX and LuaLaTeX extend standard LaTeX with improved Unicode and font handling support. However, there are some key differences in capabilities and preamble setup between these two engines that are important to understand before deciding which one to use for a particular project.

The Core Difference Between XeLaTeX and LuaLaTeX

At a high level, the core differences between XeLaTeX and LuaLaTeX can be summarized as follows:

  • XeLaTeX extends TeX with native Unicode support and modern font technologies like OpenType, allowing direct use of system fonts.
  • LuaLaTeX builds on XeLaTeX by adding an embedded Lua scripting engine, enabling advanced programmatic control and customization of typesetting via Lua callback functions.

In practical terms, XeLaTeX offers an easier way to use system fonts, while LuaLaTeX provides more customizability for power users via Lua scripting. The additional programming capabilities unlocked by Lua give LuaLaTeX more flexibility, but also make its preamble setup more complex compared to XeLaTeX.

Setting Up Fonts in the Document Preamble

One of the main reasons for using XeLaTeX or LuaLaTeX is to take advantage of their expanded font capabilities compared to standard LaTeX. However, the required preamble commands for font configuration differ noticeably between these two engines.

XeLaTeX Allows Calling System Fonts Directly

A major advantage of XeLaTeX is the ability to use system fonts by calling them directly in the document preamble. This avoids having to generate special font packages and simplifies the font selection process, especially for documents that use a wide variety of fonts.

For example, to use the Arial font in XeLaTeX, you simply need to load the fontspec package and then call \setmainfont {Arial} in the preamble. This enables simple access to thousands of system fonts without complicated setup.

LuaLaTeX Offers Full Control Over Font Handling via Lua

In LuaLaTeX, direct access to system fonts works slightly differently due to its Lua scripting engine backend. Here, font loading and handling can be customized via special Lua callback functions defined in the preamble.

For instance, you would need to write a short Lua function to load Arial and set it as the main document font instead of the \setmainfont command. This requires more preamble code compared to XeLaTeX but offers complete control over all aspects of font selection, manipulation, and rendering in LuaLaTeX.

Passing Arguments to the LaTeX Engine

LaTeX engines accept certain command line arguments that customize the compilation process, such as output format and optimization settings. The supported arguments differ between XeLaTeX and LuaLaTeX due to their divergent architectures.

XeLaTeX Requires fontspec for Font Configuration

As XeLaTeX relies on the fontspec package for font handling, the fontspec argument must be passed to XeLaTeX during compilation for fonts to work properly:

xelatex -interaction=nonstopmode -synctex=1 -fontspec document.tex

This enables fontspec's font configuration commands defined in the document preamble when compiling. Without fontspec enabled, directives like \setmainfont would fail silently and default fonts would be used instead.

LuaLaTeX Loads fontspec Automatically for Convenience

In contrast, LuaLaTeX loads fontspec automatically in the background, so the fontspec argument does not need to be passed explicitly:

lualatex -interaction=nonstopmode -synctex=1 document.tex

This saves having to enable fontspec manually as with XeLaTeX. However, font loading and handling is still customizable using Lua callback functions as noted in the previous section.

Microtype Support for Improved Typography

For professional publishing quality typesetting, both XeLaTeX and LuaLaTeX support the microtype package for typographic enhancements like character spacing, margin kerning, and font expansion.

However proper configuration of microtype differs between engines due to technical factors around font handling and rendering.

Enabling Microtype in XeLaTeX May Cause Issues with Some Fonts

In XeLaTeX, directly loading system fonts via fontspec can sometimes conflict with microtype adjustments, causing compilation issues or poor spacing/kerning in output.

Fixing these microtype problems requires additional troubleshooting and customization based on font characteristics to balance optimal use of microtype while avoiding font-related errors.

LuaLaTeX Fully Supports Microtype Without Problems

Thanks to LuaLaTeX's robust font handling backend powered by Lua, the microtype package integrates seamlessly without font conflicts or compilation failures.

This means you can enable all microtype functionality out-of-the-box for improved typography without worrying about font-related issues during compilation.

Optimizing Compile Time

For larger, complex documents, compile times can become lengthy for LaTeX engines. There are also noticeable differences in compile speed between XeLaTeX and LuaLaTeX.

LuaLaTeX Can Be Slower to Compile Than XeLaTeX

The additional Lua script processing and font handling flexibility of LuaLaTeX often leads to longer compile times compared to XeLaTeX, especially for documents with heavy customization via Lua.

Certain LuaLaTeX preamble tweaks like aggressive font caching and disabling unnecessary Lua packages can help. But in general, a speed vs capability tradeoff exists between the two engines.

Caching Loaded Fonts in Lua Can Help Reduce LuaLaTeX Compile Times

One key tactic for improving LuaLaTeX performance is utilizing Lua's font cache to minimize duplicate font loading across compiler runs.

This stores font data in memory between compiles so that LuaLaTeX doesn't reload the same fonts constantly. Properly written, such font caching routines can cut LuaLaTeX compile times significantly to be more on par with XeLaTeX.

When to Choose XeLaTeX vs. LuaLaTeX

Based on their technical differences highlighted in previous sections, here are some general guidelines on when to use XeLaTeX vs. LuaLaTeX:

For Multilingual Documents, XeLaTeX Offers Simpler Setup

If you need to typeset content containing a wide variety of languages and writing scripts, XeLaTeX's robust Unicode support through fontspec makes it easier to correctly render these without special handling.

For Advanced Programmatic Typesetting, LuaLaTeX is More Customizable

For precision publishing projects where maximum control over document elements like fonts, spacing, and layout is required, LuaLaTeX is preferable due to the fine-grained customizability provided by Lua scripting.

Leave a Reply

Your email address will not be published. Required fields are marked *