23

I am a long-term pdflatex user. Once in a while I want to give lualatex a try (e.g., to try the recent TikZ graph layout engine). A couple of good answers here at tex.se describe what to do in that case. For instance, the answer to How is a TeX document written when using LuaLaTeX? gives the following recipe (itself cited from Manuel Pé­gourié-Gon­nard’s Guide to LuaLaTeX):

  1. Don’t load inputenc; just encode your source in UTF-8.
  2. Don’t load fontenc or textcomp; load fontspec.
  3. babel works with LuaLaTeX but you can load polyglossia instead.
  4. Don’t use any package that changes the fonts; use fontspec commands instead.

I found all of that easy to follow advice, with the exception of point 4. The fontspec documentation is quite intimidating and gives no hint how to translate existing font setups to fontspec. For instance, last time I tried to switch I had the following font setup in my preamble:

\usepackage{lmodern}
\renewcommand{\sfdefault}{cmbr}
\usepackage{charter}
\usepackage[scaled=0.85]{beramono}

This is just an example, I typically pick my font setups from the LaTeX font catalogue, additionally load microtype, and that's it. Most font packages have reasonable setups with respect to all the fine details so the results are good enough for me.

However, with fontspec, I have the feeling I have to become a real font expert. In most cases I already failed to figure the font names to use, not speaking about all the other details.

So are there any translation tables for existing font packages?

Is there a technical reason that font packages not support lualatex out of the box, so one does not have to deal with intimidating fontspec commands?

Daniel
  • 37,517
  • You are confusing fontenc and fontspec in various places of your question. There are quite a number of font packages which also setup fonts for lualatex, e.g. libertine and other packages maintained by Bob Tennent. But as there exists so many fonts that you can't expect a package for everyone -- and when you get used to it you will see that calling the fonts with the fontspec commands is not so difficult. – Ulrike Fischer May 06 '15 at 15:31
  • Many fonts you find in the Font Catalogue are provided for pdflatex only ... fdfiles, tfm files etc. With LuaLaTeX, you can use open Type fonts. Some, some, font packages deal with the modern engines, check out the code for libertine.sty. – Johannes_B May 06 '15 at 15:32
  • @UlrikeFischer: Old habits... I think I have fixed all fontencs now. – Daniel May 06 '15 at 15:35
  • @Johannes_B: Sure. However, I am also sure that for most of them some OpenType version also exists, so it should be possible to describe a fontspec setup as well. – Daniel May 06 '15 at 17:05
  • I don't think it's the task of the fontspec manual to "translate" latex font commands any more than it would be the task of packages like fontenc and inputenc to "translate" fontspec's commands. I think more and more people start their latex lives directly with xelatex or lualatex, so it seems a bit anachronistic to me to include latex material into the fontspec manual. – Sverre May 13 '15 at 12:06

2 Answers2

17

The user guide of the fontspec package is indeed quite lengthy. However, I would not go as far as calling it intimidating. There's a huge and wonderful world out there related to OpenType and TrueType fonts, and it's not surprising (to me at least) that the manual that explains how to explore this world isn't brief.

In what follows, I will assume that your tex document is encoded according to the UTF8 standard. Aside: If your document uses only ASCII, it's automatically UTF8-compliant.

I have an educated guess as to why the user guide of the fontspec package doesn't explain how one might "translate" directives such as \usepackage{lmodern}, \renewcommand{\sfdefault}{cmbr}, \usepackage{charter}, or \usepackage[scaled=0.85]{beramono} to LuaLaTeX. The putative reason is quite simple: For the most part, no translation is necessary. If you're going to use font packages with LuaLaTeX that were designed originally for use under pdfLaTeX, just keep using them under LuaLaTeX and you'll be fine. (Do see below, though, for same caveats and exceptions.) If you do so, though, don't load the fontspec package as well.

For instance, the program

\documentclass{article}
\usepackage{lmodern}
\renewcommand{\sfdefault}{cmbr}
\begin{document}
The quick brown fox jumps over the lazy dog.

\sffamily
The quick brown fox jumps over the lazy dog.
\end{document}

produces the same output when run under either pdfLaTeX or LuaLaTeX:

enter image description here

Likewise, the program

\documentclass{article}
\usepackage{charter}
\usepackage[scaled=0.85]{beramono}
\begin{document}
The quick brown fox jumps over the lazy dog.

\sffamily
The quick brown fox jumps over the lazy dog.
\end{document}

produces the same output when compiled with either pdfLaTeX or LuaLaTeX:

enter image description here

The qualifier "for the most part" that was used above needs some explaining. As @UlrikeFischer points outs in a comment, an adjustment to the approach outlined above is necessary if the document contains "accented" characters which aren't included in the basic ASCII character set. (As noted above, I assume that the entire document is UTF-8 encoded.) If the document contains such characters, it is necessary to load the luainputenc package with the option utf8. With this adjustment made, the following code

\documentclass{article}
\usepackage[utf8]{luainputenc} % Not: "\usepackage[utf8]{inputenc}"
\usepackage{newtxtext}
\begin{document}
Grüße äöüÄÖÜ, \em Grüße äöüÄÖÜ
\end{document}

once again produces identical results when compiled under pdfLaTeX and LuaLaTeX:

enter image description here

Excerpting from the user guide of the luainputenc package:

From the user point of view, adapting an old document for LuaTEX is really easy: replacing inputenc by luainputenc in the preamble is enough. Note that luainputenc automatically loads inputenc if called with an old engine, so you will still be able to compile your documents with pdfTEX without changing them

That said, as @UlrikeFischer has pointed out in comments to this answer, some issues related to hyphenation may remain if you're using the luainputenc package. For instance, words with accented characters (like Grüße) may no longer be hyphenated, and LuaLaTeX's output may therefore not be exactly the same as if using pdflatex. Hence, in the long run it may nevertheless be a good idea to switch to fontspec in order to obtain optimal results.


When working with OpenType fonts, there are no packages -- with only a few exceptions (eg., the Libertine font family) -- that allow them to be used under pdf(La)TeX. To use OpenType fonts, one thus has to use either Xe(La)TeX or Lua(La)TeX -- typically in conjunction with the fontspec package. Since these fonts can't be used under pdfLaTeX anyway, a translation of instructions wouldn't be meaningful, right?

