我仅使用以下方法将图片包含到我的文档中:
\begin{center}
\includegraphics{...}
\end{center}
但是,我想要带有captions
包装的标题和正确的超链接锚点,所以我最终得到了如下结果:
\begin{center}
\captionsetup{type=figure}
\includegraphics{...}
\caption{asdf}
\label{fig1}
\end{center}
现在的问题是,如果包含图像,这似乎会产生垂直空间在页面顶部。否则,一切正常。使用平均能量损失下面看看我的意思。
MWE 也展示了两种解决方法,但我对它们都不满意。
我怎样才能做到这一点?
\documentclass{scrartcl}
\usepackage{mwe}
\usepackage[hypcap=true]{caption}
\usepackage[colorlinks=true]{hyperref}
\usepackage[showframe]{geometry}
\begin{document}
\listoffigures
\newpage
\begin{center}%
\captionsetup{type=figure}%
\includegraphics[width=.25\linewidth]{example-image}
\caption{asdf}
\label{fig1}
\end{center}
Why do I get vertical space above figure \ref{fig1}
\begin{center}%
\captionsetup{type=figure}%
\includegraphics[width=.25\linewidth]{example-image}
\caption{asdf}
\label{fig2}
\end{center}
Looks good here for \ref{fig2}. So must have something to do with top of page.
\newpage
\begin{center}
\captionsetup{type=figure}
\vspace{-3.5mm}
\includegraphics[width=.25\linewidth]{example-image}
\caption{asdf}
\label{fig3}
\end{center}
A workaround with some vspace for figure \ref{fig3}, however, the link anchor is now not optimal.
\newpage
\noindent
\begin{minipage}{\linewidth}
\centering\captionsetup{type=figure}
\includegraphics[width=.25\linewidth]{example-image}
\caption{asdf}
\label{fig4}
\end{minipage}
Using a minipage works as described in docs:
\url{https://mirror.clientvps.com/CTAN/macros/latex/contrib/caption/caption.pdf}
But now the spacing between caption and the text is too tight.
\newpage
\noindent
\begin{minipage}{\linewidth}
\centering\captionsetup{type=figure}
\includegraphics[width=.25\linewidth]{example-image}
\caption{asdf}
\label{fig5}
\end{minipage}
\vskip\baselineskip
\noindent So another workaround...
\end{document}
答案1
Minipage 是必需的,但它是一个相当低级的环境,必须明确添加间距。这可以使用 vspace 命令来完成,也可以使用 center 或使用 float 来完成,它也可以处理间距
\documentclass{scrartcl}
\usepackage{mwe}
\usepackage[hypcap=true]{caption}
\usepackage[colorlinks=true]{hyperref}
\usepackage[showframe]{geometry}
\usepackage{float}
\begin{document}
\listoffigures
\newpage
\vspace*{35\baselineskip}
\begin{center}%
\captionsetup{type=figure}%
\includegraphics[width=.25\linewidth]{example-image}
\caption{asdf}
\label{fig1}
\end{center}
You need a minipage to avoid that caption and picture gets separated, see \ref{fig1}
\newpage
\noindent
\begin{minipage}{\linewidth}
\centering\captionsetup{type=figure}
\includegraphics[width=.25\linewidth]{example-image}
\caption{asdf}
\label{fig2}
\end{minipage}
But now the spacing between caption and the text is too tight.
\newpage
\begin{center}%
\noindent
\begin{minipage}{\linewidth}
\centering\captionsetup{type=figure}
\includegraphics[width=.25\linewidth]{example-image}
\caption{asdf}
\label{fig3}
\end{minipage}
\end{center}
Minipage is a rather low-level environment, spacing must be added explicitly. This can be done with vspace commands, but also e.g. with
center or use float which handles also the spacing, see \ref{fig4}
\newpage
\begin{figure}[H]
\centering
\includegraphics[width=.25\linewidth]{example-image}
\caption{asdd}
\label{fig4}
\end{figure}
with some text behind
\end{document}