R语言学习笔记--R bookdown图表设置中英文双标题

一. 测试文档,该文档修改自 bookdown 的官方中文模板

该方案可以实现同时生成pdf和html两种格式的文件,pdf文件中实现双标题,html文件可正常生成单个标题。

二. 结合 CTEX 配置使用 latex 的运行环境

首先安装 TeX 用的发行版,yihui等推荐使用 TinyTex

配置template.tex文件:template.tex文件位于 latex 文件夹中,在\begin{document}前加入:

\usepackage{ctex}
\usepackage[list=off]{bicaption}
\captionsetup[figure][bi-first]{name=图}
\captionsetup[figure][bi-second]{name=Figure}
\captionsetup[table][bi-first]{name=表}
\captionsetup[table][bi-second]{name=Table}

三. 配置index.rmd文件

ps: 为了显示的需要 ``` 符号用 ---代替;
ps: 如果采取图片双标题方案二,则跳过这一步。

需要对knitr包的hooks进行一定的修改,在index.Rmd文件中加入:

---{r setup_knitrfigure, include = FALSE}
    library(knitr)
      if(is_latex_output()){
        knit_hooks$set(plot = function(x, options) {
          if(!is.null(options$bicap)){
            first_caption = options$fig.cap
            options$fig.cap = NULL
            paste("\\begin{figure}[!htp]",
                  hook_plot_tex(x, options), 
                  "\\bicaption{", first_caption, "}{",options$bicap,"}", 
                  "\\label{", "fig:", options$label, "}", 
                  "\\end{figure}", sep = "")
          }  else{
            hook_plot_tex(x, options)
          }
        })
      } 
---

四. 在01-introduction.rmd文件中演示相应设置

4.1 图片双标题
生成图片
---{r}
  dir.create('images')
  png('images/fig.png')
  plot(cars, main = 'png')
  dev.off()
  pdf('images/fig.pdf')
  plot(cars, main = 'pdf')
  dev.off()
---
插入图片

方案一:

---{r fig1, out.width='50%', fig.cap="中文题目", bicap='English caption', fig.align="center"}
  knitr::include_graphics("images/fig.png")
---

images文件夹中同时存在fig.pdf文件,生成pdf文档时,knitr会自动调用fig.pdf文件,如此生成高质量的pdf文件

(ref:fig2) 试试**复杂格式**的中文题目`cars` 

---{r fig2, out.width='50%', fig.cap='(ref:fig2)', bicap = "English caption2", fig.align="center"}
knitr::include_graphics("images/fig.png") 
---

方案二:

\begin{figure}
---{r, out.width='50%', fig.align="center", echo = FALSE}
  knitr::include_graphics("images/fig.png")
---
\bicaption{在不同调谐因子\textit{k}下的缺损文件可比较性实验结果}{Incompletele comparable probability in different factor k}
\label{fig:fig3}
\end{figure}
---{r fig3, echo = FALSE, out.width='50%', fig.align="center", fig.cap="在不同调谐因子k下的缺损文件可比较性实验结果"}
  library(knitr)
  if(is_html_output()) include_graphics("images/fig.png")
---
4.2 表格双标题
\begin{table}[!htbp]
    \centering
    \bicaption{表中文题目}{Table English caption}
    \label{tab:Tab1}
---{r, echo = FALSE}
  knitr::kable( head(iris), booktabs = TRUE)
---
\end{table}

---{r Tab1, echo = FALSE}
  library(knitr)
  if(is_html_output()) knitr::kable(head(iris), caption = "表中文题目", booktabs = TRUE)
---

大功告成。

主要参考:Latex图表设置中英文双标题(非ccaption宏包)How to use bicaption instead of caption?,感谢dapengde等的指点
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 3,917评论 0 5
  • 昨夜因你辗转反侧 长夜未眠 绞尽脑汁 终不能解 曾是萍水相逢 后也相依相伴 为你欢颜,为你充满力量 如今这是怎...
    蒲公英的Engagement阅读 291评论 0 1
  • 什么家长的“生存时刻”呢?请看下面的场景:为了给孩子喂奶整夜都无法睡觉,孩子的鞋子上全是泥还在家里跑来跑去,新外套...
    早起写作阅读 433评论 0 0
  • 为了放开,我走了好远的路 我遇到了一个半阴半晴的天气 这里没有鲜花,没有飞鸟,静得连风都没有 我为这世界而来,这世...
    缪斯girl_阅读 110评论 0 0