Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

showtext package integration #799

Closed
yufree opened this issue Jun 19, 2014 · 38 comments
Closed

showtext package integration #799

yufree opened this issue Jun 19, 2014 · 38 comments
Labels
feature Feature requests
Milestone

Comments

@yufree
Copy link

yufree commented Jun 19, 2014

Description

When I try to knit Chinese into a PDF from Rmd, the Chinese can't be seen in the PDF. But the Chinese could be seen in html and doc with a warning. Then I loaded the showtext package, the warning disappeared in the html and doc but still no Chinese in PDF. I referred to the log and found Rmd produce PDF for each picture, so I think the Rmd may use the PDF as default output device. If knitr could integrate the showtext package for the PDF device or define a new device for output, the user may feel free to use some font without considering the font or encode issues.

p.s. this issue may have some similarity with this one, but I think using showtext package will cover this problem.

sessionInfo()

## R version 3.1.0 (2014-04-10)
## Platform: i686-pc-linux-gnu (32-bit)
## 
## locale:
##  [1] LC_CTYPE=zh_CN.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=zh_CN.UTF-8        LC_COLLATE=zh_CN.UTF-8    
##  [5] LC_MONETARY=zh_CN.UTF-8    LC_MESSAGES=zh_CN.UTF-8   
##  [7] LC_PAPER=zh_CN.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
## [1] digest_0.6.4     evaluate_0.5.5   formatR_0.10     htmltools_0.2.4 
## [5] knitr_1.6        rmarkdown_0.2.46 stringr_0.6.2    tools_3.1.0     
## [9] yaml_2.1.13

Example


---
title: "test"
author: "yufree"
output:
  pdf_document:
    keep_tex: yes
    latex_engine: xelatex
  html_document: default
  word_document: default

---

中英文混合

This is an R Markdown document. 

```{r}
library(showtext)
showtext.begin()
plot(cars,main='中文issue')
plot(cars,main='中文issue2')
showtext.end()
```
完
@yufree
Copy link
Author

yufree commented Jun 19, 2014

