天天看點

Mac中使用LaTeX的中文字型出現Package fontspec Error: The font “宋體“ cannot be found.解決方案

文章目錄

  • ​​問題​​
  • ​​分析與解決​​
  • ​​小結​​
  • ​​參考​​

問題

最近寫論文需要用到LaTeX,之前我已經介紹過Mac上安裝Mac TeX的方法,用起來跟在Windows中沒有差別,但是在使用中文字型的時候,還是會發生諸如以下的錯誤:

! Package fontspec Error: The font "宋體" cannot be found.
! Package fontspec Error: The font "黑體" cannot be found.      

在調用的地方我采用了Windows下編譯通過的模闆,即(字型設定部分):

\setCJKmainfont{宋體}%襯線字型 
\setCJKsansfont{黑體}%serif是有襯線字型sans serif無襯線字型。
\setCJKmonofont{仿宋}%中文等寬字型      

但是這些字型在Mac中竟然找不到…

分析與解決

一開始我以為需要安裝字型才能解決問題(網上很多教程中也是這樣說的…),之後發現并不是這樣,隻是Mac中的字型表示與Windows中不同,是以在Windows下可以編譯的中文字型設定,到了Mac中反而會顯示字型缺失.

遇到這種情況,我的做法是不用加這些字型的設定,直接command+space調出spotlight,輸入font進入字型冊,找到LaTeX編譯提示缺失的字型,例如我上面提到的宋體,在字型冊中顯示為:STSong(需要點​

​預覽模式​

​為顯示字型資訊即可看到字型檔案名稱)

Mac中使用LaTeX的中文字型出現Package fontspec Error: The font “宋體“ cannot be found.解決方案

并将上面的字型配置依次修改為對應的字型檔案名稱(圖中的PostScript 名稱):

\setCJKmainfont{STSong}%襯線字型 
\setCJKsansfont{STHeitiSC-Light}%serif是有襯線字型sans serif無襯線字型。
\setCJKmonofont{STFangsong}%中文等寬字型      

就可以完美編譯啦!

如果需要在文中設定字型,無需導入其他宏集(文檔類為ctex字首的話自動進行中文字型支援),隻需要采用ctex的内置字型修改指令(具體可參考ctex文檔):

{\songti 你好世界!}
{\lishu 你好世界!}
{\heiti 你好世界!}      
Mac中使用LaTeX的中文字型出現Package fontspec Error: The font “宋體“ cannot be found.解決方案

即可完成字型的修改,另外再附上字号的調整指令:

\newcommand{\chuhao}{\fontsize{42pt}{1.25\baselineskip}\selectfont}     %初号
\newcommand{\xiaochuhao}{\fontsize{36pt}{1.25\baselineskip}\selectfont} %小初号
\newcommand{\yihao}{\fontsize{28pt}{1.25\baselineskip}\selectfont}      %一号
\newcommand{\erhao}{\fontsize{21pt}{1.25\baselineskip}\selectfont}      %二号
\newcommand{\xiaoerhao}{\fontsize{18pt}{1.25\baselineskip}\selectfont}  %小二号
\newcommand{\sanhao}{\fontsize{15.75pt}{1.25\baselineskip}\selectfont}  %三号
\newcommand{\sihao}{\fontsize{14pt}{1.25\baselineskip}\selectfont}%     四号
\newcommand{\xiaosihao}{\fontsize{12pt}{1.25\baselineskip}\selectfont}  %小四号
\newcommand{\wuhao}{\fontsize{10.5pt}{1.25\baselineskip}\selectfont}    %五号
\newcommand{\xiaowuhao}{\fontsize{9pt}{1.25\baselineskip}\selectfont}   %小五号
\newcommand{\liuhao}{\fontsize{7.875pt}{1.25\baselineskip}\selectfont}  %六号
\newcommand{\qihao}{\fontsize{5.25pt}{1.25\baselineskip}\selectfont}    %七号      

這樣在調整的時候就可以采用如下指令,例如标題需要設定為隸書二号字,就可以設定為:

{\lishu\erhao 這裡寫上标題}      

小結

參考

  1. 《LaTeX入門》劉海洋
  2. ctex宏集手冊