Fonts & Character SetsSetting Arial in fontspec has no effect on the resulting PDF compiled by xelatex

Information and discussion about fonts and character sets (e.g. how to use language specific characters)
Post Reply
thinkpadder
Posts: 11
Joined: Fri May 21, 2021 12:01 pm

Setting Arial in fontspec has no effect on the resulting PDF compiled by xelatex

Post by thinkpadder »

I write a tutorial about programming using Markdown and convert it into PDF with Pandoc. However, because the default (Tex) font doesn't look appropriate for the purposes of the tutorial and the default margins are too big for the PDF, I first convert the Markdown into the tex file:

pandoc.exe my-latex-doc.md -t latex -o my-latex-doc.tex --standalone

and edit the tex file to add these:

\usepackage[left=1.5cm, right=2cm, top=2cm]{geometry}
\usepackage{fontspec}\setmainfont[]{Arial}


and do xelatex.exe my-latex-doc.tex

The tex file is compiled without any errors.

The resulting PDF displays the changes made to the margins but it doesn't display the Arial font. Here is the resulting PDF:
my-sample-tex.png
my-sample-tex.png (27.06 KiB) Viewed 11241 times
I've tried compiling with the lualatex.exe, too but the result's the same i.e. it gets compiled fine but it doesn't display the Arial font. (I 've first tried using pdflatex, but it turned out that it wasn't capable of compiling fontspec)

The complete tex file in full:

Code: Select all

% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[a4paper,12pt]{article}

% -------------------------------------------
\usepackage[left=1.5cm, right=2cm, top=2cm]{geometry}

% \usepackage[T1]{fontenc}
% \usepackage{charter}

\usepackage{fontspec}\setmainfont[]{Arial}

% \setmainfont{texgyretermes-regular.otf}[
%   BoldFont=texgyretermes-bold.otf,
%   ItalicFont=texgyretermes-italic.otf]
% --------------------------------------------

\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\newenvironment{Shaded}{}{}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{#1}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{#1}}}}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi

\author{}
\date{}

\begin{document}

\hypertarget{my-sample-on-markdown-and-latex}{%
\section{My sample on Markdown and
Latex}\label{my-sample-on-markdown-and-latex}}

Here's \emph{javascript} with \textbf{syntax highlighting}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{var}\NormalTok{ str }\OperatorTok{=} \StringTok{"Hi"}
 \FunctionTok{alert}\NormalTok{(str)}\OperatorTok{;}
\end{Highlighting}
\end{Shaded}

Here is a \emph{quadratic equation}:
\(-b \pm \sqrt{b^2 - 4ac} \over 2a\)

How I convert this MD to TeX using Pandoc on Windows7

\textbf{\$}
\texttt{pandoc.exe\ my-latex-doc.md\ -t\ latex\ -o\ my-latex-doc.tex\ -\/-standalone}

\end{document}
I'm using MikTeX-21.2 (the latest version) and the Pandoc-2.13 on Windows 7.

How to edit the tex file or any other thing so that the PDF displays the desired font, Arial or any other font?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Setting Arial in fontspec has no effect on the resulting PDF compiled by xelatex

Post by Ijon Tichy »

As long as you load other font package like lmodern setting of the main font with \setmainfont before loading the font package doesn't work, because loading of the font package will also change the main font. BTW: You should not load legacy font package like lmodern if you work with XeLaTeX or LuaLaTeX.
Last edited by Ijon Tichy on Fri May 21, 2021 4:37 pm, edited 1 time in total.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
thinkpadder
Posts: 11
Joined: Fri May 21, 2021 12:01 pm

Setting Arial in fontspec has no effect on the resulting PDF compiled by xelatex

Post by thinkpadder »

Ijon Tichy wrote:As long as you load other font package like lmodern setting of the main font with \setmainfont before loading the font package doesn't work, because loading of the font package will also change the main font. BTW: You should not load legacy font package like lmodern if you work with XeLaTeX or LuaLaTeX.

BTW: Your example code is not complete. Loading of the class is missing.
Thank you for the answer. It worked out. I've carried the \usepackage{fontspec}\setmainfont[]{Arial} line to the line right before the \begin{document} and recompiled with xelatex and finally the Arial font showed up.

Actually, all those codes including lmodern are produced by the Pandoc. The only codes I've added were those at the start between those 2 % ----------------------- lines; I had tried them by commenting in and out. Because I thought adding fontspec anywhere in the document would definitely change the default font, so I've added it into between those 2 comment lines. In this context, I didn't even notice that lmodern lines were there, because they were added by Pandoc.
Post Reply