There is, of course, a catch: everything between begin{...} and \end{...} is interpreted as raw LaTeX by Pandoc, so you can't use this technique to center the output of R code chunks, or Markdown content.
I used the answer from Jonathan to google inserting images into LaTeX and if you would like to insert an image named image1.jpg and have it centered, your code might look like this in Rmarkdown
I had the same question. I have tried all solutions provided above and none of them worked... But I have found a solution that works for me, and hopefully for others too.
This code will center both the image and the caption. It is essential that you leave lines between ABC0, the image code, and </center>, otherwise the image will be centered but the caption will disappear.
If you want your image to have a clickable link, you can embed things like
Same here, make sure there are empty lines in between each command ones. If you want both the image and the caption to be clickable, then combine the middle and the last codes above. I hope this helps a bit.
If you know your format is PDF, then I don't see how the HTML tag
can be useful... It definitely does not seem to work for me. The other pure LaTeX solutions obviously work just fine. But the whole point of Markdown is not to do LaTeX but to allow for multiple format compilation I believe, including HTML.
None of the answers work for all output types the same way and others focus on figures plottet within the code chunk and not external images.
The include_graphics() function provides an easy solution. The only argument is the name of the file (with the relative path if it's in a subfolder). By setting echo to FALSE and fig.align=center you get the wished result.
(Obviously, you can also directly type the content of the chunk into your .css file...).
The tex file includes the proper centering commands.
该方法的crucial优点是它允许在块内写入markdown代码。
In my previous answer, r ctrFmt("Centered **text** in html and pdf!") does not bold for the word "text", but it would if inside a fenced div.
For images, etc... the lua filter is available here
None of these solutions worked for me when inserting a pdf figure in the text. After intensive trial and error, what made the trick for me (for a pdf output) was:
Since the question asks for both text and image alignment and I had a hard time finding an option for text that worked, with non of the ones above working withing a code chunk (for me). I wanted to share this: it seem to do the trick to the best of it's abilities (meaning it's not perfectly centered but close)