[Tex/LaTex] Sideways figure and caption to fit textwidth and no page break

graphicsrotating

I'm struggling with placing some plots in latex. I'm trying to include the plots (sideways, but not the heading) in an appendix, so the first plot needs to be placed at the same page as the appendix heading.
Also the plots, including the caption need to be placed inside the top/bottom/left/right margins.
When using sidewaysfigure i get (1) a pagebreak, such that the heading and plot are placed at two different pages and (2) when the caption is long, it gets outside the margins.

Is there any way I can prevent the pagebreak?
and how to prevent the plot and caption to break the margins? (is there any way to measure the total height of plot and caption, and scale the plot (graphic) so that it has width=textheight (inside margins?), and height=textwidth(inside margins?)-"captionheight"….

my preamble

\documentclass[pdftex,10pt,b5paper,twoside]{report}
\usepackage[lmargin=25mm,rmargin=25mm,tmargin=27mm,bmargin=30mm]{geometry}

and

\begin{sidewaysfigure}
\captionsetup{width=0.75\textwidth}
\begin{center}
\includegraphics[keepaspectratio=true, width=\textwidth]{./Figur/fundvar.png}
\caption[Commercial crude oil inventories, SPX excess returns, S\&P GCSI excess return time series]{Commercial crude oil inventories calculated as the logarithm of the current value divided by the mean of same weekly values over the past 5 years, Log excess returns on the S\&P Goldman Sachs Commodity Index,  Log excess returns on the S\&P 500 index.}
\label{fig:fundvar}
\end{center}
\end{sidewaysfigure}

Best Answer

You could use the hvfloat package:

\documentclass[pdftex,10pt,b5paper,twoside]{report}
\usepackage[showframe,lmargin=25mm,rmargin=25mm,tmargin=27mm,bmargin=30mm]{geometry}
\usepackage[demo]{graphicx}
\usepackage{hvfloat}

\begin{document}

\appendix
\chapter{Test chapter}

\hvFloat[%
floatPos=htb,%
capVPos=c,%
rotAngle=90,
objectPos=c]{figure}{\includegraphics[width=353pt,height=290pt]{image}}%
[Commercial crude oil inventories, SPX excess returns, S\&P GCSI excess return time series]{Commercial crude oil inventories calculated as the logarithm of the current value divided by the mean of same weekly values over the past 5 years, Log excess returns on the S\&P Goldman Sachs Commodity Index,  Log excess returns on the S\&P 500 index.}{fig:test}

\end{document}

The demo option for graphicx was used to make my example compilable for everyone; do not use the option in your actual code.