I know I can set fig.align = 'center'
if I want to center my figure. But what if I want to center my output in a pdf document?

- 9,229
- 24
- 74
- 132
-
2do you want HTML or PDF output? (The answers will differ.) http://stackoverflow.com/questions/14051715/markdown-native-text-alignment/34859340#34859340 – Ben Bolker Nov 04 '16 at 02:14
-
PDF output. Thanks for clarifying. – Adrian Nov 04 '16 at 02:14
-
2does putting `\begin{center} ... \end{center}` around your code chunk work? (I don't know if it will, but it's worth a try.) – Ben Bolker Nov 04 '16 at 02:15
-
That didn't work for me – Adrian Nov 04 '16 at 02:17
-
Did you put chunk between begin and end ? In my env, `\begin{center}` text `\end{center}` works. – cuttlefish44 Nov 04 '16 at 07:27
3 Answers
The following code worked for me.
\center Centered Text \center
And, if you need to put it in bold, as my case, you can use the underline (two at the beggining, two at the end):
\center __Centered Text__ \center

- 300
- 5
- 9
-
For using within a chunk, I had to escape the `c` so used `\\center ` instead. – Nova Jul 13 '21 at 12:29
The option for centering a plot you generated by R code in Rmd file and that for an existing figure from a file elsewhere is different.
For the figures you generated in R, I think fig.align="center"
is sufficient, even if you want a PDF output.
For <center> <\center>
to work, you can only use an HTML output. If you still want PDF output, I think you can use include_graphics(your_img.png)
from knitr
package, and then you can use chunk options like fig.align="center"
. If you also want to change the size of this figure, please use something like out.width="50%"
, but not fig.width
.
For more details, I found this article really useful tips and tricks for working with images and figures in r markdown documents.

- 1
- 1

- 648
- 7
- 12