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

How to use bicaption instead of caption? #1554

Closed
bubifengyun opened this issue Jun 11, 2018 · 10 comments
Closed

How to use bicaption instead of caption? #1554

bubifengyun opened this issue Jun 11, 2018 · 10 comments
Milestone

Comments

@bubifengyun
Copy link

I had search the question, as https://stackoverflow.com/questions/44049547/is-rmarkdown-supporting-multiple-captions , no ans. I know latex, could be write as

\begin{figure}[!htp]
  \centering
  \includegraphics[width=4cm]{example/sjtulogo.png}
  \hspace{1cm}
  \includegraphics[width=4cm]{example/sjtulogo.jpg}
  \bicaption[这里将出现在插图索引中]
    {中文题图}
    {English caption}
  \label{fig:SRR}
\end{figure}

and R code

```{r logo, out.width='32.8%', fig.show='hold', fig.cap="中文", fig.align="center"}
knitr::include_graphics("figure/sjtulogo.pdf")
```

will be,

\begin{figure}

{\centering \includegraphics[width=0.328\linewidth]{figure/sjtulogo} 

}

\caption{中文}\label{fig:logo}
\end{figure}

has it any method to change caption to bicaption? Thank you.

@bubifengyun
Copy link
Author

Thank you, I have solve this problem,

in index.Rmd add this line

```{r setup, include=FALSE}

library(knitr)
knit_hooks$set(plot = function(x, options) {
  if(!is.null(options$bicap)){
    paste("\\begin{figure}[!htp]",
        hook_plot_tex(x, options), 
        "\\bicaption{", options$bicap[1], "}{",options$bicap[2],"}", 
        "\\end{figure}", sep = "")
  }
  else{
    hook_plot_tex(x, options)
  }
})

also, you must use bicaption package in your latex,

\RequirePackage[list=off]{bicaption}

then you can write in your Rmd file, like this.

```{r logo, out.width='32.8%', fig.show='hold', bicap=c('上海交通大学','SJTU'), fig.align="center"}
knitr::include_graphics("figure/sjtulogo.pdf")
```

ref: https://stackoverflow.com/questions/50235888/different-wrong-behaviour-of-knitr-hook-with-ggplot-compared-to-plot

@yihui yihui added this to the v1.21 milestone Jun 12, 2018
@xiaoyao177147
Copy link

\RequirePackage[list=off]{bicaption}后面应该加上两行
\captionsetup[figure][bi-first]{name=图}
\captionsetup[figure][bi-second]{name=Fig. }

@xiaoyao177147
Copy link

这种设置下,导出pdf完全正常,gitbook图片会无法显示;改进方案:在 index.Rmd 中 library(knitr)后面加上if(is_latex_output())条件语句。

@bubifengyun
Copy link
Author

bubifengyun commented Mar 31, 2019

这个建议不错。不知道这个功能有没有加入bookdown。对于gitbook,可以自己根据需要写

@xiaoyao177147
Copy link

这个建议不错。不知道这个功能有没有加入bookdown。对于gitbook,可以自己根据需要写

gitbook怎么弄,请指教。

另外,你的这个方案仅仅适用于图片,表格不可以,表格我研究半天目前尚未找到对策

@xiaoyao177147
Copy link

这个建议不错。不知道这个功能有没有加入bookdown。对于gitbook,可以自己根据需要写

找到表格双标题解决方案了:

\begin{table}[!htbp]
\centering
\bicaption{中文题目}{English caption}
\label{tab:Tab1}
---
{r , echo = FALSE}
knitr::kable( head(iris), booktabs = TRUE)
---
\end{table}

这样pdf中没问题,gitbook不识别latex,所以应该有更好的方案,

{r tab1}
library(knitr)  
  if(is_latex_output()) {
        eval(paste("\\begin{table}[!htp]",
              "\\bicaption{中文题目}{English caption}", 
              "kable(head(iris), booktabs = TRUE)",
              "\\end{table}", sep = "\n"))
      }  else{
        kable(head(iris), booktabs = TRUE, caption = "中文题目")
      }

gitbook正常,pdf失败,应该是代码哪写的不对,你比较懂latex,帮我看看

@xiaoyao177147
Copy link

关于bookdown图表中英文双标题,汇总了一下,详见R语言学习笔记--R bookdown图表设置中英文双标题测试文档

@bubifengyun
Copy link
Author

非常感谢,看你提交到赵鹏的那个 rbookdownplus了。我也顺便更新到我的 deepin-bible模板。确实不错。

@bubifengyun
Copy link
Author

@xiaoyao177147 仔细阅读了一下,感觉直接加在 bookdown 里面更好,我们这种只是临时方案,不能通用。有条件的可以到 bookdown 提交。

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants