在 Overleaf 中使用横排图形时,出现宽高比错误和内联文字的原因是什么?

在 Overleaf 中使用横排图形时,出现宽高比错误和内联文字的原因是什么?

我正在尝试使用以下页面提供的答案(此链接)以最大化横向模式下的图形及其标题的大小。我正在这样做背页编译是自动的,我有一个aasjournal模板。但是,我得到的是一个具有不同纵横比的压缩图。我只想在不改变图形纵横比的情况下充分利用页面的大部分空间。另外,从图中可以明显看出,图的背面有一些不需要的文字,应该与图分开。

以下是 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}

答案1

这是一种最大化图像所占高度的方法(不会溢出页面)。环境有点尴尬,因为它会改变和sidewaysfigure的值,因此必须在外部计算横向图形的正确最大高度。\textwidth\linewidth

\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}

showframes选项显示页面布局:

第 1 页图片

第 2 页图片

这假设字幕为单行。如果您的字幕较长,则需要调整最大设置。例如:

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

以下是带有长标题的示例:

\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}

这会导致页面溢出:

带有溢出标题的页面图像

有两种方法可以解决这个问题。最简单的方法是引入一个提供调整的额外参数:

\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}

如果您有很多图形,这会有点麻烦。另一种方法是提供标题作为参数并测量它。这需要知道使用的格式\caption(此时不实际使用\caption)。由于这可能会因类和包而异,我仅提供了一个简单的命令来模拟格式:¹

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

您可能需要调整它以匹配文档使用的标题样式(例如合并字体更改命令)。以下是调整后的 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}

带长标题的图形图像(现在适合)


¹此时图形编号不正确,因为它直到实际\caption命令才增加(例如,在图 10 中,这将稍微低估标题内容),但这只是一个粗略的指南。

相关内容