38

I am trying to emulate MS Word 2010 functionality with LaTeX since I am fed up with word.

I am using TexShop, and I have been able to set the main font to Calibri through the use of XeTeX.

How do I set a different font/color/size for every type of section (section,subsection,subsubsection,...), without being limited to the standard LaTeX fonts? It would be nice to be able to choose any font from my system like XeTeX allows me to.

I have tried the package sectsty, but I was not able to figure out how to use Calibri with it.

migrated from stackoverflow.com Feb 3 '11 at 3:31

This question came from our site for professional and enthusiast programmers.

  • I think you have to interchange Cambria and Calibri while setting sans and main fonts – user38767 Oct 22 '13 at 22:26
51

The best way to have total control over the sectioning is the titlesec package. Here's a quick version of the standard MSWord sectioning for the first three levels. To control colours, you need the xcolor package.

If you are using different fonts for different section levels (not recommended) you should use fontspec's \newfontfamily command to define the font first, then use that in the redefinition of the title format, as in the subsubsection example.

This document can be compiled with either XeLaTeX or LuaLaTeX.

% Compile with XeLaTeX or LuaLaTeX
\documentclass[12pt]{article}
\usepackage[vmargin=1in,hmargin=1.25in]{geometry}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{titlesec}
\defaultfontfeatures{Ligatures=TeX}
% Set sans serif font to Calibri
\setsansfont{Calibri}
% Set serifed font to Cambria
\setmainfont{Cambria}
% Define light and dark Microsoft blue colours
\definecolor{MSBlue}{rgb}{.204,.353,.541}
\definecolor{MSLightBlue}{rgb}{.31,.506,.741}
% Define a new fontfamily for the subsubsection font
% Don't use \fontspec directly to change the font
\newfontfamily\subsubsectionfont[Color=MSLightBlue]{Times New Roman}
% Set formats for each heading level
\titleformat*{\section}{\Large\bfseries\sffamily\color{MSBlue}}
\titleformat*{\subsection}{\large\bfseries\sffamily\color{MSLightBlue}}
\titleformat*{\subsubsection}{\itshape\subsubsectionfont}

\begin{document}
\section{A section}
This is some text.
\subsection{A subsection}
\subsubsection{A subsubsection}
\end{document}

output of code

  • Thanks for the great reply! I have a few quick questions: - what does the * in \titleformat* represent? - I see how you set the color of the titles using \titleformat, but how do I change the font,size, and other settings for each title type individually. Eg. section in Bold Calibri MSBlue, and subsection in Italics, Times New Roman, and MSLightBlue. Thanks! – klyngbaek Feb 3 '11 at 18:24
  • @zucchini The * is just the titlesec syntax for that command. It roughly means that it's the quick version of the command (the unstarred version is more extensive.) All of this should be clear from the titlesec documentation. And my example gave you two different formats for section and subsection so I'm not sure I understand the question. I've added a bit to the example about font selection in XeLaTeX. – Alan Munn Feb 3 '11 at 18:45
  • Could you add a screenshot of the output? – doncherry Mar 13 '12 at 20:47
  • @Alan Munn The titlesec documentation does not seem clear to me. Could you cite where the manual discusses starred versions of \titleformat? I can only seem to find "4.2 Starred Versions" (updated June 14, 2000), which mentions that starred versions of sectioning commands are strongly discouraged (it seems that \section*{} is meant here) – Jonathan Komar Nov 7 '14 at 11:28
1

Using a KOMA-Script class like scrartcl, which is an enhanced drop-in replacement for the standard class article, you can use the build in font element interface. There are two commands \setkomafont{element}{font commands} and \addtokomafont{element}{font commands} to either set or extend font settings for an element. An element can be, i.e., a section level like, part, section, subsection, subsubsection, paragraph or subparagraph.

With this you need only the wanted font and either a font support package or lualatex or xelatex with package fontspec and a color package, either color or xcolor. You do not need and should not use a package to redefine the section commands.

% Use lualatex or xelatex for this example
\documentclass[12pt]{scrartcl}
\usepackage{fontspec}
\usepackage[svgnames]{xcolor}
\defaultfontfeatures{Ligatures=TeX}
% Set sans serif font to Calibri
\setsansfont{Calibri}
% Set serifed font to Cambria
\setmainfont{Cambria}
% Define a new fontfamily for the subsubsection font
% Don't use \fontspec directly to change the font
\newfontfamily\subsubsectionfont[Color=LightBlue]{TeX Gyre Termes}
\addtokomafont{section}{\color{DarkBlue}}
\addtokomafont{subsection}{\color{MediumBlue}}
\addtokomafont{subsubsection}{\normalfont\itshape\subsubsectionfont}

\begin{document}
\section{A section}
This is some text.
\subsection{A subsection}
\subsubsection{A subsubsection}
\end{document}

example image

For more information about available elements see the KOMA-Script manual, scrguien.pdf (English) or scrguide.pdf (German).

BTW: I would not recommend to mix up different roman fonts like I've done using both, Cambria and TeX Gyre Termes.

  • Interesting solution using KOMA Script but... How to redefine the margin size as in Word? Is there a way to achieve it from typearea? – Aradnix Nov 23 '17 at 8:31
  • 1
    @Aradnix: You can still use geometry as recommended in chapter 2 of the KOMA-Script manual, iff you want so setup your own margins. – Schweinebacke Nov 23 '17 at 8:32

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.