Mico
  • 506,678
  • 2
    No he will not be fine if he uses standard pdflatex packages with lualatex. Add Grüße to your example and you will see why. – Ulrike Fischer May 06 '15 at 16:35
  • Actually, I have tried that – but did not get UTF-8 characters to work, as mentioned by Ulrike. – Daniel May 06 '15 at 17:02
  • @UlrikeFischer - Thanks. I've added a paragraph to note that if the input contains "accented" characters that aren't in the ASCII set (but which are UTF8-encoded), the user will have to change the instruction \usepackage[utf8]{inputenc} to \usepackage[utf8]{luainputenc}. With this adjustment made, the program should compile identically under pdfLaTeX and LuaLaTeX. – Mico May 06 '15 at 17:05
  • @Daniel - what exactly did you try? Is the input utf8-encoded? Which version of LuaLaTeX do you use? – Mico May 06 '15 at 17:06
  • "There's a huge and wonderful world related to OpenType and TrueType fonts out there" Yes, I believe that and I am optimistic that one day I would be able to value it as well. However, my current setup is more like this: I have a lecture series with 500+ beamer slides with hand-optimized line breaks. I am totally happy with the current fonts and pdflatex, but for some cool package that requires lua I want to try lualatex. But, please, with as little change as possible to the fonts. – Daniel May 06 '15 at 17:15
  • 2
    This isn't perfect either: The hyphenation patterns don't expect the ß at this position and so "Grüße" will no hyphenate in a german document. Using standard font packages is possible as a workaround but should not be recommended. – Ulrike Fischer May 06 '15 at 17:16
  • @UlrikeFischer - Isn't the matter of suboptimal hyphenation of certain words an entirely separate topic? The incorrect (or missing...) hyphenation of "Grüße" -- when using babel with the option ngerman, along with luainputenc and, say, lmodern -- occurs under both pdfLaTeX and LuaLaTeX. Do observe that I am not advocating anywhere in my answer that one shouldn't use fontspec when using LuaLaTeX. All my answer says that if one's content with a font package designed for use under pdfLaTeX, one can continue to use it under LuaLaTeX, with the exception of the luainputenc matter. – Mico May 06 '15 at 17:34
  • @Daniel - Which fonts do you use in the pdfLaTeX version of your beamer-based lecture notes? – Mico May 06 '15 at 17:40
  • With pdflatex Grüße will hyphenate if you use T1-fencoding, with lualatex not. That's a side effect of the font setup, (O)T1-encoded fonts (or other standard encodings) and the utf8-orientated patterns of lualatex clash. In beamer slides this probably won't matter, but one should know that this side effect exists. – Ulrike Fischer May 06 '15 at 17:46
  • 1
    @UlrikeFischer - Ummm, \documentclass{article} \usepackage[textwidth=1mm]{geometry} \usepackage[T1]{fontenc} \usepackage[utf8]{luainputenc} \usepackage{lmodern} \begin{document} x Grüße hoffen \end{document} does not hyphenate "Grüße" (but does hyphenate "hoffen") under either pdfLaTeX or LuaLaTeX. Which font are you using to get a different outcome? (I currently use MacTeX2015 pre, which features version 0.80.0 of LuaTeX and version 3.14159265-2.6-1.40.16 of pdfTEX.) – Mico May 06 '15 at 17:57
  • You are not loading the hypenation patterns. \usepackage[ngerman]{babel} – Ulrike Fischer May 06 '15 at 18:07
  • Mico, you are the man, luainputenc does indeed the trick! I wasn't aware of the existence of this package before. (Possible minor hyphenation issues are less of a problem in beamer slides, where I typically try hard to avoid any kind of hyphenation.) – Daniel May 06 '15 at 18:43
  • @Daniel - I'm glad the switch to luainputenc is getting the job done for you. :-) – Mico May 06 '15 at 18:47
  • Mico, while there are not an issue for my particular use case, the answer would nevertheless profit from mentioning the hyphenation issues Ulrike brought up. Especially the quote from luainputenc can be somewhat misleading, as one would expect the same typographical output. – Daniel May 13 '15 at 09:27
  • @Daniel - Thanks. I've updated and expanded the text in several locations. Hope this meets your objective. – Mico May 13 '15 at 10:50
  • @Mico: Almost, I think some more details would be useful. I have edited them in; feel free to reject this edit if you don't like it. – Daniel May 13 '15 at 11:23
  • @Daniel - Thanks! I'm fine with your edits, and applied a few more touch-ups. – Mico May 13 '15 at 12:04
  • 1
    I've never cleaned it up, but I made small sample for LuaLaTeX with fontenc, unicode text and hyphenation: https://gist.github.com/michal-h21/aa57f226dc78f841dbde – michal.h21 May 13 '15 at 12:06
  • @michal.h21 - Thanks for this. Just to be sure I understand what the code in the link does: is it a way of fixing the residual hyphenation issues that Ulrike points out in her comments? Please advise. – Mico May 13 '15 at 12:08
  • @Mico yes, it fixes the hyphenation and enables the unicode input. it is just a proof of concept, as T1 font encoding is hardwired, it would need some support for other font encodings to be fully usable – michal.h21 May 13 '15 at 12:18
  • @michal.h21 - This is great! It sure works, for both Czech and German -- at least on words such as "Straße" and "Grüße". (Maybe Ulrike has a longer list of German problem words...) Do you have plans to share this code with the maintainer(s) of the babel package? – Mico May 13 '15 at 13:10
  • @Mico the problem is how to detect font encoding for a given font - maybe we could polish it in a new question? – michal.h21 May 13 '15 at 13:56
  • @michal.h21 - Would you like me to post such a question? :-) – Mico May 13 '15 at 13:58
  • @Mico it would be nice :) – michal.h21 May 13 '15 at 14:16
  • too late for today (I'm in Singapore...); will post a question tomorrow, if that's OK. – Mico May 13 '15 at 14:40
  • @michal.h21 - Sorry for the delay getting back to you. Upon studying your code some more, I realize I don't understand what all the issues may be that should be fixed. May I therefore ask you to ask that query about how to auto-detect the font encoding that's in use? – Mico May 14 '15 at 14:03
  • @Mico OK, I will investigate it and will try to make a question – michal.h21 May 14 '15 at 14:21
  • 1
    @Mico I've created Github project, I had some success, with some issues: https://github.com/michal-h21/luafontenc – michal.h21 May 28 '15 at 12:02
  • @michal.h21 @Mico Nearly three years later, would you still consider "luafontenc" useful? I would like to have newtxtttin my documents and I did not find any replacement for it. So, I'd like to use that package if it is still "OK". - Would you mind polishing it and submit it to CTAN? – koppor Mar 10 '18 at 01:08
  • 1
    @koppor it would definitely need some polishing, I don't even remember that I wrote it :/ I am quite busy lately, so I cannot promise anything. but I will look at it – michal.h21 Mar 10 '18 at 06:59
  • This worked back then, but today, (with an updated Texlive), the lmodern example warns about "Font shape TU/cmbr/m/n' undefined", thecharterexample warns about "Font shapeTU/bch/m/n' undefined". Should this be seen as bugs in some component, or is this answer no longer valid? – pst Nov 19 '19 at 10:37
6

The CMBright fonts are available in OpenType format as part of the cm-unicode project.

Granted that your file is UTF-8 encoded, you should get comparable results by doing

\documentclass{scrartcl}
\usepackage{ifluatex}

\ifluatex
  \usepackage{fontspec}
  \setmainfont{XCharter}
  \setsansfont{CMU Bright}[
    Scale=MatchUppercase
  ]
  \setmonofont{Ubuntu Mono}[ % or other monospaced font
    Scale=MatchUppercase
  ]
\else
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{charter}
  \usepackage{cmbright}
  \usepackage{beramono}
\fi

\usepackage{kantlipsum}

\begin{document}

\section{Section title}

\kant[1]

\texttt{Some text in monospaced font}

\section{Font list}

\begin{itemize}
\item Roman font: \expandafter\texttt\expandafter{\fontname\font}

\item {\sffamily Sans font: \expandafter\texttt\expandafter{\fontname\font}}

\item {\ttfamily TT font: \expandafter\texttt\expandafter{\fontname\font}}
\end{itemize}

\end{document}

There's no OpenType free version of the BeraMono fonts, as far as I know.

Output with pdflatex

enter image description here

Output with pdflatex after adding \usepackage{microtype}

enter image description here

Output with lualatex

enter image description here

egreg
  • 1,121,712