答案1
下面提供了对marginfloat
s (和marginfigure
和margintable
) 的一些基本支持。您不能将\marginpar
它们一起使用 (当然可以,但不要指望它能正常工作)。
此外,如果您使用普通浮点数,我无法保证它们的编号是正确的(我没有彻底测试过。使用单个浮点数进行的快速测试figure
表明编号确实正确,但不知何故边距浮动分页符太早了)。
我让它eso-pic
进行输出例程黑客攻击,它使用atbegshi
,主要是为了帮助放置使用\AtTextUpperLeft
编辑:缺少了一个\fi
应该匹配的\ifhmode
(现在已添加),我还添加了\marginfloat@sep
控制两个 s 之间最小距离的长度marginfloat
。此外,我已改用\vtop to \textheight
而不是\vbox to \textheight
,因此\ht\marginfloat@ins\z@
不再需要。
代码如下:
\documentclass[]{book}
\usepackage{showframe} % only to show that the placement is roughly correct
\usepackage{eso-pic}
\makeatletter
\AddToShipoutPictureBG{\marginfloat@handle}
\newinsert\marginfloat@ins
\count\marginfloat@ins\z@
\skip\marginfloat@ins\z@
\dimen\marginfloat@ins\textheight
\newlength\marginfloat@sep
\marginfloat@sep2ex
\newenvironment{marginfloat}[1]
{%
\def\@captype{#1}%
\ifhmode\@bsphack\fi
\insert\marginfloat@ins\bgroup
\penalty100
\vbox\bgroup
\hsize=\marginparwidth
\linewidth=\hsize
\@afterheading
\@afterindentfalse
}%
{%
\egroup
\vfill
\vskip\marginfloat@sep
\egroup
\@Esphack
}
\newenvironment{marginfigure}{\marginfloat{figure}}{\endmarginfloat}
\newenvironment{margintable}{\marginfloat{table}}{\endmarginfloat}
\newcommand*\marginfloat@handle
{%
\setbox\marginfloat@ins\vtop to \textheight
{\null\vfill\unvbox\marginfloat@ins\vskip-\marginfloat@sep}%
\if@twoside
\ifodd\c@page
\AtTextUpperLeft
{%
\put(\LenToUnit{\dimexpr\textwidth+\marginparsep\relax},0)
{\box\marginfloat@ins}%
}%
\else
\AtTextUpperLeft
{%
\put(\LenToUnit{-\dimexpr\marginparsep+\marginparwidth\relax},0)
{\box\marginfloat@ins}%
}%
\fi
\else
\AtTextUpperLeft
{%
\put(\LenToUnit{\dimexpr\textwidth+\marginparsep\relax},0)
{\box\marginfloat@ins}%
}%
\fi
}
\makeatother
\usepackage[]{graphicx}
\usepackage{duckuments} % dummy content
\begin{document}
\blindduck[1-4]
\begin{marginfigure}
\centering
\includegraphics[width=\linewidth]{example-image-duck}
\caption{a duck}
\end{marginfigure}
\blindduck[1]
\begin{marginfigure}
\centering
\includegraphics[width=\linewidth]{example-image-duck}
\caption{another duck}
\end{marginfigure}
\begin{marginfigure}
\centering
\includegraphics[width=\linewidth]{example-image-duck}
\caption{another duck}
\end{marginfigure}
\begin{marginfigure}
\centering
\includegraphics[width=\linewidth]{example-image-duck}
\caption{another duck}
\end{marginfigure}
\begin{marginfigure}
\centering
\includegraphics[width=\linewidth]{example-image-duck}
\caption{another duck}
\end{marginfigure}
\begin{marginfigure}
\centering
\includegraphics[width=\linewidth]{example-image-duck}
\caption{another duck}
\end{marginfigure}
\blindduck[1-4]
\begin{marginfigure}
This is even more text.
\end{marginfigure}
\blindduck[5]
\begin{marginfigure}
This is even more text that is really long.
\end{marginfigure}
\clearpage
\begin{marginfigure}
\centering
\includegraphics[width=\linewidth]{example-image-duck}
\caption{yet another duck}
\end{marginfigure}
\blindduck[full]
\end{document}
该文件的第一页: