2

I have the following code:

\documentclass{book}
\usepackage{fontspec}
\begin{document}
    \part{title}
    \chapter{\fontspec{Latin Modern Roman} chapter title}
    \part{title}
    \chapter{title}
\end{document}

I use package fontspec to switch font from one to another. As in this code, I want to typeset the chapter title using font Latin Modern Roman. Actually, it works fine with report and memoir class. But it produce an error in book class. How can I fix it?

  • I suspect a missing \protect just before \fontspec, i.e. \protect\fontspec, but I can't test right now, since I get errors on the missing font – user31729 Jan 6 '15 at 17:57
  • Do you want to have all chapter heading to look different/have another font? There are packages giving you an interface to define such stuff globally. – Johannes_B Jan 6 '15 at 17:59
  • @ChristianHupfer I tried to add it, but still not work! – Say OL Jan 6 '15 at 18:00
  • @Johannes_B I want another font! – Say OL Jan 6 '15 at 18:00
  • 3
    you can make it work but it is always wrong to put formatting instructions in a section heading. It is supposed to just have text which can be set in several contexts, the heading itself, the page head, the table of contents etc. The argument should just have text and the fonts used in each context should be set by the class – David Carlisle Jan 6 '15 at 19:15
4

Using the scrbook-class of KOMA-Script:

Serif-Font in all headings (chapter, section, etc.)

\documentclass{scrbook}

\usepackage{fontspec}
\addtokomafont{disposition}{\rmfamily}

\begin{document}

\chapter{Hello World}
Test
\end{document}

Or explicitly set the font with fontspec:

\documentclass{scrbook}

\usepackage{fontspec}
\newfontfamily{\headingsfont}{Latin Modern Roman}
\addtokomafont{disposition}{\headingsfont}

\begin{document}

\chapter{Hello World}
Test
\end{document}

The result is identical if the standard serif font is Latin Modern Roman, you can set this with \setromanfont{<fontname>}:

result

If you just want to change certain structure elements use e.g. \addtokomafont{chapter}{} or \addtokomafont{section}{} etc:

\documentclass[open=any]{scrbook}

\usepackage{fontspec}
\setmainfont{Tex Gyre Pagella}
\newfontfamily{\partnumfont}{Comic Sans MS}
\newfontfamily{\partfont}{Latin Modern Roman}
\newfontfamily{\chapterfont}{Tex Gyre Chorus}
\newfontfamily{\sectionfont}{Tex Gyre Heros}    
\usepackage{xcolor}

\addtokomafont{partnumber}{\color{red}\partnumfont}
\addtokomafont{part}{\partfont}
\addtokomafont{chapter}{\chapterfont}
\addtokomafont{section}{\sectionfont}}

\begin{document}
\part{Test}
\section{my first Koma-Script book}
\end{document}

enter image description here

Using the standard book class and package titlesec:

You probably need to adjust the settings.

\documentclass{book}
\usepackage{fontspec}
\setmainfont{Tex Gyre Pagella}
\newfontfamily{\partfont}{Tex Gyre Heros}
\newfontfamily{\chapterfont}{Tex Gyre Chorus}
\newfontfamily{\sectionfont}{Tex Gyre Heros}        
\usepackage{xcolor}

\usepackage{titlesec}
\titleformat{\part}[display]{\partfont\Huge\bfseries\centering}{\partname\ \thepart}{20pt}{}
\titleformat{\chapter}{\chapterfont\huge\bfseries}{\chaptername\ \thechapter}{20pt}{}
\titleformat{\section}{\sectionfont\large\bfseries}{\thesection}{20pt}{}

\begin{document}
\part{Test}
\chapter{Hello Worl.}
\section{standard book}
Hello World!
\end{document}
  • 1
    The preferred way to choose a font using fontspec is to use \newfontfamily etc. and not to use the \fontspec command directly. See §5 of the fontspec manual. – Alan Munn Jan 6 '15 at 19:51
  • ok, i changed it :) – MaxNoe Jan 6 '15 at 19:56
  • I see! We can change it for memoir and scrbook. So this means that we don have such options to do in book class, right? – Say OL Jan 6 '15 at 20:15
  • 1
    simple options? No. Packages that implement solutions? Yes. Documentclasses that implement simple options? Yes. – MaxNoe Jan 6 '15 at 20:21
  • @MaxNoe What are the packages? and what are the document classes beside memoir and scrbook? – Say OL Jan 7 '15 at 3:05
2

I used the xpatch package to hook into the command for typesetting the chapter head. This will change the font for all chapters.

\documentclass{book}
\usepackage{fontspec,xpatch}
\makeatletter
\xpatchcmd{\@makechapterhead}{\normalfont}{\fontspec{Latin Modern Roman}}{}{}
\makeatother
\begin{document}
    \part{title}
    \chapter{chapter title}
    \part{title}
    \chapter{title}
\end{document}

We can use this solution to easily adjust the font per chapter

\documentclass{book}
\usepackage{fontspec,xpatch}
\newcommand*\setchapterfont[1]{\xdef\chapterfont{#1}}
\setchapterfont{Latin Modern Roman}
\makeatletter
\xpatchcmd{\@makechapterhead}{\normalfont}{\fontspec\chapterfont}{}{}
\makeatother
\begin{document}
\setchapterfont{Latin Modern Sans}
\chapter{Test 1}
\setchapterfont{TeX Gyre Heros}
\chapter{Test 2}
\setchapterfont{TeX Gyre Schola}
\chapter{Test 3}
\end{document}

enter image description here

enter image description here

enter image description here

With memoir

\documentclass{memoir}
\usepackage{fontspec,blindtext}
\newcommand*\setchapterfont[2][]{
  \renewcommand*\chapnamefont{\huge\bfseries\fontspec[#1]{#2}}
  \renewcommand*\chapnumfont{\huge\bfseries\fontspec[#1]{#2}}
  \renewcommand*\chaptitlefont{\Huge\bfseries\fontspec[#1]{#2}}
}
\newcommand*\setsectionfont[2][]{
  \setsecheadstyle{\Large\bfseries\fontspec[#1]{#2}}
}
\newcommand*\setsubsectionfont[2][]{
  \setsubsecheadstyle{\large\bfseries\fontspec[#1]{#2}}
}
\newcommand*\setsubsubsectionfont[2][]{
  \setsubsubsecheadstyle{\normalsize\bfseries\fontspec[#1]{#2}}
}
\begin{document}
\setchapterfont{Latin Modern Sans}
\setsectionfont[Numbers=OldStyle]{TeX Gyre Cursor}
\setsubsectionfont[Color=009911]{Linux Libertine O}
\setsubsubsectionfont{Raleway}
\blinddocument
\end{document}
  • It works. Anyway, what are the command for changing the part and section? – Say OL Jan 6 '15 at 18:22
  • @OLSAY If you want to change all of your headings this isn't a suitable approach. You should look into the packages titlesec or the scrbook or the memoir documentclass. I will provide an example for memoir. – Henri Menke Jan 6 '15 at 18:27
  • What is the solution? If we have a better solution by using memoir class, I think I should change to use it. – Say OL Jan 6 '15 at 18:41
  • @OLSAY See the updated answer. You can now also specify extra font feature in square brackets. – Henri Menke Jan 6 '15 at 18:51
  • 2
    The preferred way to choose a font using fontspec is to use \newfontfamily etc. and not to use the \fontspec command directly. See §5 of the fontspec manual. – Alan Munn Jan 6 '15 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.