# Fit sidewaysfigure to page width including caption and source

I'm trying to add a sidewaysfigure to my document, but when I set the width to \textwidth it comes out too wide.

This is my code:

\usepackage{rotating}

\begin{sidewaysfigure}
\centering
\begin{minipage}{\textwidth}
\caption{The caption}
\includegraphics[width=\textwidth]{img/diagramm_hs.png}\\
\source{The source}
\label{fig:diagramm_hs}
\end{minipage}
\end{sidewaysfigure}


And here is the result:

As you can see, the image is wider than the textwidth (textwidth is as wide as the line in the header), especially with the caption and source.

How can I make the image as large as possible on the page while everything, including caption and source, remain in the textarea?

EDIT: The image dimensions are 1327x960 px. This is my documentclass:

\documentclass[
paper=A4,
fontsize=12pt,
BCOR12mm,
DIV14,
parskip=half*,
listof=totoc,
footinclude=false,
final
]{scrartcl}

• Have you tried \includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{img/diagramm_hs.png}? – Mico Oct 12 '15 at 8:08
• Yes, that didn't change anything. – user3249829 Oct 12 '15 at 8:49
• In this case, with caption and source, maybe you can set the image-width manually, like \includegraphics[width=\dimexpr\textwidth-80pt]. Otherwise it would be nice to know what the code for the header line looks like – musicman Oct 12 '15 at 11:56
• Which document class do you use, what's the page size (A4? Letter?), and what are the dimensions of the textblock? – Mico Oct 12 '15 at 19:47
• I added some more information to the question, hope that helps. – user3249829 Oct 13 '15 at 9:18

First, the original \textwidth is lost using sidewaysfigure. Second, I needed to test if the total figure was too big. Finally, I needed the image height using [width=\textwidth] in order to determine what [height=] should be to compensate.

I tested \listoffigures to make sure only one caption showed up.

\documentclass{article}
\usepackage{showframe}
\usepackage{rotating}
\usepackage{caption}
\captionsetup{justification=raggedright,
singlelinecheck=false}

