• R语言已解决
  • 用 CTeX 模板生成中文 PDF 时,latex 的加粗命令 \mathbf{} 或 \bf{} 不起作用

当用Ctex模板生成含有中文的pdf时,加粗命令\mathbf{}\bf{}不起作用。
比如输入$\mathbf{\Sigma}$时,在编译出来的pdf文件中,该位置直接出现空白。但这一问题在不用Ctex模板编译成pdf时却不会出现。
不知道其他人有没有碰到这个奇怪的问题?

我rmarkdown的测试文档如下:

---
title: "测试"
author:
  - 无
documentclass: ctexart
keywords:
  - 无
output:
  rticles::ctex:
    fig_caption: yes
    number_sections: yes
    toc: yes
geometry: tmargin=1.8cm,bmargin=1.8cm,lmargin=2.1cm,rmargin=2.1cm  
---
$\mathbf{\Sigma}$

相应的系统信息为

xfun::session_info()
#> R version 4.1.1 (2021-08-10)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19042)
#> 
#> Locale:
#>   LC_COLLATE=Chinese (Simplified)_China.936 
#>   LC_CTYPE=Chinese (Simplified)_China.936   
#>   LC_MONETARY=Chinese (Simplified)_China.936
#>   LC_NUMERIC=C                              
#>   LC_TIME=Chinese (Simplified)_China.936    
#> 
#> Package version:
#>   backports_1.2.1   base64enc_0.1.3   callr_3.7.0       cli_3.0.1        
#>   clipr_0.7.1       compiler_4.1.1    crayon_1.4.1      digest_0.6.28    
#>   ellipsis_0.3.2    evaluate_0.14     fansi_0.5.0       fastmap_1.1.0    
#>   fs_1.5.0          glue_1.4.2        graphics_4.1.1    grDevices_4.1.1  
#>   highr_0.9         htmltools_0.5.2   jquerylib_0.1.4   jsonlite_1.7.2   
#>   knitr_1.34        lifecycle_1.0.1   magrittr_2.0.1    methods_4.1.1    
#>   pillar_1.6.2      pkgconfig_2.0.3   processx_3.5.2    ps_1.6.0         
#>   purrr_0.3.4       R.cache_0.15.0    R.methodsS3_1.8.1 R.oo_1.24.0      
#>   R.utils_2.10.1    R6_2.5.1          rematch2_2.1.2    reprex_2.0.1     
#>   rlang_0.4.11      rmarkdown_2.11    rprojroot_2.0.2   rstudioapi_0.13  
#>   stats_4.1.1       stringi_1.7.4     stringr_1.4.0     styler_1.6.2     
#>   tibble_3.1.4      tinytex_0.33      tools_4.1.1       utf8_1.2.2       
#>   utils_4.1.1       vctrs_0.3.8       withr_2.4.2       xfun_0.26        
#>   yaml_2.2.1

rmarkdown::pandoc_version()
#> [1] '2.11.4'

<sup>Created on 2021-09-25 by the reprex package (v2.0.1)</sup>

我这边也存在这个问题,测试文档 1 生成的 PDF 文件是空白,测试文档 2 生成的 PDF 文件正常显示。

测试文档 1

---
documentclass: ctexart
output:
  rticles::ctex:
    keep_tex: TRUE
---

$\mathbf{\Sigma}$

测试文档 1 生成的 tex1 文档

% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{ctexart}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \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{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
\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{\vspace{-2.5em}}

\begin{document}

<code class="katex-escape">\(\mathbf{\Sigma}\)</code>

\end{document}

测试文档 2

---
output:
  pdf_document:
    keep_tex: TRUE
---

$\mathbf{\Sigma}$

测试文档 2 生成的 tex2 文档

% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \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[margin=1in]{geometry}
\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
\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{\vspace{-2.5em}}

\begin{document}

<code class="katex-escape">\(\mathbf{\Sigma}\)</code>

\end{document}

tex1 和 tex2 文档差异部分

tex1 里文档类型是 \documentclass[]{ctexart},tex2 里的文档类型 \documentclass[]{article}

另外 tex2 文档里多了 \usepackage[margin=1in]{geometry}

系统环境

xfun::session_info()
#> R version 4.1.1 (2021-08-10)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.3 LTS
#> 
#> Locale:
#>   LC_CTYPE=zh_CN.UTF-8       LC_NUMERIC=C              
#>   LC_TIME=zh_CN.UTF-8        LC_COLLATE=zh_CN.UTF-8    
#>   LC_MONETARY=zh_CN.UTF-8    LC_MESSAGES=zh_CN.UTF-8   
#>   LC_PAPER=zh_CN.UTF-8       LC_NAME=C                 
#>   LC_ADDRESS=C               LC_TELEPHONE=C            
#>   LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C       
#> 
#> Package version:
#>   backports_1.2.1   base64enc_0.1.3   callr_3.7.0       cli_3.0.1        
#>   clipr_0.7.1       compiler_4.1.1    crayon_1.4.1      digest_0.6.27    
#>   ellipsis_0.3.2    evaluate_0.14     fansi_0.5.0       fastmap_1.1.0    
#>   fs_1.5.0          glue_1.4.2        graphics_4.1.1    grDevices_4.1.1  
#>   highr_0.9         htmltools_0.5.2   jsonlite_1.7.2    knitr_1.33       
#>   lifecycle_1.0.0   magrittr_2.0.1    markdown_1.1      methods_4.1.1    
#>   mime_0.11         pillar_1.6.2      pkgconfig_2.0.3   processx_3.5.2   
#>   ps_1.6.0          purrr_0.3.4       R.cache_0.15.0    R.methodsS3_1.8.1
#>   R.oo_1.24.0       R.utils_2.10.1    R6_2.5.1          rematch2_2.1.2   
#>   reprex_2.0.0      rlang_0.4.11      rmarkdown_2.10    rprojroot_2.0.2  
#>   rstudioapi_0.13   stats_4.1.1       stringi_1.7.4     stringr_1.4.0    
#>   styler_1.5.1      tibble_3.1.4      tinytex_0.33      tools_4.1.1      
#>   utf8_1.2.2        utils_4.1.1       vctrs_0.3.8       withr_2.4.2      
#>   xfun_0.25         yaml_2.2.1
rmarkdown::pandoc_version()
#> [1] '2.11.4'

<sup>Created on 2021-09-25 by the reprex package (v2.0.0)</sup>

所以还得比较 pandoc 的 article 和 ctexart 模板之间的差异……

    9-5-2-7 貌似这个加粗的 Σ\Sigma 符号只能在 latex_engine: pdflatex 下正常编译,但是 pdflatex 对中文支持比较差。

      chuxinyuan 我建议你不要再用 rticles 里的中文模版,如今 Pandoc 内置的 LaTeX 模版已经足够好地支持了中文。

      BTW,上次你提到 R Markdown 里制作 beamer 幻灯片的过程,分享在这里

        chuxinyuan 更改标题为「用 CTeX 模板生成中文 PDF 时,latex 的加粗命令 \mathbf{} 或 \bf{} 不起作用