汗, 完字上下都有```的

@yihui
Copy link
Owner

yihui commented Jun 20, 2014

缩进四个空格即可。

@yufree
Copy link
Author

yufree commented Jun 21, 2014

果然如此,困扰半天,谢谢谢大!

@yihui yihui added this to the v1.7 milestone Jul 21, 2014
@yihui yihui added the Feature label Jul 21, 2014
yihui added a commit that referenced this issue Jul 21, 2014
@yihui yihui closed this as completed in 7229c5f Jul 21, 2014
@yihui
Copy link
Owner

yihui commented Jul 21, 2014

搞定。用fig.showtext=TRUE选项即可。例:

---
title: "test"
author: "yufree"
output:
  html_document: default
  pdf_document:
    keep_tex: yes
    latex_engine: xelatex
  word_document: default
---

中英文混合

This is an R Markdown document. 

```{r fig.showtext=TRUE}
plot(cars,main='中文issue',family = 'wqy')
plot(cars,main='中文issue2',family='wqy')
```

完

@yufree
Copy link
Author

yufree commented Jul 21, 2014

赞,完美解决!另附正文中中文的显示方法:

修改 "R/i686-pc-linux-gnu-library/3.1/rmarkdown/rmd/latex" 下 default.tex 文档,在 \begin{document} 之前加入

\usepackage{xeCJK}  
\setCJKmainfont{文泉驿微米黑}  % 字体可以更换

即可在rmd文档转换pdf的正文中显示中文(虽然不是knitr包的问题,但放到这里方便别人解决问题)

@yihui
Copy link
Owner

yihui commented Jul 21, 2014

谢谢,不过不需要这样黑模板,你可以把这段代码放在header.tex中,然后用includes字段把它插入模板中,例如

---
output:
  pdf_document:
    includes:
      in_header: header.tex
---

我的系统下字体名字要用WenQuanYi Micro Hei才行。

@yixuan
Copy link

yixuan commented Jul 21, 2014

Just updated a blog post briefly describing how to use showtext in knitr. If needed, this file can be used as an example in knitr-examples.
Thanks for the nice work!

@yufree
Copy link
Author

yufree commented Jul 22, 2014

大赞!没意识到yaml这个功能,这样岂不是可以在rmd里随意调用latex的包了,直接header.tex里加入宏包,然后正文里可以写tex代码了,尝试了下tikz可行,rmd一统江湖,写论文应该用不到rnw了,现在感觉就差文献管理软件整合了。

同时感谢@yixuan的介绍,作为knitr与showtext重度用户十分感谢二位的工作!

@yihui
Copy link
Owner

yihui commented Jul 22, 2014

是啊,Pandoc的设计有超级牛力的。文献方面不知道你看过这个没有:http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html 基本上你要是用过BibTeX的话就没什么障碍了。

@yufree
Copy link
Author

yufree commented Jul 22, 2014

还真没看过但BibTex折腾过,这功能太实用了,强赞!

@BigHai
Copy link

BigHai commented Nov 1, 2014

我是个latex新手,win7系统,xelatex和字库始终没弄协调,fc中有的字体也编译不出来。
我想用ctex包来处理正文中的中文字符,创建了一个header.tex文件,只包含一句
\usepackage{ctex}
并将其与需编译的rmd文件放到了同一个文件夹

output:
pdf_document:
latex_engine: pdflatex
keep_tex: true
includes:
in_header: header.tex

但是从生成的tex文件里发现并没有加上header.tex的内容
自然中文字符显示都是乱码,到底是哪里出问题了,麻烦解答一下。谢谢!

@yufree
Copy link
Author

yufree commented Nov 1, 2014

yaml语法是要求有缩进的 参照谢大上面的代码即可 另外文档编码一定用UTF-8 不然容易烫着

@yihui
Copy link
Owner

yihui commented Nov 1, 2014

我前一段时间受一个老美推荐研究一下fandol字体,发现它可能会是一个通用的解决方案。因为它是一个免费的包,所以我们不必再藏头缩尾去用那些商业Adobe中文字体了。我在Linux/Mac底下都测试了,只要字体安装上了,ctex里面用nofonts选项就会自动寻找fandol字体。注意字体是安装到系统中,而不是作为一个LaTeX包安装上。不知道Yu老大是否有空研究一下,然后我们可以给rticles提交一个PR(写一个rticles::pdf_ctex()函数):rstudio/rticles#1

@yufree
Copy link
Author

yufree commented Nov 2, 2014

我也尝试了下,安装fandol字体后确实用nofonts选项就可以调用了,不过要是xetex编译,换成pdflatex还是中止。之后我查了下ctex的文档,里面大概的意思如下:

  • ctex默认选项为winfonts 默认调用windows下的四种随系统安装字体;
  • 使用macfonts选项则会调用mac系统下华文那几种字体,也是随系统安装的,svn版ctex包新特性;
  • adobefonts 是为其他系统服务的,但只支持xetex编译,这几个字体是商用的
  • nofonts 就需要自己设定字体了,因为本质上用了xecjk包,所以设定命令与该包一致
  • linux系统下字体名比较混乱,所以要手动设置,这解释了我电脑上文泉驿的名字与谢大的不一样的问题,详见这里
  • 上面这些选项只对xetex编译有效,pdflatex虽然老外用的多,但真不符合国情

但我看了下ctex的源码,在fontset选项里莫名其妙多出一个fandol选项,所以我猜测在使用nofonts选项时实际调用的是fandol选项,这大概解释了自动寻找字体的问题,但官方文档中没有提及这个配置,所以存在ctex包升级后这个选项失效的风险。

关于fandol字体,据说源自某破产字体厂商,后来被挖出来开源了,不过似乎没有完全支持所有汉字的编码,这倒没关系,有比没有好太多。这个项目还有个只有12刀的基金会,那个作者应该是知乎上的李阿玲,latex专家,不知道用了什么法子让ctex包里多出个fandol的选项,但基于此写文档倒是规避了字体设置的问题了。

眼下我也没有windows系统,不过写死了必须用utf-8跟xetex编译,应该没有问题。riticles我也特别想写模版,因为投稿要用一个化学类期刊的latex包achemso,结果它只提供了文档类调用,这样就需要pandoc转化时加参数,然后我就直接lyx写了。如果用PR的话,应该是将其作为一个preamble来调用吧,那这个函数rticles::pdf_ctex()的作用应该是在文档转换前加入一句类似\documentclass[UTF8,nofonts]{ctexart}来指定文档类别或者直接把\usepackage[UTF8,nofonts]{ctex}写到导言区里吧?具体怎么实现我还没概念,希望谢大指导!

ps: 我水平很菜,最多就是读懂代码,但写的话就渣了,谢大不要Yu老大,我跟您差了十万八千里,都是读您的文章入门的。

@yixuan
Copy link

yixuan commented Nov 2, 2014

中文字体就是这点麻烦,太多,所以很少有免费又合法的……

@yihui
Copy link
Owner

yihui commented Nov 2, 2014

@yixuan 是啊,好不容易出来一个免费字体,看起来还比较美观,但我也一直没找到它的来源,从上面 @yufree 的八卦才了解到一点,感觉fandol的作者好像不太靠谱的样子(虽然很用功),从马路边捡了个字体就拿来GPL了,完全是黑户啊。

@yufree 谢谢深挖,信息很全面 👍

忽略下面的回复,我觉得好像走歪了,需要再想想。


关于pdf_ctex()我的想法如下:

  • 它的latex_engine参数(参见rmarkdown::pdf_document)默认取值在Windows下用pdflatex,Mac/Linux下用xelatex,即latex_engine = if (.Platform$OS.type == 'windows') 'pdflatex' else 'xelatex',参数都是可以在YAML元数据里面修改的,所以这样默认设置应该是合理,而且用户方便修改的
  • 通过pandoc_args参数传递合理的默认classoption和documentclass变量数据:https://github.com/rstudio/rmarkdown/blob/master/inst/rmd/latex/default.tex
pdf_ctex = function() {
  ...
}
---
output:
  rticles::pdf_ctex
    ctex:
      classoption: nofonts
      documentclass: ctexart
---

@yufree
Copy link
Author

yufree commented Nov 3, 2014

看到今天加入了plos one的期刊模版,我打算把化学类的期刊(美国化学会旗下)参照着加一下,凭空写不行,但照虎画猫大概还行,如有错误还望楼上几位多加指点。

@yufree
Copy link
Author

yufree commented Nov 3, 2014

我今天试图创建acs模版,基本完成,但有几个问题

  • email 的链接无法生成
  • 作者列表总是无法循环得到,我怀疑与achemso包的一些设定有关
  • achemso包中定义了一些环境,markdown好像无法实现,只好在模版里写plain tex了

希望大家有空指点下,谢谢!
rstudio/rticles#15

@yihui
Copy link
Owner

yihui commented Nov 3, 2014

照虎画猫也是本事。你表太谦虚。世上哪有那么多纯创新的东西,0.1%创新加99.9%没技术含量的苦力活就足够了,有多少苦力活是没人愿意干的啊,大家都盯着创新,仿佛创新能拯救世界,其实不然。

@yihui
Copy link
Owner

yihui commented Nov 4, 2014

R Markdown中文PDF支持完毕:rstudio/rticles#16 敬请测试:

devtools::install_github('rstudio/rticles')

@korterling
Copy link

header.tex文件在哪里找到

@yufree
Copy link
Author

yufree commented Jun 30, 2015

@yangbenfa 自己新建一个header.tex文档放在需要编译的rmd文档目录下,里面加入需要的tex代码即可在文档编译时调用,不过眼下用rticles包的中文模版就可以了

@korterling
Copy link

@yufree 好的,谢谢!,因为使用rticles,只显示了图片中的中文,而正文中文没显示 ,故还是使用header.tex来尝试一下,

@yihui
Copy link
Owner

yihui commented Jun 30, 2015

@yangbenfa 不太可能吧,rticles里的中文我都设置好了啊,是不是你没有安装文章中说的字体?https://github.com/rstudio/rticles/blob/master/inst/rmarkdown/templates/ctex/skeleton/skeleton.Rmd

@korterling
Copy link

@yihui 是的,安装了字体后,使用CTeX Documents模版,不显示中文,只有英文,于是,我就修改了其中一行,classoption: "hyperrefr if (.Platform$OS.type != 'windows') ',nofonts'"
改成了classoption: nofonts;结果连英文都不显示了,直接报错,信息如下:

output file: 转pdf,中文问题1.knit.md

! Undefined control sequence.
l.75 ...�题。当然这句话得站在巨人\href

pandoc: Error producing PDF from TeX source
错误: pandoc document conversion failed with error 43
停止执行

@korterling
Copy link

@yufree
最后的解决方案是,

修改 "R/i686-pc-linux-gnu-library/3.1/rmarkdown/rmd/latex" 下 default.tex 文档,在 \begin{document} 之前加入字体,搞定,测试结果如下:
---
title: "test"
author: "test"
date: "2015年7月1日"
output:
pdf_document:
latex_engine: xelatex
---

你好,测试一下

```{r, echo=FALSE,fig.showtext=TRUE}
plot(cars,main="你好")
```

@korterling
Copy link

由于没有调用模版,故排版很差,于是想借助模版来优化一下排版,就调用CTeX Documents模版信息,增加一行 template: !expr rticles::ctex_template(),结果又报错了,为什么,不使用模版,一切正常,使用了模版,就错误,如下:
! Undefined control sequence.
l.75 ...�题。当然这句话得站在巨人\href

pandoc: Error producing PDF from TeX source
错误: pandoc document conversion failed with error 43
停止执行

@korterling
Copy link

 弱弱的问一句,其实只要中文显示好了,一旦生成pdf文件,不管对方是windows,还是Linux,都可以打开,并正常显示吧,
所以,我觉得“装鸵鸟,把头埋沙子里,自己默念一百遍“别人都不用Windows,我只管我的系统上的字体能用就好了”;”这句话似乎不成立

@korterling
Copy link

经过一番尝试,终于解决,原来问题出在这里classoption: "hyperrefr if (.Platform$OS.type != 'windows') ',macfonts'",使用模版也可以了

@yufree
Copy link
Author

yufree commented Jul 1, 2015

@yangbenfa 看你用macfonts这个选项了,说明ctex已更新。我查了下 https://github.com/CTeX-org/ctex-kit 确实对mac系统支持了macfonts选项,那么rticles里可能要做点更新,感谢提醒!
@yihui 是不是classoption可以写成下面这样?

classoption: "hyperref`r if (Sys.info()["sysname"]== 'Linux') ',nofonts' else if (.Platform$OS.type == 'Darwin') ',macfonts' `"

我更新到最新版了,可以重复出问题了,看起来ctex 2.x的更新比较大,上面那个代码不行,估计得更新下skeleton了

额,又看了下,其实现在可以不管哪个字体设置了,ctex 2.x可以自动适配系统,这样可以直接在rmd文档中指定文档类型就可以显示中文了,不过默认pandoc的tex模版里有些语句需要删掉,否则编译还是通不过,我几乎删光了才编译成功,目测是超链接的问题。

@yihui
Copy link
Owner

yihui commented Jul 2, 2015

@yufree 好,那确实需要更新文档了。我最近有点忙,你要是找出来毛病在哪儿欢迎提交PR。谢谢!

@yufree
Copy link
Author

yufree commented Jul 2, 2015

@yihui 额,我先给一个比较脏的skeleton修改yaml临时方案吧:

classoption: "hyperref`r ','`"

目前ctex也不建议自己调字体,那就直接忽略算了,过会我PR过去,不过模版里的文本估计要重新组织,我暂时会在模版里加一个说明,因为估计多数没意识到latex的包也会出现大更新。

@yihui
Copy link
Owner

yihui commented Jul 6, 2015

文本已经大致重新组织了一下。至于Pandoc模板是啥问题,就留给你慢慢研究了。

@yufree
Copy link
Author

yufree commented Jul 6, 2015

@yihui 谢大辛苦 pandoc模版似乎没问题 是我当时没理解hyperref这个选项跟您做的修改 重新尝试已经没问题了
ps: 在yaml里写r代码这个黑魔法很赞

@andyfayecindy
Copy link

@yangbenfa 我 include header.tex 后生成的tex文件中文也是乱码,请问这个问题你解决了吗?

@andyfayecindy
Copy link

@yihui

---
title: "这是一个标题"
output:
  pdf_document:
    includes:
      in_header: header.tex
    keep_tex: yes   
    latex_engine: xelatex
  word_document: default
  html_document: default
---

 中文 

header.tex 文件
\usepackage{ctex} 

pdf可以显示中文,但是生成的tex文件中文都是乱码,并没有把中文包包含近来,怎么修改呢?

@changyeli
Copy link

请问header.tex这个文件需不需要加上\begin{document}之类的code?还是只需要\usepackages{...}就可以了?

@yihui
Copy link
Owner

yihui commented Jan 15, 2016

不需要。后来者请不要再在这个帖子底下回复,中文Markdown转CTeX/LaTeX/PDF的问题早已经在rticles包中解决了:https://github.com/rstudio/rticles

Repository owner locked and limited conversation to collaborators Jan 15, 2016
heavenzone referenced this issue in heavenzone/zhonghaoguang.com Jan 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Feature requests
Projects
None yet
Development

No branches or pull requests

7 participants