# Centering the figure caption when switching page orientation

I'm a newcomer to Latex but I've already found it very useful. I have a document that requires me to switch the orientation from portrait to landscape midway through the document so I can properly fit an image. I searched quite a few posts and was able to achieve it by doing the following in RMarkdown

\newpage
\paperwidth=\pdfpageheight
\paperheight=\pdfpagewidth
\pdfpageheight=\paperheight
\pdfpagewidth=\paperwidth
\begingroup
\vsize=\textwidth
\hsize=\textheight


Once I flipped the orientation I could insert my code for the figure

{r, fig.pos = "H",fig.align='center', fig.cap="P3b Amplitude Fz (E15)", echo=FALSE, out.height='550px', out.width='600px', dpi=300}
knitr::include_graphics("images/P3bAmp_E15.png")



And then flip the page orientation back to portrait.

\endgroup

\newpage
\paperwidth=\pdfpageheight
\paperheight=\pdfpagewidth
\pdfpageheight=\paperheight
\pdfpagewidth=\paperwidth


I have 2 issues that I am currently running into. The first is that the caption will not center according to the width of a landscape page. Below are screenshots

Does anyone have any input on how this may be fixed?

Here is the header I have for the document in case it might be of use

---
title: "Data Report"
author: "My Name Here"
output:
pdf_document:
keep_tex: yes
toc: true
toc_depth: 3
- \usepackage{pdflscape}
- \usepackage{graphicx}
- \usepackage{lipsum}
- \usepackage{fancyhdr}
- \usepackage{float}
- \pagestyle{fancy}
- \fancyhf{}
{r global_options, include=FALSE}


• you would need to reset some more lengths but it is best not to use such low level setting, either use the rotating package and use sidewaysfigure instead of figure or use lscape package and use \begin{landscape}.\begin{figure}...\end[landscape} – David Carlisle Jun 22 '18 at 17:32