我喜欢在段落内放置浮动,这样文本就会环绕浮动(而不是左右环绕)。写完后,我会微调源文件中浮动前后的行,以我满意的方式排列浮动周围的行。这很麻烦,所以我正在寻找一种自动的方法来做到这一点。要么平衡浮动周围的行(例如,浮动前 5 行,浮动后 5 行),要么指定浮动前后的行数。
下面是一个 MWE,展示了我想要做的事情:
\documentclass{article}
\usepackage[demo]{graphicx}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Viverra adipiscing at in tellus integer feugiat scelerisque varius morbi.
\begin{figure}[htb]
\centering
\includegraphics[width = 0.8\textwidth]{foobar}
\end{figure}
Lacinia quis vel eros donec ac.
Tristique et egestas quis ipsum suspendisse ultrices gravida.
Integer enim neque volutpat ac tincidunt vitae.
Pellentesque massa placerat duis ultricies lacus sed turpis tincidunt id.
At ultrices mi tempus imperdiet nulla malesuada.
Nunc mattis enim ut tellus elementum sagittis vitae et.
Euismod quis viverra nibh cras pulvinar.
\end{document}
我也可以像这样格式化它(我只移动到图形的定义下面一点):
\documentclass{article}
\usepackage[demo]{graphicx}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Viverra adipiscing at in tellus integer feugiat scelerisque varius morbi.
Lacinia quis vel eros donec ac.
Tristique et egestas quis ipsum suspendisse ultrices gravida.
Integer enim neque volutpat ac tincidunt vitae.
Pellentesque massa placerat duis ultricies lacus sed turpis tincidunt id.
\begin{figure}[htb]
\centering
\includegraphics[width = 0.8\textwidth]{foobar}
\end{figure}
At ultrices mi tempus imperdiet nulla malesuada.
Nunc mattis enim ut tellus elementum sagittis vitae et.
Euismod quis viverra nibh cras pulvinar.
\end{document}
TL;DR:如何微调浮动在段落中的准确位置?
答案1
将浮动元素放置在页面中间的唯一方法是使用,h
这意味着浮动元素会准确地出现在源文件中的位置(或者更确切地说,在段落分成几行之后,浮动元素会位于包含该点的行之后)。LaTex 永远不会将页面中间的浮动元素浮动在页面内。
如果浮动元素是浮动的,那么它将始终使用以下方式之一进行定位:tbp
位于页面的顶部或底部,或者位于只有浮动元素而没有文本的浮动页面上。
答案2
正如@DavidCarlisle 和本网站其他许多人所说,最好摆脱漂浮的 figure
环境及其h
选项,如果您确实想要绝对定位。后者并不真正推荐... 然后,一种常见的策略是使用minipage
,结合caption
允许将 置于\caption
非浮动环境中的包。
在此答案中,我解决了您的具体要求“指定行数“
与我在评论中所说的相反,该cutwin
包实际上并不适合实现您在图片中建议的结果。事实上,它旨在将文本环绕在“窗口”周围,如果有人要求两侧边距为零,它会坚持在两侧放置一些文本,这会溢出边距和图形内容。
insbox.tex
因此,在下面的 MWE 中,我使用能够执行您要求的操作的包,只是上面的行数不是固定为一个数字(就像我使用的那样cutwin
),而是由您调用命令的位置定义\InsertBoxC
,在某种意义上更接近您在初始代码中建议的策略。
为了完整起见,我保留了一个cutwin
使用示例,如问题所示使用 cutwin 定制通用 wrapfig,可以用来克服广泛使用的wrapfig
封装的一些限制。
以下是 MWE(文本来自的文档cutwin
):
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=25mm,verbose=false]{geometry}
\usepackage{graphicx}
\usepackage{caption} % if you want a caption
\usepackage{cutwin}
\input{insbox.tex}
%%-------------------
% what follows is for demonstration purpose only
% and must be suppressed in real use.
\usepackage{xcolor}
\usepackage{showframe}
\renewcommand*\ShowFrameColor{\color{red}}
\setlength{\fboxsep}{0pt}
\renewcommand{\fbox}[1]{\fcolorbox{red}{white}{#1}}
\usepackage{mwe}
%%-------------------
\begin{document}
\section {With \texttt{cutwin} package}
\vspace*{2\baselineskip} %% skip needed as cutwin performs a vertical shift
\opencutcenter
\renewcommand{\windowpagestuff}{\fbox{\begin{minipage}{\linewidth}%
\centering\includegraphics[width=0.75\linewidth]{example-image-golden}%
\captionof{figure}{Example figure}\par\end{minipage}}}
\begin{cutout}{3}{0.3\textwidth}{0.3\textwidth}{8}
A rectangular space can be placed in a paragraph with the text flowing across
the gap. The space may break open into the top or side of the paragraph or,
with some care, into the bottom (the number of lines specified
for the cutout should not exceed the amount of text available for those lines).
Some text or a logo or graphic may be placed within the window, or it may be
left empty. In this instance I have put three short bold text lines in the
window opening. The window should not be too wide as it can be difficult to
track the exterior text lines across the gap. A rectangular space can be placed
in a paragraph with the text flowing across the gap. The space may break open
into the top or side of the paragraph or, with some care, into the bottom.
\end{cutout}
\section {With \texttt{inbox.tex}}
\newsavebox{\inscontent}
\savebox{\inscontent}{\fbox{\begin{minipage}{\textwidth}%
\centering\includegraphics[width=0.30\linewidth]{example-image-golden}%
\captionof{figure}{Example figure}\par%
\end{minipage}}}
A rectangular space can be placed in a paragraph with the text flowing across
the gap. The space may break open into the top or side of the paragraph or,
with some care, into the bottom (the number of lines specified
\InsertBoxC{\usebox{\inscontent}}
for the cutout should not exceed the amount of text available for those lines).
Some text or a logo or graphic may be placed within the window, or it may be
left empty. In this instance I have put three short bold text lines in the
window opening. The window should not be too wide as it can be difficult to
track the exterior text lines across the gap. A rectangular space can be placed
in a paragraph with the text flowing across the gap. The space may break open
into the top or side of the paragraph or, with some care, into the bottom.
\end{document}
结果:
评论:
这里使用红线是为了显示框的界限。它们将被抑制:1 丢弃两者之间的包和设置
%%--------
2 删除\fbox
主体代码中的在这两种方法中,图形的内容都必须放在 中,
savebox
它在 中是预定义的cutwin
,但必须在 中声明。如果想要使用、或与图片分组的任何其他内容,insbox
使用很方便,但不是强制性的。minipage
\caption
\label
该文件
insbox.tex
是不是一个 LaTeX 包(即一个.sty
文件),而是一个.tex
文件(也可以与 Plain-TeX 一起使用),并且必须用\input
而不是加载\usepackage
。正如您所说,这远非自动化,但在 LaTeX 中,当您想控制 LaTeX 自身要做什么时,这很常见。