Go to page:

Search:   Help

Edit

LyX /

# XeTeX

<< | Page list | >>

How to use LyX with XeTeX

### What is XeTeX?

XeTeX (pronounced /ˈziːtɛx/) is a TeX typesetting engine using Unicode and supporting modern font technologies such as OpenType. Initially developed for Mac OS X, it is now available for all major platforms.

XeTeX natively supports Unicode while its input file is assumed to be in UTF-8 encoding. XeTeX can use all fonts of the operating system without additional configuring and access font features such as special ligatures and variable font weights.

XeTeX works well with both LaTeX and ConTeXt. Its LaTeX counterpart is invoked as XeLaTeX.

Homepage: http://scripts.sil.org/xetex

### Setting up LyX to work with Xetex

#### From LyX 2.0 onwards

You need to click Document→Settings...→Fonts→Use non-TeX fonts (via XeTeX/LuaTeX). Then you can immediately View→PDF (XeTeX).

Note: If Document→Settings...→Fonts→Use non-TeX fonts (via XeTeX/LuaTeX) is grayed out (you cannot activate it), make sure that the fontspec package is installed (and you need to reconfigure LyX after installing this package).

#### Up to LyX 1.6.x

This assumes you have Xetex installed. On my texlive installation, it was installed automatically. Try running 'xetex' or 'xelatex' to check if its there. If not, install it (depends on your LaTeX distribution).

First of all, you need to add a PDF (xetex) File Format and LyX->Xetex conversion:

Under Tools->Preferences->File handling->File formats, click "New" and

Format: PDF (xelatex)
[x] check "Document Format"
[x] check "Vector Graphic Format"
Short Name: pdf4
Extension: pdf
Viewer: xdg-open
(Windows users should use pdfview. Alternatively, use any viewer that worked for you under File Format PDF (pdflatex).)

Under "Converters" (just above the "File formats" you just clicked on), set

From format: LaTeX (pdflatex)
To format: PDF (xelatex)
Converter: xelatex i
Extra flag: latex
[x] check "Enabled"

### Using XeTex in a LyX document

Xetex is a program that is invoked instead of LaTeX, so if you set up your LyX document to use Xetex, it seems you cannot export to pdflatex anymore, just Xetex. Dont worry though, there wont be any problems (except the one just below).

Go to Document->Settings->LaTeX Preamble and add the following entries:

 \usepackage{fontspec}
\usepackage{xunicode}
\usepackage{xltxtra}


also, set the encoding in "Language" to Unicode (XeTeX) (utf8).

From now on, exporting to PDF (pdflatex) will show errors, but PDF (xelatex) will work.

### Using true type fonts (ttf) in XeTeX math mode (LyX 2.0 and later)

Go to

Document→Settings→Fonts

but do not click "Use non-TeX fonts (via XeTeX/LuaTeX)". Set all fonts (default family, LaTeX font encoding, roman, sans serif, typewriter) to default, and add the following to the preamble:

Document→Settings→LaTeX Preamble

 \usepackage{mathspec}%main package to select fonts in mathmode, includes the fontspec package
\setmainfont{...} set font used in non-math mode, i.e. font for main paragraphs, headings, foot/margin notes etc,
\setmathsfont(Digits,Latin)[Numbers={Lining,Proportional}]{...}%choose font for Latin and Arabic numerals, needed to match mainfont,
\setmathsfont(Greek){...}%choose a font with Greek characters, or leave this out for the default computer modern Greek letters,
\setmathrm{...}%choose roman (upright) font, needed for mathbf to appear in same font as set mathfont,
default is computer modern roman in upright ("\mathrm") and upright bold ("\mathbf"),


where in the curly brackets any true type font can be used, provided it can supply those characters (for Greek alphabet choose a font which includes Greek, etc). You can also declare the remaining font alphabets, or leave them out for the default computer modern;

 \setmathcal{...}%(caligraphic)
\setmathsf{...}%(sans serif)
\setmathbb{...}%(blackboard bold)
\setmathfrak{...}%(fraktur)


Beware that multiple fonts selected can cause the error message:

 LaTeX error: too many fonts used in version normal


while in equation mode. One simple solution is to use normal text mode "\textrm{...}" instead of "\mathrm{...}" for upright characters (it works out the same).

Also, one can insert ttf characters supplied from any ttf font (or it could match the mainfont) in an equation, for example Russian Cyrillic, Arabic, or extended Latin including "hbar" ħ, although these will be upright ("roman"). A math macro that makes them italic can be defined in the preamble;

 \def\anysymb[#1]{\fontspec{(font name here)...}\textit{#1}}


where in the curly brackets the font is inserted. Here the command is "\anysymb[...]" (you could define another name instead of "anysymb" of course), in the square brackets the ttf character is inserted, for example:

 \anysymb[ħЖ]


would generate ħЖ in the font chosen in the macro definition.

### Actually using foreign characters

You can just go ahead and e.g. type a Chinese character like 猫 into your document (this one means 'cat'). However, chances are the font you're using doesnt have that character, so Xetex will just display nothing. To fix that, you need to install a font that has Chinese characters (a list of such fonts and how they look can be found here). Suppose your font is called 'Sazanami Mincho'. Just add a TeX (ERT) block to the document (by clicking that TeX symbol) and write

 {\fontspec{Sazanami Mincho}


after that TeX thingy, you can just go ahead and write your Chinese characters. However, if you mix english, you'll see that it uses the english characters from Sazanami Mincho, which arent exactly beatiful. The solution is to add another TeX block after you're done writing chinese containg

 }


This closes the trailing { from before and reverts back to 'normal' text mode. Now you can write your english.

### Actually using foreign characters (the easy way, without TeX blocks)

If you follow the instructions below you can just go ahead and use CJK characters in your document. Only CJK characters will use a CJK font and everything else will remain untouched. Finally no ugly TeX blocks.

Download the xeCJK macro from CTAN:macros/xetex/latex/xecjk/, should be included in texlive 2009.

 \usepackage{xeCJK}
%\CJKlanguage{Japanese}
%'\CJKlanguage' has been removed from the lastest version.
\setCJKmainfont{Sazanami Mincho}


### Declaring Font Families

If you intermix e.g. english and chinese a lot, this might be useful to you. Instead of always explicitly declaring the font family, just declare it once as a family and use that family. This is handy in case you want to change that font later.

Declare it like this:

 \newfontfamily\Chinese{Sazanami Mincho}


And now use

 {\Chinese


 {\fontspec{Sazanami Mincho}


### Examples

Here is a LyX-document showing you how to use XeTeX's unicode features: xetex.lyx
and its PDF-output: xetex.pdf

Hints on using XeTeX with LyX 1.5.x are on the Mac.XeTeX page.

### Problems

• Broken Preview Button

As you cannot use LaTeX anymore, the handy preview button is broken. You need to View->PDF (xelatex) which is one more click than you're probably used to. A Workaround will be posted here soon.

I don't know how to get xelatex preview from a button, but you can make a key binding "Shortcut" to simplify life:

From the toolbar, navigate

     Tools->Editing->Shortcuts->Document and Window


You can select a bindings file from a pull-down menu at the top (I like xemacs).

Hit the "New" button at the bottom.

For "Function:" fill in buffer-update pdf4 ("pdf4" is the "Short Name" given to the "PDF (xelatex)" file format above)

For "Shortcut:" type the keystrokes that you wish to use. They are probably already in use, so the UI will tell you to removee the old use. After making sure that you don't mind sacrificing the old binding (e.g., I use PDF instead of XDVI, so I recycled a binding for buffer-update xdvi), select the old binding and the "Remove" button. The interface continues to display the old binding, lined out, and it appears to be easy to restore. After removing the old binding, do the new binding again.

The binding now works for me, but I had the impression that it failed on the first try. I might have goofed something else, or the binding might have taken effect on the next execution of LyX, or I might have neglected an "Apply" or "OK" button.

Another way is to change the definition of the preview button in

             DIR_NAME/Resources/ui/stdtoolbars.inc


where DIR_NAME is the directory LyX is installed. By changing

                Item "View PDF (dvipdfm)" "buffer-view pdf3"
Item "Update PDF (dvipdfm)" "buffer-update pdf3"


to

                Item "View PDF (xetex)" "buffer-view pdf4"
Item "Update PDF (xetex)" "buffer-update pdf4"


and restart Lyx, you can see the buttons becomes "View PDF (xetex)" and "Update PDF (xetex)". They work. In order to use icons, we need to prepare image files named as buffer-view_pdf4.png and buffer-update_pdf4.png in DIR_NAME/Resources/images. Maybe the easiest way is copying from existing image files for pdf3.

• Quotes

If the quote signs do not work the way you expect, look here.

• LyX display font

I can't figure out the general way to have LyX display the font that I have selected through xetex's \fontspec. I am using a version of the visible speech symbols, encoded in the "Private Use" section of Unicode. I can probably discover the right approach by inspecting the LyX treatment of IPA (International Phonetic Alphabet) symbols. For now, I see either blanks, or Asian symbols selected from a different font. The same output appears to apply to both the LyX buffer display, and the selection tool to insert a Symbol. I expect that there will be many uses of specialized fonts for symbols, so this problem is probably worth a well-integrated solution.

• Input of strange Unicode symbols

Similarly to the display font problem above, I have a problem finding a convenient way to input my unusual (Visible Speech) symbols. A solution to the display problem will probably make them selectable through the "Insert" menu on the tool bar. I will address part of the problem by cooking up some LaTeX macros referring systematically to VS symbols and natural combinations of them. So far, I'm clueless how to provide efficient input in the LyX buffer.

### References

Categories: XeTeX Unicode