我是否真的必须指定两次包裹图形的宽度?

我是否真的必须指定两次包裹图形的宽度?

我正在尝试向文档添加一个图形。因为我希望文本环绕它,所以我使用环境wrapfigure

\begin{wrapfigure}{r}{1cm}
  \begin{center}
    \includegraphics[width=1cm]{"My figure"}
  \end{center}
    \caption{A caption of my figure}
\end{wrapfigure}

如您所见,我希望我的图形缩小(文件很大),因此我将宽度设置为 1 厘米。但现在我还必须告诉宽度为wrapfigure1 厘米。为什么不能wrapfigure自动与它包含的图形一样宽?

答案1

wrapfig实际上并不关心它包含什么。因此,图像和环境宽度之间没有任何联系wrapfigure。但是,你可以将宽度设置为,\linewidth使其填满其所在的容器:

在此处输入图片描述

\documentclass{article}
\usepackage{wrapfig,graphicx}
\begin{document}

\begin{wrapfigure}{r}{3cm}
  \includegraphics[width=\linewidth]{example-image}
  \caption{A caption of my figure}
\end{wrapfigure}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Maecenas luctus id tellus semper mattis. Integer quis neque 
sem. Etiam semper, tortor in lacinia feugiat, nisi mi cursus 
metus, et molestie felis nisl quis sapien. Fusce vel ligula 
eget nunc gravida malesuada ac sit amet risus. Duis et urna 
vestibulum, iaculis metus nec, placerat sem. Fusce suscipit 
quis velit mattis fermentum. Curabitur odio urna, tempor ac 
orci a, euismod elementum est. Mauris id mollis est, a consequat 
purus. Mauris quis risus non ipsum accumsan elementum ut in 
quam. Proin suscipit non augue dapibus tempus. Mauris 
scelerisque, turpis et adipiscing interdum, neque nunc 
tristique ipsum, a sollicitudin libero eros eget tellus. Donec 
fringilla cursus diam, eget malesuada erat dapibus in. Aliquam 
pellentesque vitae enim ut rutrum. In auctor nunc ac laoreet 
molestie. Proin scelerisque convallis tortor, sit amet egestas 
nulla accumsan ac. Donec urna justo, fringilla et vulputate eu, 
adipiscing sed augue.

\end{document}

尝试在序言中设置默认值:

\setkeys{Gin}{width = \linewidth}

答案2

或者,例如,定义一个自定义命令,将您的图形包装在必要的环境中。它需要 3 个参数和第四个可选参数。

\documentclass{article}
\usepackage{wrapfig,graphicx,kantlipsum}
\newcommand{\mywrapfigure}[4][r]{%
  \begin{wrapfigure}{#1}{#2}
    \centering
    \includegraphics[width=#2]{#3}
    \caption{#4}
  \end{wrapfigure}}

\begin{document}
\mywrapfigure{25mm}{example-image-a}{A caption of my figure}
\kant[1-2]

\mywrapfigure[l]{30mm}{example-image-b}{A caption of my other figure}
\kant[3]
\end{document}

\我的包装图

相关内容