0

I am using knitR to import my image into my rmarkdown and I wrote captions using fig.cap. My image is already centered but my caption is always left-aligned. Is there anyway to also align my caption under my image?

```{r, echo=FALSE, fig.align='center', fig.cap="tmap with multiple data classification methods and classes"}
knitr::include_graphics("tmap01.png")

knitr image

camille
  • 16,432
  • 18
  • 38
  • 60
  • What type of document are you rendering to? How you can style a document depends in part on that e.g. if you're rendering to HTML, you can set up CSS styling for stuff like this pretty easily. If it's Word, set a reference doc. – camille Nov 24 '22 at 06:09
  • @camille I am rendering to a HTML. I am thinking without adjusting the CSS styling, from rmarkdown are there any options to do it – Rshiny Learner Nov 24 '22 at 06:12
  • There's a bunch of ideas [here](https://stackoverflow.com/q/3912694/5325862) that are markdown in general, not rmarkdown specifically – camille Nov 24 '22 at 06:28
  • You could also have a glance at [`quarto`](https://quarto.org/docs/authoring/figures.html#caption-locations). – Julian Nov 24 '22 at 10:44

1 Answers1

0

Inspecting the document shows that the caption is in a p tag with class caption, and there's no CSS associated to this class. So you can set some CSS properties to the caption class, by adding this CSS chunk (e.g. at the beginning of the document):

```{css}
.caption {
  margin: auto;
  text-align: center;
}
```
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225