## writing an article with XeLaTeX : part 1

Writing an article with LaTeX and compiling it with pdflatex might be a good solution if you don’t expect to use japanese, chinese, arabic characters or anything else which is not included in the roman alphabet. But if you plan, for example, to write in japanese, then pdflatex is not the better way. Moreover, the documentation on the web about CJK, LaTeX and japanese is generally outdated. See for example the japanese documentation for LaTeX in the japanese wiki of Ubuntu. In the outlook section of LaTeX packages, it is even recommended to install pTeX, based on TeTeX.

Nowadays, we don’t have to use LaTeX anymore, we can use XeLaTeX. Compiling a *.tex file with XeLaTeX can be done with TeXLive 2009 or TeXLive 2010, so you don’t have to update your TeX distribution if you use Ubuntu 10.04 or any other operating system with a recent TeX distribution. I have made a template to be compiled with XeLaTeX, based on classicthesis.sty, mathspec.tex and many other informations I found on the web. Here is a brief description of the template I have pushed to my repository at Github :

\documentclass[
paper=a4,
fontsize=10pt,
%DIV=calc,
%footsepline, %to get a line separating footnote from page number.
%titlepage=true, %if you want an entire page for you title, name and date.
version=last,
%twoside=true, %if you want to print on both side (recto-verso) of the %page.
footinclude=true,
mpinclude=true,
%BCOR=0mm,
fleqn
]{scrartcl}

The above code show you scrartcl with some options, depended of what you want, you can comment, uncomment or add others. If you need documentation about the class scrartcl, and if you use Ubuntu with gnome, type alt+F2 and in the prompt enter texdoc scrguien. If you don’t use Ubuntu, and you don’t know where are the files related to Koma-script, you can download it at ctan.org

.

\usepackage{xltxtra} %include fontspec, xunicode, etc. For more %details, texdoc xltxtra
%\usepackage{mathspec}
%\usepackage{polyglossia} %xetex version of the babel %package. Since it is incomplete, %french users have to use babel. For %other users, replace french by your language
%\setmainlanguage{french}
%\setmainlanguage[variant=british]{english} %for english users. For
%more documentation, texdoc polyglossia.
%\usepackage{xspace}
\usepackage[frenchb
%arabic
%english
]{babel}

The above are the package xltxtra which load other packages to be used with xelatex. Also, since I need the french localization, I load frenchb with babel, but if you are english, german, etc, you just have to comment frenchb and uncomment or add other one. More details about it with texdoc babel, or at ctan. Notice that you could use polyglossia, the replacement of babel made for XeTeX. Unfortunately, the french option for polyglossia is not as good as the one for babel, that’s why I use babel with XeLaTeX. But if you are not french, you should try polyglossia.

%**********************************************************
%Comment or uncomment to select one of the following fonts
%**********************************************************
%\setmainfont{Charis SIL}
%\setmainfont{Linux Libertine O}
%\setmainfont{Gentium Book Basic}
%\usepackage{fourier}
%\setmainfont{Utopia-Regular}
% \usepackage[scaled=0.85]{berasans}
% \usepackage[scaled=0.85]{beramono}
%\usepackage[sc]{mathpazo}
\setmainfont[Numbers=OldStyle,Ligatures=Historic]{TeX Gyre Pagella}

Here are some examples of fonts commented. TeX Gyre Pagella is uncommented since it is the font I use by default for my articles.

%***********************************************************
%For japanese input \newfontfamily\ja[Scale=0.8]{IPA明朝} %If you wish to write in
%japanese, then use the command
%\jap. Here is an example : “Hello, here is a test : {\ja ここはテストしてま〜〜〜す！}.” Try to compile it. %***********************************************************
%If you want to write in arabic from the right to the left load the %following package :
%\usepackage{bidi} %then, use the command \setRL to right from the right to the left. For more details, texdoc bidi.

Here are the settings for japanese, and an example. Also, notice that you can write your texts from the right to the left with the package bidi.

