You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to display a figure wrapped in text. I want this figure to have a caption and be cross-referenced. I want the rmarkdown file to be knitted both to html and pdf.
After browsing SO and googling I have not find a complete answer to my question.
My attempt to proceed is as follows.
---title: 'Wrapped figures'date: "`r format(Sys.time(), '%d %B, %Y')`"output:
bookdown::html_document2:
df_print: pagedtoc: TRUEtoc_float: TRUEtoc_depth: 4code_download: TRUEcode_folding: hidefig_caption: yesnumber_sections: yesglobal_numbering: truebookdown::pdf_document2:
toc: no# toc_depth: 2fig_caption: yesnumber_sections: truekeep_tex: yeslatex_engine: lualatexlatex_engine: lualatexheader-includes:
- \usepackage{wrapfig}---```{r message=FALSE, warning=FALSE, echo=FALSE}
library(knitr)library(rmarkdown)```# Example
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
```{r wrapped-figure, fig.cap="A wrapped figure", echo=FALSE, out.width="65%", out.extra='style="float:right; padding:10px"', results='asis'}
# Define figure variablesfig_path <- "wikidata.png"fig_caption <- "A wrapped figure"fig_ref <- "wrapped-figure"# Output-specific logic for the figureif (knitr::is_latex_output()) { cat(sprintf("\\begin{wrapfigure}{R}{.5\\textwidth}\\begin{center}\\includegraphics[width=.5\\textwidth]{%s}\\caption{%s \\label{fig:%s}}\\end{center}\\end{wrapfigure}", fig_path, fig_caption, fig_ref))}if (knitr::is_html_output()) { knitr::include_graphics(fig_path)}```
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? See Figure \@ref(fig:wrapped-figure).
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
If the pdf knits as expected (the figure is wrapped in text, cross-referenced and caption is correctly placed) see screen shot :
This is not the case in the html output (here the figure is wrapped in text, cross-referenced but the caption is placed on the top and not even on top of the figure but rather on top of the neighboring text.) see screenshot.
I do not understand how to resolve this. Any help is welcome !
What is the issue
I would like to display a figure wrapped in text. I want this figure to have a caption and be cross-referenced. I want the rmarkdown file to be knitted both to html and pdf.
After browsing SO and googling I have not find a complete answer to my question.
My attempt to proceed is as follows.
If the pdf knits as expected (the figure is wrapped in text, cross-referenced and caption is correctly placed) see screen shot :
This is not the case in the html output (here the figure is wrapped in text, cross-referenced but the caption is placed on the top and not even on top of the figure but rather on top of the neighboring text.) see screenshot.
I do not understand how to resolve this. Any help is welcome !
Checklist
formatted your issue so it is easier for us to read?
included a minimal, self-contained, and reproducible example?
Code is pasted above. Picture used is
xfun::session_info('bookdown')
in your issue?Yes, through
update.packages(ask = FALSE, checkBuilt = TRUE)
remotes::install_github("rstudio/bookdown")
?No.
The text was updated successfully, but these errors were encountered: