How to hold figure position with figure caption in pdf output of knitr?

后端 未结 7 1955
不知归路
不知归路 2020-11-29 18:45

I am using knitr (1.9.5 and 1.9.17) and rmarkdown (0.5.3.1), and would like to hold figure position in the pdf output. The generated pdf file is working fine when chunk opti

7条回答
  •  孤街浪徒
    2020-11-29 19:09

    As Yihui mentioned in his answer (Figure position in markdown when converting to PDF with knitr and pandoc), we cannot expect too much about formatting from mardown. To workaround this problem, just write some R scripts to replace htbp to H.

    Compared with knit from knitr package, I found render from rmarkdown is better to export a tex file. Just remember to add keep_tex: yes in the yaml header of your rmarkdown file.

    library(rmarkdown)
    render('filepath.Rmd')
    x <- readLines('filepath.tex')
    pos <- grep('begin\\{figure\\}\\[htbp\\]', x)
    x[pos] <- gsub('htbp', 'H', x[pos])
    writeLines(x, 'filepath.tex')
    tools::texi2pdf('filepath.tex', clean = TRUE)  # gives foo.pdf
    
    file.remove('filepath.tex')
    

提交回复
热议问题