安装 MingLiU 和 Noto Sans 字体并将其与 TinyTeX 结合使用

安装 MingLiU 和 Noto Sans 字体并将其与 TinyTeX 结合使用

我通过 Quarto 使用 TinyTex。后者会自动安装缺失的字体。但是,这两种字体似乎不可用。

我有以下 Quarto 文件:

---
title: "unicode_pdf"
# pdf-engine: xelatex
format:
  html:
    fontsize: 12pt
    # mainfont: "Noto Sans"
    # monofont: "Noto Sans"
    mainfont: "MingLiU"
    monofont: "MingLiU"
  pdf:
    fontsize: 12pt
    # mainfont: "SimHei"
    # monofont: "SimHei"
    mainfont: "MingLiU"
    monofont: "MingLiU"
---

## JavaScript

Javascript 是一种解释性语言。 C 是一种编译语言。

```JAVASCRIPT
config.log('Hi!, 你好世界');
```

在文档上运行 quarto render 时,出现以下错误:

updating tlmgr

updating existing packages
finding package for MingLiU(-(Bold|Italic|Regular).*)?[.](tfm|afm|mf|otf|ttf)
finding package for MingLiU
finding package for mktextfm
> 1 package to install
> installing texlive-scripts (1 of 1)
  This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex)
   restricted \write18 enabled.
  entering extended mode

  kpathsea: Running mktextfm MingLiU

  The command name is C:\Users\xxx\AppData\Roaming\TinyTeX\bin\windows\mktextfm
  name = MingLiU, rootname = MingLiU, pointsize =
  mktexmf: empty or non-existent rootfile!

  kpathsea: Running mktexmf MingLiU.mf

  The command name is C:\Users\xxx\AppData\Roaming\TinyTeX\bin\windows\mktexmf
  Cannot find MingLiU.mf.
  kpathsea: Appending font creation commands to missfont.log.

finding package for MingLiU(-(Bold|Italic|Regular).*)?[.](tfm|afm|mf|otf|ttf)
finding package for MingLiU
finding package for mktextfm

compilation failed- package installation error
Package fontspec Error: The font "MingLiU" cannot be found.

For immediate help type H <return>.
 ...

l.27   \setmonofont
                   []{MingLiU}

这是生成的 tex 文件:

% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
%
\documentclass[
  12pt,
  letterpaper,
  DIV=11,
  numbers=noendperiod]{scrartcl}

\usepackage{amsmath,amssymb}
\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
\usepackage{lmodern}
\ifPDFTeX\else  
    % xetex/luatex font selection
  \setmainfont[]{MingLiU}
  \setmonofont[]{MingLiU}
\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}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
% Make \paragraph and \subparagraph free-standing
\ifx\paragraph\undefined\else
  \let\oldparagraph\paragraph
  \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
  \let\oldsubparagraph\subparagraph
  \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{241,243,245}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.40,0.45,0.13}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\BuiltInTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\ExtensionTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.28,0.35,0.67}{#1}}
\newcommand{\ImportTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\NormalTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.07,0.07,0.07}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}

\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}\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}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother

\KOMAoption{captions}{tableheading}
\makeatletter
\makeatother
\makeatletter
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\AtBeginDocument{%
\ifdefined\contentsname
  \renewcommand*\contentsname{Table of contents}
\else
  \newcommand\contentsname{Table of contents}
\fi
\ifdefined\listfigurename
  \renewcommand*\listfigurename{List of Figures}
\else
  \newcommand\listfigurename{List of Figures}
\fi
\ifdefined\listtablename
  \renewcommand*\listtablename{List of Tables}
\else
  \newcommand\listtablename{List of Tables}
\fi
\ifdefined\figurename
  \renewcommand*\figurename{Figure}
\else
  \newcommand\figurename{Figure}
\fi
\ifdefined\tablename
  \renewcommand*\tablename{Table}
\else
  \newcommand\tablename{Table}
\fi
}
\@ifpackageloaded{float}{}{\usepackage{float}}
\floatstyle{ruled}
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
\floatname{codelisting}{Listing}
\newcommand*\listoflistings{\listof{codelisting}{List of Listings}}
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\@ifpackageloaded{subcaption}{}{\usepackage{subcaption}}
\makeatother
\makeatletter
\@ifpackageloaded{tcolorbox}{}{\usepackage[skins,breakable]{tcolorbox}}
\makeatother
\makeatletter
\@ifundefined{shadecolor}{\definecolor{shadecolor}{rgb}{.97, .97, .97}}
\makeatother
\makeatletter
\makeatother
\makeatletter
\makeatother
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same} % disable monospaced font for URLs
\hypersetup{
  pdftitle={unicode\_pdf},
  colorlinks=true,
  linkcolor={blue},
  filecolor={Maroon},
  citecolor={Blue},
  urlcolor={Blue},
  pdfcreator={LaTeX via pandoc}}

\title{unicode\_pdf}
\author{}
\date{}

\begin{document}
\maketitle
\ifdefined\Shaded\renewenvironment{Shaded}{\begin{tcolorbox}[breakable, sharp corners, interior hidden, borderline west={3pt}{0pt}{shadecolor}, enhanced, frame hidden, boxrule=0pt]}{\end{tcolorbox}}\fi

\hypertarget{javascript}{%
\subsection{JavaScript}\label{javascript}}

Javascript 是一种解释性语言。 C 是一种编译语言。

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{config}\OperatorTok{.}\FunctionTok{log}\NormalTok{(}\StringTok{\textquotesingle{}Hi!, 你好世界\textquotesingle{}}\NormalTok{)}\OperatorTok{;}
\end{Highlighting}
\end{Shaded}




\end{document}

答案1

我现在可以解决这个问题了。让我在这里为想要使用其他字体(例如 truetype 字体)的任何人更新背景和解决方案。

注意:此处的解决方案适用于 Microsoft Windows 操作系统。

Quarto 使用 TinyTeX 将 qmd 文件转换为 tex 文件,并通过 xeletax(TinyTex 自带)将 tex 文件转换为 pdf 文件。

在我的环境中,%APPDATA%C:/Users/yhm2/AppData/Roaming

  1. 在 中添加包含所需字体的目录%APPDATA%\TinyTeX\texmf-var\fonts\conf\fonts.conf。对于我的环境,该文件具有以下配置:
<dir>C:/WINDOWS/fonts</dir>
<dir>C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-dist/fonts/opentype</dir>
<dir>C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-dist/fonts/truetype</dir>
<dir>C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-local/fonts/truetype</dir>
  1. 运行%APPDATA%\TinyTeX\bin\windows\fc-cache.exe -fv以更新在中配置的目录下的字体列表%APPDATA%\TinyTeX\texmf-var\fonts\conf\fonts.conf

  2. 运行%APPDATA%\TinyTeX\bin\windows\fc-list.exe查看映射字体列表。在我的环境中,部分输出如下:

C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-local/fonts/truetype/NotoSansSC-Black.ttf: Noto Sans SC,Noto Sans SC Black:style=Black,Regular
C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-local/fonts/truetype/NotoSansSC-Bold.ttf: Noto Sans SC:style=Bold
C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-local/fonts/truetype/NotoSansSC-ExtraBold.ttf: Noto Sans SC,Noto Sans SC ExtraBold:style=ExtraBold,Regular
C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-local/fonts/truetype/NotoSansSC-ExtraLight.ttf: Noto Sans SC,Noto Sans SC ExtraLight:style=ExtraLight,Regular
C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-local/fonts/truetype/NotoSansSC-Light.ttf: Noto Sans SC,Noto Sans SC Light:style=Light,Regular
C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-local/fonts/truetype/NotoSansSC-Medium.ttf: Noto Sans SC,Noto Sans SC Medium:style=Medium,Regular
C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-local/fonts/truetype/NotoSansSC-Regular.ttf: Noto Sans SC:style=Regular
C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-local/fonts/truetype/NotoSansSC-SemiBold.ttf: Noto Sans SC,Noto Sans SC SemiBold:style=SemiBold,Regular
C:/Users/yhm2/AppData/Roaming/TinyTeX/texmf-local/fonts/truetype/NotoSansSC-Thin.ttf: Noto Sans SC,Noto Sans SC Thin:style=Thin,Regular

简称TinyTeX 用来映射实际文件的名称是字体文件名后面的名称。例如,简称Noto Sans SC-* 的意思是“Noto Sans SC”。

所以我应该在我的 qmd 文件中使用“Noto Sans SC”:

---
title: "unicode_pdf"
pdf-engine: xelatex
format:
  html:
    fontsize: 12pt
    mainfont: "Noto Sans SC"
    monofont: "Noto Sans SC"
  pdf:
    fontsize: 12pt
    mainfont: "Noto Sans SC"
    monofont: "Noto Sans SC"
---

## JavaScript

Javascript 是一种解释性语言。 C 是一种编译语言。

```JAVASCRIPT
config.log('Hi!, 你好世界');
```

相关内容