我正在尝试将两张图片包裹在一个段落中,如下面两张图片所示。两个红框显示了两幅图所需的位置。
不幸的是,我无法让以下代码实现我想要的效果。
\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{wrapfigure}{R}{3cm}
\centering
\rule{3cm}{3cm}
\end{wrapfigure}
\begin{wrapfigure}{L}{3cm}
\centering
\rule{3cm}{3cm}
\end{wrapfigure}
\lipsum[1-6]
\end{document}
我怎样才能准确指定 s 的位置wrapfigure
?
答案1
以下是使用 的手动操作\parshape
。由于您没有提供要包含的图像的实际测量值,因此您的设置很可能略有不同。但是,用法非常相似:
\documentclass{article}
\usepackage{graphicx}
% https://tex.stackexchange.com/a/133660/5764
\makeatletter
\def\newparshape{\parshape\@npshape0{}}
\def\@npshape#1#2#3{\ifx\\#3\expandafter\@@@npshape\else\expandafter\@@npshape\fi
{#1}{#2}{#3}}
\def\@@npshape#1#2#3#4#5{%
\ifnum#3>\z@\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\expandafter\@@npshape\expandafter{\the\numexpr#1+1\relax}{#2 #4 #5}{\numexpr#3-1\relax}{#4}{#5}}%
{\@npshape{#1}{#2}}}
\def\@@@npshape#1#2#3{#1 #2 }
\makeatother
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae mauris risus. Sed
varius dui diam, sit amet volutpat risus euismod in. Ut ac dui fermentum, laoreet arcu
volutpat, vestibulum urna. Integer vitae odio at neque imperdiet sollicitudin vel vel enim.
Nullam sodales facilisis magna sit amet tempor. Morbi ut nulla sem. Aliquam sagittis,
lacus auctor tincidunt semper, odio dui lacinia lectus, quis tempus quam leo sed mi.
Maecenas eu mauris dolor.
% Placement of top-left image
\newparshape
{5}{\dimexpr3cm+\tabcolsep}{\dimexpr\textwidth-3cm-\tabcolsep}% Unique to top-left image
{3}{0pt}{\textwidth}% full-width lines where no image is present
{6}{0pt}{\dimexpr\textwidth-3cm-\tabcolsep}\\% Unique to bottom-right image
\noindent\leavevmode
\llap{%
\raisebox{\dimexpr-\height+\ht\strutbox}[0pt][0pt]{%
\includegraphics[width=3cm,height=4.5\baselineskip]{example-image-a}}%
\hspace*{\tabcolsep}}%
Pellentesque porta pharetra leo, ut accumsan diam feugiat sit amet. Ut mattis nulla sed
sapien suscipit, eu sodales elit cursus. Vestibulum sit amet sollicitudin nisl. Aenean
vestibulum purus sed tincidunt convallis. Aliquam ut sodales risus. Mauris in mauris a
ante venenatis faucibus. Ut nunc ante, cursus sit amet iaculis eget, imperdiet quis libero.
Pellentesque laoreet dolor eget consectetur vehicula. Maecenas ultrices vehicula ultricies.
Morbi cursus risus ut eros placerat condimentum. Mauris nec varius lectus. Nulla adipiscing
porttitor tellus vitae congue. Aliquam commodo gravida erat, lacinia vehicula enim mollis
nec. Nulla sed metus gravida, faucibus neque eu, rutrum purus. Integer ornare nunc vel
dolor lacinia, sit amet venenatis turpis porttitor. Fusce lobortis magna eget odio iaculis,
sed blandit urna dictum.%
% Placement of bottom-right image
\hfill\rlap{\hspace*{\tabcolsep}%
\raisebox{\dimexpr-\height+5\baselineskip+\ht\strutbox}[0pt][0pt]{%
\includegraphics[width=3cm,height=4.5\baselineskip]{example-image-b}}}
Morbi id dolor vulputate, euismod sapien nec, condimentum tellus. Fusce tincidunt sem non
turpis ultricies, eu egestas lorem posuere. Integer risus lectus, gravida vel tincidunt sit
amet, vestibulum eget leo. Curabitur vitae enim sem. Vivamus rutrum velit eget justo
condimentum egestas. Phasellus sit amet lacus mauris. Sed quis aliquet tellus. In mauris
ante, porttitor sit amet rhoncus et, ultrices quis elit. Proin sed aliquet eros.
\end{document}
我们首先使用 设置要更改形状的段落的布局\newparshape{<num>}{<indent>}{<width>}...{<num>}{<indent>}{<width>}\\
。因此,使用上面的代码,我们设置了 3 个不同的行定义:
{5}
行的缩进量为3cm+\tabcolsep
(以避免覆盖实际图像),宽度为(减去图像宽度和图像与文本之间的间隙\textwidth-3cm-\tabcolsep
的余数);\textwidth
{3}
没有缩进 (0pt
) 和全宽 (\textwidth
) 的行;以及{6}
没有缩进 (0pt
) 和宽度的行\textwidth-3cm-\tabcolsep
。
请记住,我们必须设置段落形状前段落开始。
左上角的图像放置在段落之前,而右下角的图像放置在段落末尾。两个图像都\raisebox{<height>}
根据段落上的数字放置/提升(使用)到位
- 它们占据的行数,
- 他们的身高,
- 的高度
\strutbox
。
本质上,它们的定位方式是它们都具有零高度/深度(由 的[0pt][0pt]
参数产生\raisebox
)。
\tabcolsep
是图像和文本之间的水平间隙。
\newparshape
取自更精简的版本\parshape
。
我已经添加了\noindent
,但是是否使用它可能取决于您。
下面是一个不同的设置,其中两个图像在同一段落内共享一些常见的缩短行:
\documentclass{article}
\usepackage{graphicx}
% https://tex.stackexchange.com/a/133660/5764
\makeatletter
\def\newparshape{\parshape\@npshape0{}}
\def\@npshape#1#2#3{\ifx\\#3\expandafter\@@@npshape\else\expandafter\@@npshape\fi
{#1}{#2}{#3}}
\def\@@npshape#1#2#3#4#5{%
\ifnum#3>\z@\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\expandafter\@@npshape\expandafter{\the\numexpr#1+1\relax}{#2 #4 #5}{\numexpr#3-1\relax}{#4}{#5}}%
{\@npshape{#1}{#2}}}
\def\@@@npshape#1#2#3{#1 #2 }
\makeatother
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae mauris risus. Sed
varius dui diam, sit amet volutpat risus euismod in. Ut ac dui fermentum, laoreet arcu
volutpat, vestibulum urna. Integer vitae odio at neque imperdiet sollicitudin vel vel enim.
Nullam sodales facilisis magna sit amet tempor. Morbi ut nulla sem. Aliquam sagittis,
lacus auctor tincidunt semper, odio dui lacinia lectus, quis tempus quam leo sed mi.
Maecenas eu mauris dolor.
\newparshape
{7}{\dimexpr3cm+\tabcolsep}{\dimexpr\textwidth-3cm-\tabcolsep}% Unique to top-left image
{2}{\dimexpr3cm+\tabcolsep}{\dimexpr\textwidth-6cm-2\tabcolsep}% Between top-left and bottom-right image
{6}{0pt}{\dimexpr\textwidth-3cm-\tabcolsep}\\% Unique to bottom-right image
\noindent\leavevmode
\llap{%
\raisebox{\dimexpr-\height+\ht\strutbox}[0pt][0pt]{%
\includegraphics[width=3cm,height=9\baselineskip]{example-image-a}}%
\hspace*{\tabcolsep}}%
Pellentesque porta pharetra leo, ut accumsan diam feugiat sit amet. Ut mattis nulla sed
sapien suscipit, eu sodales elit cursus. Vestibulum sit amet sollicitudin nisl. Aenean
vestibulum purus sed tincidunt convallis. Aliquam ut sodales risus. Mauris in mauris a
ante venenatis faucibus. Ut nunc ante, cursus sit amet iaculis eget, imperdiet quis libero.
Pellentesque laoreet dolor eget consectetur vehicula. Maecenas ultrices vehicula ultricies.
Morbi cursus risus ut eros placerat condimentum. Mauris nec varius lectus. Nulla adipiscing
porttitor tellus vitae congue. Aliquam commodo gravida erat, lacinia vehicula enim mollis
nec. Nulla sed metus gravida, faucibus neque eu, rutrum purus. Integer ornare nunc vel
dolor lacinia, sit amet venenatis turpis porttitor. Fusce lobortis magna eget odio iaculis,
sed blandit urna dictum.
\hfill\rlap{\hspace*{\tabcolsep}%
\raisebox{\dimexpr-\height+8\baselineskip+\ht\strutbox}[0pt][0pt]{%
\includegraphics[width=3cm,height=9\baselineskip]{example-image-b}}}
Morbi id dolor vulputate, euismod sapien nec, condimentum tellus. Fusce tincidunt sem non
turpis ultricies, eu egestas lorem posuere. Integer risus lectus, gravida vel tincidunt sit
amet, vestibulum eget leo. Curabitur vitae enim sem. Vivamus rutrum velit eget justo
condimentum egestas. Phasellus sit amet lacus mauris. Sed quis aliquet tellus. In mauris
ante, porttitor sit amet rhoncus et, ultrices quis elit. Proin sed aliquet eros.
\end{document}