Fonts & Character SetsAltering the Caption Font

Information and discussion about fonts and character sets (e.g. how to use language specific characters)
Post Reply
abclatex
Posts: 17
Joined: Wed Nov 28, 2012 10:09 pm

Altering the Caption Font

Post by abclatex »

Hi forum friends,

I wonder how to change the caption font of images in margin (sf) to Calibri-Light, which is also the font of the main text.

You can see the current condition that fonts of text and image caption of are different.
image 1.JPG
image 1.JPG (15.08 KiB) Viewed 22399 times
Sample code.

Code: Select all

\documentclass[10pt,a4paper]{book}
\usepackage{fancyhdr}
\pagestyle{plain}
\renewcommand{\headrulewidth}{0.4pt} \renewcommand{\footrulewidth}{0.4pt}
\usepackage[german,french]{babel}
\usepackage{xltxtra}
\usepackage{fontspec}
\usepackage{graphicx}
\graphicspath{{fig/}}
\usepackage{caption}
\captionsetup[figure]{name={Figure.}}
\captionsetup{font={scriptsize,sf,singlespacing}}


\begin{document}
\frontmatter
\begin{flushleft}
\thispagestyle{empty}
\pagenumbering{Roman}
\mainmatter
\fontspec{Calibri-Light}


\chapter{chapter1}
"Chasing Pavements Chasing Pavements Chasing Pavements Chasing Pavements Chasing Pavements Chasing Pavements Chasing Pavements" \marginpar{\centering
 \includegraphics[width=\marginparwidth]{1.jpg}
 \captionof{figure}{TextTextTextTextTextTextTextTextTextTextTextTextText,TextTextTextTextTextTextText,TextTextTextText}} (Figure 1.)


\backmatter
\end{flushleft}
\end{document}
Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Altering the Caption Font

Post by cgnieder »

The solution is simple. However, I have some suggestions regarding your code in general, too. I am going to list them all and include them in an example code.
  • The main document font should be chosen in the preamble with \setmainfont{font name} rather than using \fontspec
  • if you do that and leave the sf in the caption setup out you should get the same font in the captions as well.
  • Instead of manually putting the figure in the margin you could use the wrapfig package to define a {marginfigure} environment that would give a cleaner semantic markup in your text. You could then also use \caption directly.
  • Do not use " for citations but ``citet text'' to get real opening and closing quotation marks. Better yet: use csquotes as this will give you quotations marks suiting to the typographic conventions of the chosen document language (French in your case).
  • I'd use \label and \ref to refer to the figure.
  • Don't put your whole document in a {flushleft} environment. Rather set \raggedright at the beginning or \RaggedRight from the ragged2e package for a ragged body that also allows hyphenation.
  • I'd also set the captions raggedright when used in the margin. It will avoid lots of bad boxes and give (IMHO) a nicer result optically.
  • All in all I get the expression you might like the tufte-book document class. It uses a ragged text body and provides an environment for margin figures.
  • I have used a different font in the example below since I don't have Calibri installed (It is a Microsoft font if I know correctly?)

Code: Select all

\documentclass[10pt,a4paper]{book}
% Language settings
\usepackage[french]{babel}

% font and other typographic settings:
\usepackage{fontspec}
\setmainfont[Ligatures=TeX]{Bitstream Vera Sans}
\usepackage{ragged2e}

% quotes:
\usepackage{csquotes}

% figures and the like:
\usepackage{graphicx}
\usepackage{mwe}% just for this example as it provides `example-image-a'
\usepackage{caption}
\captionsetup[figure]{name={Figure.}}
\captionsetup{font={scriptsize,singlespacing},justification=RaggedRight}
\usepackage{wrapfig}

% {marginfigure} definition
\newlength{\marginspace}
\setlength{\marginspace}{\dimexpr\marginparwidth+\marginparsep\relax}
\setlength{\wrapoverhang}{\dimexpr\marginspace+\columnsep\relax}
\newenvironment{marginfigure}
  {\wrapfigure{o}{\marginspace}}
  {\endwrapfigure}

\begin{document}
\RaggedRight
\frontmatter
\thispagestyle{empty}
\pagenumbering{Roman}
\mainmatter

\chapter{chapter1}
\begin{marginfigure}
 \centering
 \includegraphics[width=\marginparwidth]{example-image-a}
 \caption{TextTextTextTextTextTextTextTextTextTextTextTextText,
 TextTextTextTextTextTextText,TextTextTextText}
 \label{fig:example}
\end{marginfigure}

\enquote{Chasing Pavements Chasing Pavements Chasing Pavements Chasing Pavements Chasing
Pavements Chasing Pavements Chasing Pavements} (Figure \ref{fig:example})

\backmatter
\end{document}
marginfigure.png
marginfigure.png (14.55 KiB) Viewed 22390 times
Regards
site moderator & package author
abclatex
Posts: 17
Joined: Wed Nov 28, 2012 10:09 pm

Re: Altering the Caption Font

Post by abclatex »

Many...thanks to you, cgnieder.

Your advice is totally great and helps me a lot to correct mistakes and reorganize the whole codes.

I'll read other recommended packages later.

Since the paper (around 200 pages) has lots of footnotes at margins plus images at the main body or margins, is the tufte-book document class better than others? Frankly, I have no idea about this issue.
Post Reply