\newcommand{\source}[1]{\ttfamily #1}

\newsavebox{\tempbox}
\newlength{\tempdima}
\newlength{\tempdimb}
\newlength{\tempdimc}

\begin{document}
\tempdima=\textwidth% inside \textwidth=\linewidth=\textheight
\begin{sidewaysfigure}
\textheight=\tempdima
\settoheight{\tempdimb}{\includegraphics[width=\textwidth]{example-image-a}}% default height
\savebox{\tempbox}{\begin{minipage}{\textwidth}
\caption{The caption}
\rule{\textwidth}{\tempdimb}% same size but faster
\par\source{The source}
\end{minipage}}%
\tempdima=\ht\tempbox\relax% can't get \dimexpr to work
\tempdimc=\tempdimb
\ifdim\textheight<\tempdima
\fi
\tempdima=\dimexpr \tempdimb*\textwidth/\tempdimc\relax% compute scaled width
\hfil\begin{minipage}{\tempdima}
\caption{The caption}
\includegraphics[width=\textwidth]{example-image-a}\par
\source{The source}
\end{minipage}
\end{sidewaysfigure}
\end{document}


• Thanks! This seems to be working (although I don't quite understand how ;) ). One question: I want the caption and source to be left aligned with the left side of the image (as seen in the screenshot in my original question). So I'd like to wrap caption, figure and source in a minipage. Can you tell me how to set the dimensions of the minipage and the includegraphics in this case? – user3249829 Oct 13 '15 at 9:03
• Aligning the source with the left side of the page is easy; just stop centering. Aligning the source with the left side of the figure (which is what I assume you want) means computing a scaled down minipage. Unfortunately, \caption ALWAYS centers itself in the available space. One would basically have to replace \@makecaption, which can be done but will be ugly. – John Kormylo Oct 13 '15 at 13:22
• I missed the singlelinecheck option from the caption package and will steal it from @Micro shamelessly. – John Kormylo Oct 13 '15 at 14:03
• Let me rephrase: I wrapped the whole thing in a minipage. That minipage is now at the top of the page. I'd like to know how I can center it vertically. Meanwhile I'll accept your answer since it solves the original problem. Thanks! – user3249829 Oct 14 '15 at 7:26

As far as I can tell, you haven't provided any information yet about the natural size of the image (or of its natural height to width ratio), the dimensions of the textblock, or even of the document class you use. It's thus not possible to give very specific advice.

All I can think of suggesting is to set the image's width to \textwidth and its height to something like 0.7\textheight; see the screenshot below. Depending on the natural size of the image, you will probably have to adjust the factor 0.7; it may also be useful to set the option keepaspectratio.

\documentclass{report}
\usepackage{caption}
\captionsetup{justification=raggedright,
singlelinecheck=false}
\usepackage{rotating}
\usepackage[showframe]{geometry}
\begin{document}

\begin{sidewaysfigure}
\caption{The caption}
\label{fig:diagramm_hs}
\includegraphics[width=\textwidth,
height=0.7\textheight,
%keepaspectratio  % may need to uncomment this option
]{example-image-a}

Quelle: The source
\end{sidewaysfigure}
\end{document}


Here is a code that works for me (not perfect but I can work with it):

\documentclass[a4paper,twoside]{article}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{rotating}
\usepackage[showframe ,lmargin=1.0783in,rmargin=1.3in,top=2.8cm,bottom=4.15cm]{geometry}
\usepackage{fancyhdr}
\usepackage{hyperref}

\usepackage{lipsum}

%For caption
\def\abcapskip{3.5mm}
\def\belcapskip{4mm}
\usepackage[aboveskip=\abcapskip,belowskip=\belcapskip]{caption}
\renewcommand{\captionfont}{\rmfamily}
\renewcommand{\captionlabelfont}{\bfseries\sffamily}
\captionsetup{labelsep=colon}
\usepackage{amsmath}
\numberwithin{figure}{section}
\renewcommand{\thesection}{\Roman{section}}
\renewcommand{\thefigure}{\thesection.\arabic{figure}}
\newcounter{nextfigure}
\newcommand*{\PhantomTheFigure}{\setcounter{nextfigure}{\value{figure}}\stepcounter{nextfigure}\Roman{section}.\roman{nextfigure}}
%For sidewaysfigure's height
\newsavebox{\capbox}
\newsavebox{\textbox}
\newlength{\swheight}
\newlength{\mywidth}
\newlength{\myheight}
\setlength{\mywidth}{\textwidth}
\setlength{\myheight}{\textheight}
\newcommand{\makeswcap}{\ifx\FArg\empty\captionof{figure}{\SArg}\else\captionof{figure}[\FArg]{\SArg}\fi\TArg}
\newcommand{\makeswtext}{\ifx\FArg\empty\relax\else\par\usebox{\textbox}\par\fi}

\begin{document}
\setlength{\mywidth}{\textwidth}
\setlength{\myheight}{\textheight}
\pagestyle{fancy}
\section{First example with caption below}
\lipsum[1-2]

{\color{red}Here comes the Sidewaysfigure~\ref{fig:A1} with the caption below the figure:}

\begin{sidewaysfigure}
\swcaption[This is my short caption]{This is my extended caption that will use more than one row and its height will be automatically measured to be used for figure height adjustment}{\label{fig:A1}}{}
\includegraphics[height=\swheight]{example-image-a}
\makeswcap
\end{sidewaysfigure}

\clearpage
\subsection{Second example with caption below\ldots Plus text above}
\lipsum[1-2]

{\color{red}Here comes the Sidewaysfigure~\ref{fig:A2} with the caption below the figure and some text above:}

\begin{sidewaysfigure}
\swcaption[This is my short caption]{This is my extended caption that will use more than one row and its height will be automatically measured to be used for figure height adjustment}{\label{fig:A2}}{Here is a text that will be added in this last sideways figure below the caption and the figure. Here is a text that will be added in this last sideways figure below the caption and the figure.}
\makeswtext
\includegraphics[height=\swheight]{example-image-b}
\makeswcap
\end{sidewaysfigure}

\clearpage
\section{Second example with caption above}
\lipsum[1-2]

{\color{red}Here comes the Sidewaysfigure~\ref{fig:B1} with the caption above the figure:}

\begin{sidewaysfigure}
\swcaption[This is my short caption]{This is my extended caption that will use more than one row and its height will be automatically measured to be used for figure height adjustment}{\label{fig:B1}}{}
\makeswcap
\includegraphics[height=\swheight]{example-image-b}
\end{sidewaysfigure}
\clearpage

\subsection{Second example with caption above\ldots Plus text}
\lipsum[1-2]

{\color{red}Here comes the Sidewaysfigure~\ref{fig:B2} with the caption above the figure and some text below}

\begin{sidewaysfigure}
\swcaption[This is my short caption]{This is my extended caption that will use more than one row and its height will be automatically measured to be used for figure height adjustment}{\label{fig:B2}}{\vspace*{20pt}Here is a text that will be added in this last sideways figure below the caption and the figure}
\makeswcap
\includegraphics[height=\swheight]{example-image-b}
\makeswtext
\end{sidewaysfigure}

\end{document}