%Other useful packages for a basic article
\usepackage{url} %for url
\usepackage{longtable} %for long tables
\usepackage{amsmath} %for maths
\usepackage{graphicx} %to include graphics
\usepackage{wrapfig} %to get nice wrap of figures
\usepackage{eukdate} %to redefine the default layout of the date format
% ********************************************************************
% Hyperreferences setup
%*******************************************************
\usepackage{hyperref}
% uncomment the following line if you want to have black links (e.g., for printing)
% plainpages=false, bookmarksnumbered, bookmarksopen=true, bookmarksopenlevel=1,
% hypertexnames=true, pdfhighlight=/O,
%hyperfootnotes=true,%nesting=true,
%pagecolor=Black,% pdftitle={Article},%the title pdfauthor={Alexandre Krispin},%your name pdfsubject={},% pdfkeywords={},% pdfcreator={XeLaTeX},% pdfproducer={A happy XeLaTeX user}% }

Here is nothing special, just some packages that can be useful, and the hyperref setup.

%**********************************************************
%Appearance, etc
%*********************************************************
\usepackage{xcolor} %use color with the command \textcolor{spot}{some text to

%redefining the section command in order to get previously defined
%color for each section title.
% \makeatletter
% \renewcommand\section{\@startsection {section}{1}{\z@}%
% {-3.5ex \@plus -1ex \@minus -.2ex}%
% {2.3ex \@plus.2ex}%
% {\color{spot}\normalfont\Large\bfseries}}
% \makeatother

\areaset[5mm]{312pt}{761pt} % 686 (factor 2.2) + 33 head + 42 head \the\footskip
\setlength{\marginparwidth}{7em}%
\setlength{\marginparsep}{2em}%

\usepackage[automark]{scrpage2}
%chp. 4

Here is the setup for the margins, etc. You will notice that I tried to set a color for the sections, but haven’t been able to get it successufly working. And since I don’t really care about it, I have given up.

%**************************************************************************************
% for the following, see mathspec.tex (for me /usr/local/texlive/2010/texmf-dist/xelatex/mathspec/mathspec.tex
% for ubuntu users who have install texlive with the package manager, replace /usr/local/texlive/2010 by /usr/share/texmf-texlive (but I’m not sure, you have to check your directory /usr/share)
\definecolor{spot}{rgb}{0.6,0,0}
\definecolor{boxframe}{rgb}{0.6,0,0}
\definecolor{boxfill}{rgb}{1,.95,.95}

\makeatletter
\setlogokern{eT}{-0.074em}
\setlogokern{Xe}{-0.063em}
\setlogokern{eL}{-0.068em}
\setlogokern{La}{-0.305em}
\setlogokern{aT}{-0.07313em}
\setlogokern{X2}{0.101em}
\setlogodrop{0.131em}
\setLaTeXa{%
\ifdim\fontdimen\@ne\font=\z@\else
\fi
\if b\expandafter\@car\f@series\@nil
\check@mathfonts\fontsize\sf@size\z@
\math@fontsfalse\selectfont A%
\else
\scshape a%
\fi}
\setLaTeXee{\mbox{$\epsilon$}}
\seteverylogo{%
\if b\expandafter\@car\f@series\@nil%
\setlogokern{Te}{-0.075em}%
\setlogokern{eX}{-0.068em}%
\else
\setlogokern{Te}{-0.084em}%
\setlogokern{eX}{-0.063em}%
\fi}
\newcommand\tablerowa[3]{$\csname #1#3\endcsname$ & \cmd{#1#3} & $\csname #2#3\endcsname$ & \MakeLowercase{\cmd{#2#3}}}
\newcommand\tablerowb[1]{$\csname #1\endcsname$ & $\csname var#1\endcsname$ & \cmd{var#1}}
\def\topbottomrule{\noalign{\ifnum0=}\fi
\@aboverulesep=\aboverulesep
\global\@belowrulesep=\belowrulesep
\global\@thisruleclass=\@ne
\@ifnextchar[{\@BTrule}{\@BTrule[\heavyrulewidth]}}
\renewcommand{\topfraction}{.75}
\renewcommand{\bottomfraction}{.75}
\renewcommand{\textfraction}{.25}
\renewcommand{\floatpagefraction}{.75}
\setcounter{topnumber}{9}
\setcounter{bottomnumber}{9}
\setcounter{totalnumber}{20}
\setcounter{dbltopnumber}{9}
\def\fps@figure{htb!}
\newlength\captionwidth
\newlength\mincaptionwidth
\setlength\mincaptionwidth{50mm}
\long\def\collecttable#1\caption#2{%
\toks@{#1}%
\sbox\z@{\ignorespaces\the\toks@\unskip}%
\setlength\captionwidth{\wd\z@}%
\ifdim\captionwidth \captionwidth
\centerline{\parbox[t]{\captionwidth}{\formatcaption#1:#2\@nil}}%
\else
\global\@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}
{\end{description}\end{minipage}}
\makeatother

The above code show you the definition of the color “spot” and other settings. Since it is just a copy of settings found in mathspec.tex, you can look at your xelatex directory of your TeX distribution. For me it is at /usr/local/texlive/2010/texmf-dist/doc/xelatex/mathspec/mathspec.tex. If you don’t find where is your directory, take a look at ctan.

\usepackage{placeins}
\usepackage{titlesec}
\usepackage{booktabs} % for better rules in tables
\usepackage{textcase} % for \MakeTextUppercase
% sections \FloatBarrier
\titleformat{\section} {\relax}{\textsc{\MakeTextLowercase{\thesection}}}{1em}{\large\scshape\lowercase}
% subsections
\titleformat{\subsection} {\relax}{\textsc{\MakeTextLowercase{\thesubsection}}}{1em}{\normalsize\rmfamily\mdseries\itshape\lowercase}

% subsubsections
\titleformat{\subsubsection} {\relax}{\textsc{\MakeTextLowercase{\thesubsubsection}}}{1em}{\normalsize\rmfamily\mdseries\itshape\lowercase}

% paragraphs
\titleformat{\paragraph}[runin]
{\normalfont\normalsize}{\theparagraph}{0pt}{\scshape}

% descriptionlabels
\renewcommand{\descriptionlabel}[1]{\hspace*{\labelsep}\scshape{#1}}

% spacedlowsmallcaps textit textsc
% spacing
{\relax}%
{\titlespacing*{\chapter}{0pt}{1\baselineskip}{1.2\baselineskip}}
\titlespacing*{\section}{0pt}{1.25\baselineskip}{1\baselineskip}
\titlespacing*{\subsection}{0pt}{1.25\baselineskip}{1\baselineskip}
\titlespacing*{\paragraph}{0pt}{1\baselineskip}{1\baselineskip}

Here is some code I have taken from the classicthesis package. This code modify the layout of sections, subsections, subsubsections, paragraphs and of text in \items[]. If you want to look at the original file, classicthesis.sty, you can find it in directory */texmf-dist/tex/latex/classicthesis/, where * could be /usr/share, or /usr/local/texlive/2010, depending on the installation way you have chosen to install TeXlive. Otherwise, take a look at ctan.org

.

p, li { white-space: pre-wrap; }

%*********************************************************

%Table of content

%*******************************************************

\setcounter{tocdepth}{2}

\makeatletter

\def\@seccntformat#1{\protect\makebox[0pt][r]{\csname the#1\endcsname\hspace{\marglistsep}}}

\makeatother

%*************************************************

%if you want to get dot between the word and the page number in the

% \makeatletter

% \renewcommand\l@section[2]{%

% \ifnum \c@tocdepth >\z@

% \setlength\@tempdima{1.5em}%

% \begingroup

% \parindent \z@ \rightskip \@pnumwidth

% \parfillskip -\@pnumwidth

% \leavevmode {\bfseries

% \hskip -\leftskip

% #1}\nobreak\

% \leaders\hbox{$\m@th\mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}

% \hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par

% \endgroup

% \fi}

% \makeatother

%**********************************************************

I have taken the code for the table of content from mathspec.tex, so once again if you like it look at this file in the directory previously indicated. I have added the package multicol in order to get the same layout than the one you can see in mathspec.pdf. Also, though I don’t like it, you can uncomment the lines between makealetter and makeatother if you want to get dots between the name of your titles and the page number.

\begin{document}
%\frenchspacing

\begin{titlepage}

\title{
\rmfamily
\mdseries
\lowercase{
\scshape{
\textcolor{spot}{
}}}}

\subtitle{
\rmfamily
\mdseries
\itshape
\normalsize{
}}
\author{
%Enter your name and first name
}

\end{titlepage}

\maketitle

%\addtocontents{toc}{\protect\begin{multicols}{2}} %if you want to get a table of
% contents with 2 columns. If you prefer one column, comment this line and
% the one just before \end{document}
\tableofcontents

%*******************************************************************
%
%
%******************************************************************

\end{document}

The settings for the title is similar to those of mathspec.tex, but since in mathspec.tex a different document class is used, I have changed some settings to make it work with the koma-script class, scrartcl.

Lastly, if you want to use the color spot previously defined, write \textcolor{spot}{the text you want to colorize} and you will get it. Here are 2 screenshots showing how this template looks with an example of short article :

And the other picture :

You can find this example at the same directory of my repository, here.

1. The following is a comment by drebon posted in my previous blog.

Hello,

Great description of preamble, it is nice and useful.

I recently tried to switch from latex with [utf8]{inputenc} to xelatex.

I do have a problem : when latex was able to directly deal with characters like “œ”, xelatex seems to be unable to do so. In order to produce a ligaturated oe I have to input \oe{} which is a little bit painfull.

Have you ever been confrontated to such a problem and have you found a solution ?

Thank you

I can’t answer precisely, so here are some questions :
– Did you try with the template I have pushed on my repository ? In this case, it might work.
– If it didn’t work with the template I have pushed at Github, then did you changed the main fonts ? If so, what is your main font ?
– Finally, what is your operating system ? If it is Windows, do you have Miktex, if it is GNU/Linux, do you have installed TeXlive 2009 ? Or 2010 ?

In any case, it worked for me with Ubuntu 10.04, TeXlive 2010, and TeX Gyre Pagella as the main fonts, defined as follows in the preamble :
\setmainfont[Numbers=OldStyle,Ligatures=Historic]{TeX Gyre Pagella}`

Cheers

• Drebon
• September 27th, 2010

Well, I didn’t tried with your template. Actually I am converting a style file I hve done for latex to xelatex because I use preamble variable definition in utf-8 and latex does not handle this (And I do not want to have two encodings in the same document).

I tried to define the font with \setmainfont[Numbers=OldStyle,Ligatures=Historic]{TeX Gyre Pagella}, but mktexmf complains about missing files.

I am under Ubuntu 10.04, I used the ubuntu distributed texlive, but now I have switched to normal texlive 2010 full install. Is there something I missed ?

Thank you

2. I use TeXlive 2010 and have no problem with it. Since I have switched to XeLaTeX from pdfLaTeX recently, I may also have a lack of experience. For this reason, maybe your problem is due to something I don’t have experimented yet.

But in any case, please, provide me the *.tex file or an abbreviated version of it if it is too long, and your *.log file. I will try to compile it by myself and see what is going wrong.

Cheers

• Drebon
• September 29th, 2010

Hello,

I have no problem with sending you the problematic document, but I doubt you will have the courage to look at it.

Basicly, I had written a sty file that suited my needs for latex documents. Recently I tried to switch to utf8 (for compatibilty reason, I was still using latin1 encoding with tex).

My way of defining some variables made latex do wrong things with utf8 (latex seems to be unable to deal with utf8 everywhere in the document) So I tried to move to xelatex.

I was first happlily suprised : my doc compiled without significant problems… Expcept that œ chararcter was not understood by xelatex…

After some resaerch I discovered that I had to use xltxtra and fontspec packages. There the œ works, but fonts are messed up…

helvet package is incompatible with xetex, so I swith to a \setmainfont specification…

After many texlive packages updates, it seems to allmost work expect that :

some formating command are not taken into account ( e.g.

\color{jaunefonc}\huge \bf \sf{Un petit texte}}

Gives something with the right size and color but not in bold …

I get also many

“! LaTeX Error: Command \nobreakspace unavailable in encoding EU1.” errors…

Well, so if you are willing to take a look at my files, I will happily supply them to you, but I’m afraid you probably won’t want to dive into my sty package…

Best ragards

3. No, that’s ok, don’t worry. But thank you !

My email address is : k.m.alexandre@gmail.com
But I will post the answer here.

For the color I will see when you will have sent me your files (sty, tex and log), maybe you should look at xcolor : texdox xcolor

By the way, you don’t need to load fontspec when you have already loaded xltxtra (for more details, see the related documentation : texdoc xltxtra).