# What is the cause of wrong aspect ratio and in-line writings in using sideways figure in Overleaf?

I am trying to use the answer provided in the following page (this link) to maximize the size of a figure in a landscape mode along with its caption. I am doing this in Overleaf where the compile is automatic and I have an aasjournal template. However, I am getting a squeezed figure with a different aspect ratio. All I want is to make use of most of a page without changing the aspect ratio of the figure. Also as it is obvious from the figure, there are writings in the back of the figure which is not wanted and should be separated from figure.

Here is a MWE:

\documentclass[twocolumn,trackchanges]{aastex61}

\usepackage{natbib}

\usepackage[]{geometry}

\usepackage{graphicx}
\usepackage{latexsym}
\usepackage{amssymb}

\usepackage{rotating}

\newlength\maximageheight
\newlength\maximagewidth
\newlength\currentimagewidth
\newsavebox\imagebox

\newlength\maxsidewaysheight
\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip-6\baselineskip}%

\newcommand{\includeimg}[2][]{%
\setlength\maximageheight{\dimexpr\textheight-\abovecaptionskip-1.5\baselineskip}%
\setlength{\maximagewidth}{\textwidth}%
\innerincludeimg{#1}{#2}%
}

\newcommand{\includesidewaysimg}[2][]{%
\setlength\maximagewidth{\textheight}%
\setlength\maximageheight{\maxsidewaysheight}%
\innerincludeimg{#1}{#2}%
}

\newcommand{\innerincludeimg}[2]{%
\sbox\imagebox{\includegraphics[#1,height=\maximageheight]{#2}}%
\settowidth{\currentimagewidth}{\usebox\imagebox}%
\ifdim\currentimagewidth>\maximagewidth
\includegraphics[#1,width=\maximagewidth]{#2}%
\else
\usebox\imagebox
\fi
}

\usepackage{amsmath}

\newcommand{\vdag}{(v)^\dagger}
\newcommand\aastex{AAS\TeX}
\newcommand\latex{La\TeX}

\begin{document}

\section{Results} \label{Section 3}

\subsection{2d Projected Matter Distribution} \label{Section 3.1}
Here are our maps:

\begin{sidewaysfigure}
\centering
\includesidewaysimg{m12b_low_low}
\caption{a very long (7 lines caption.}
\label{fig:Figure 1}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{m12c_low_low}
\caption{Still very long caption.}
\label{fig:Figure 3}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{m12f_low_low}
\caption{Still very long caption.}
\label{fig:Figure 5}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{m12m_low_low}
\caption{Still very long caption.}
\label{fig:Figure 7}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{m12q_low_low}
\caption{Still very long caption.}
\label{fig:Figure 9}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{m12b_local_low}
\caption{Still very long caption.}
\label{fig:Figure 2}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{m12c_local_low}
\caption{Still very long caption.}
\label{fig:Figure 4}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{m12f_local_low}
\caption{Still very long caption.}
\label{fig:Figure 6}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{m12m_local_low}
\caption{Still very long caption.}
\label{fig:Figure 8}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{m12q_local_low}
\caption{Still very long caption.}
\label{fig:Figure 10}
\end{sidewaysfigure}

\section*{Acknowledgments}
We would like to thank graduate students.

\bibliographystyle{aasjournal}
\bibliography{my_references}

\appendix
\section{Appendix 1}  \label{Appendix:Appendix A}

\end{document}

• This is probably independent from Overleaf and will happen on your system as well. There probably is an error in the code, which we don't know. Please provide a minimal working example. – Johannes_B Dec 18 '17 at 6:13
• You're specifying both a width and a height. This usually alters the aspect ratio (unless you've made the correct calculations). – Nicola Talbot Dec 18 '17 at 18:36
• @NicolaTalbot, I realized that. Thanks, but I am not aware of the true ratio a priori. Is there any way to fit it optimally in the page? – Ash Dec 18 '17 at 18:38
• I removed "height=1\textheight" and it seems to be working. But I don't know why text is appearing on the page. It's not supposed to. – Ash Dec 18 '17 at 18:44

Here's a method to maximize the height taken up by an image (without overflowing the page). The sidewaysfigure environment is a bit awkward as it changes the values of \textwidth and \linewidth, so the correct maximum height for sideways figures must be calculated outside.

\documentclass{article}

\usepackage[
showframe% debugging, remove in final document
]{geometry}
\usepackage{graphicx}
\usepackage{rotating}

\newlength\maximageheight
\newlength\maximagewidth
\newlength\currentimagewidth
\newsavebox\imagebox

\newlength\maxsidewaysheight
\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip-1.5\baselineskip}%

\newcommand{\includeimg}[2][]{%
\setlength\maximageheight{\dimexpr\textheight-\abovecaptionskip-1.5\baselineskip}%
\setlength{\maximagewidth}{\textwidth}%
\innerincludeimg{#1}{#2}%
}

\newcommand{\includesidewaysimg}[2][]{%
\setlength\maximagewidth{\textheight}%
\setlength\maximageheight{\maxsidewaysheight}%
\innerincludeimg{#1}{#2}%
}

\newcommand{\innerincludeimg}[2]{%
\sbox\imagebox{\includegraphics[#1,height=\maximageheight]{#2}}%
\settowidth{\currentimagewidth}{\usebox\imagebox}%
\ifdim\currentimagewidth>\maximagewidth
\includegraphics[#1,width=\maximagewidth]{#2}%
\else
\usebox\imagebox
\fi
}

\begin{document}

\section{this is main section} \label{Section 1}
\subsection{this is subsection} \label{Section 1.1}
There are some paragraphs in here.

\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}
\caption{This is some caption}
\label{fig:Figure 1}
\end{sidewaysfigure}

\begin{figure}
\centering
\includeimg{example-image-10x16}
\caption{This is another caption}
\end{figure}

\end{document}


The showframes option shows the page layout:

This assumes single-lined captions. If you have longer captions, you'll need to adjust the maximum settings. For example:

\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip-6\baselineskip}%


Here's an example with a long caption:

\documentclass{article}

\usepackage[
showframe% debugging, remove in final document
]{geometry}
\usepackage{graphicx}
\usepackage{rotating}
\usepackage{lipsum}% dummy text for testing

\newlength\maximageheight
\newlength\maximagewidth
\newlength\currentimagewidth
\newsavebox\imagebox

\newlength\maxsidewaysheight
\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip-1.5\baselineskip}%

\newcommand{\includeimg}[2][]{%
\setlength\maximageheight{\dimexpr\textheight-\abovecaptionskip-1.5\baselineskip}%
\setlength{\maximagewidth}{\textwidth}%
\innerincludeimg{#1}{#2}%
}

\newcommand{\includesidewaysimg}[2][]{%
\setlength\maximagewidth{\textheight}%
\setlength\maximageheight{\maxsidewaysheight}%
\innerincludeimg{#1}{#2}%
}

\newcommand{\innerincludeimg}[2]{%
\sbox\imagebox{\includegraphics[#1,height=\maximageheight]{#2}}%
\settowidth{\currentimagewidth}{\usebox\imagebox}%
\ifdim\currentimagewidth>\maximagewidth
\includegraphics[#1,width=\maximagewidth]{#2}%
\else
\usebox\imagebox
\fi
}

\begin{document}

\section{this is main section} \label{Section 1}
\subsection{this is subsection} \label{Section 1.1}
There are some paragraphs in here.

\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}
\caption{This is some caption}
\label{fig:Figure 1}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}
\caption{\protect\lipsum*[1]}% long dummy caption
\end{sidewaysfigure}

\begin{figure}
\centering
\includeimg{example-image-10x16}
\caption{This is another caption}
\end{figure}

\end{document}


This results in an overfull page:

There are two ways of dealing with this. The simplest is to introduce an extra argument that provides an adjustment:

\documentclass{article}

\usepackage[
showframe% debugging, remove in final document
]{geometry}
\usepackage{graphicx}
\usepackage{rotating}
\usepackage{lipsum}% dummy text for testing

\newlength\maximageheight
\newlength\maximagewidth
\newlength\currentimagewidth
\newsavebox\imagebox

\newlength\maxsidewaysheight
\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip}%

\newcommand{\includeimg}[3][]{%
\setlength\maximageheight{\dimexpr\textheight-\abovecaptionskip-#3}%
\setlength{\maximagewidth}{\textwidth}%
\innerincludeimg{#1}{#2}%
}

\newcommand{\includesidewaysimg}[3][]{%
{%
\setlength\maximagewidth{\textheight}%
\setlength\maximageheight{\dimexpr\maxsidewaysheight-#3}%
\innerincludeimg{#1}{#2}%
}%
}

\newcommand{\innerincludeimg}[2]{%
\sbox\imagebox{\includegraphics[#1,height=\maximageheight]{#2}}%
\settowidth{\currentimagewidth}{\usebox\imagebox}%
\ifdim\currentimagewidth>\maximagewidth
\includegraphics[#1,width=\maximagewidth]{#2}%
\else
\usebox\imagebox
\fi
}

\begin{document}

\section{this is main section} \label{Section 1}
\subsection{this is subsection} \label{Section 1.1}
There are some paragraphs in here.

\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}{1.5\baselineskip}
\caption{This is some caption}
\label{fig:Figure 1}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}{10.5\baselineskip}
\caption{\protect\lipsum*[1]}% long dummy caption
\end{sidewaysfigure}

\begin{figure}
\centering
\includeimg{example-image-10x16}{1.5\baselineskip}
\caption{This is another caption}
\end{figure}

\end{document}


This is a bit of a nuisance if you have a lot of figures. Another method is to supply the caption as the argument and measure it. This requires knowing the format used by \caption (without actually using \caption at this point). Since this can vary according to class and packages, I've just provided a simplistic command to simulate the format:¹

\newcommand{\captionformat}[1]{Figure \thefigure: #1}


You may need to adjust this to match the caption style used by your document (such as incorporating font changing commands). Here's the adjusted MWE:

\documentclass{article}

\usepackage[
showframe% debugging, remove in final document
]{geometry}
\usepackage{graphicx}
\usepackage{rotating}
\usepackage{lipsum}% dummy text for testing

\newlength\maximageheight
\newlength\maximagewidth
\newlength\currentimagewidth
\newlength\captionheight
\newsavebox\imagebox

\newlength\maxsidewaysheight
\setlength{\maxsidewaysheight}{\dimexpr\textwidth-\abovecaptionskip
-\baselineskip}%

\newcommand{\captionformat}[1]{Figure \thefigure: #1}% change as appropriate

\makeatletter
\newcommand{\measurecaptionheight}[1]{%
\setbox\@tempboxa\hbox{\parbox{\linewidth}{\captionformat{#1}}}%
\captionheight=\dimexpr\ht\@tempboxa+\dp\@tempboxa
\setbox\@tempboxa\box\voidb@x
}
\makeatother

\newcommand{\includeimg}[3][]{%
{%
\measurecaptionheight{#3}%
\setlength\maximageheight{\dimexpr\textheight-\abovecaptionskip
-\baselineskip-\captionheight}%
\setlength{\maximagewidth}{\textwidth}%
\innerincludeimg{#1}{#2}%
\caption{#3}%
}%
}

\newcommand{\includesidewaysimg}[3][]{%
{%
\measurecaptionheight{#3}%
\setlength\maximageheight{\dimexpr\maxsidewaysheight-\captionheight}%
\setlength\maximagewidth{\textheight}%
\innerincludeimg{#1}{#2}%
\caption{#3}%
}%
}

\newcommand{\innerincludeimg}[2]{%
\sbox\imagebox{\includegraphics[#1,height=\maximageheight]{#2}}%
\settowidth{\currentimagewidth}{\usebox\imagebox}%
\ifdim\currentimagewidth>\maximagewidth
\includegraphics[#1,width=\maximagewidth]{#2}%
\else
\usebox\imagebox
\fi
}

\begin{document}

\section{this is main section} \label{Section 1}
\subsection{this is subsection} \label{Section 1.1}
There are some paragraphs in here.

\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}{This is some caption}
\label{fig:Figure 1}
\end{sidewaysfigure}

\begin{sidewaysfigure}
\centering
\includesidewaysimg{example-image}{\protect\lipsum*[1]}% long dummy caption
\end{sidewaysfigure}

\begin{figure}
\centering
\includeimg{example-image-10x16}{This is another caption}
\end{figure}

\end{document}


¹The figure number won't be correct at this point as it doesn't get incremented until the actual \caption command (for example, in figure 10 this will slightly under-estimate the caption contents), but it's just a rough guide.

• @Thanks Nicola, do you know why the text is still appearing in-line with figure? What float should I used inside [] to avoid that? – Ash Dec 18 '17 at 19:08
• @Allan I can't reproduce the text appearing in the figure problem. I don't have your class, so maybe there's something in there that changes some settings or perhaps you have some overfull content (check the log file) or perhaps your image doesn't have the bounding box set correctly. – Nicola Talbot Dec 18 '17 at 19:49
• It seems I have to modify your code since it produces all overlapped figures without specifying a single page for each of my 10 figures. Do you know what should be the cause of it? – Ash Dec 19 '17 at 1:40
• @Allan It's difficult to tell without more information. Does it happen with a specific set of figures? (For example, do you get the same problem with the example figures supplied with the mwe package?) Does it only happen with a particular class or does it also happen with, for example, report? – Nicola Talbot Dec 19 '17 at 22:16
• It happens with all figures intended to span the whole page. It happens at least with aasjournal class which I want to work with. The figures appears clipped on the left-side of the page while there is plenty of room just below figure. – Ash Dec 20 '17 at 3:13