![如何防止下一段覆盖描述标签的图像?](https://linux22.com/image/273832/%E5%A6%82%E4%BD%95%E9%98%B2%E6%AD%A2%E4%B8%8B%E4%B8%80%E6%AE%B5%E8%A6%86%E7%9B%96%E6%8F%8F%E8%BF%B0%E6%A0%87%E7%AD%BE%E7%9A%84%E5%9B%BE%E5%83%8F%EF%BC%9F.png)
\documentclass{article}
\usepackage{mwe}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}
\setlength{\parindent}{0pt}
% begin custom preamble
\usepackage{enumitem}
\setlist[description]{style=multiline, leftmargin=4.2cm}
\renewcommand{\descriptionlabel}[1]{%
\begin{minipage}[t]{4cm}
\vspace{-\fontcharht\font`(}#1
\end{minipage}
}
% end custom preamble
\begin{document}
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}
\blindtext
\end{document}
结果是:
问题
我希望下一段自动垂直分隔,只需改变序言,而不是主文档。(请记住,它是潘多克!
答案1
可以让列表环境知道图像的深度,但使用起来更容易,wrapfig
我展示了两种变体,将缩进带到下一段,或将下一段向下移动,
\documentclass{article}
\usepackage{mwe,wrapfig}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}
\setlength{\parindent}{0pt}
\begin{document}
% Solution 1
\begin{wrapfigure}{l}{4cm}
\includegraphics{example-image}
\end{wrapfigure}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\blindtext
\bigskip
\hrule
\bigskip
% Solution 2
\begin{wrapfigure}{l}{4cm}
\includegraphics{example-image}
\end{wrapfigure}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\blindtext
\end{document}
答案2
最后,坚持不懈终有回报。这是一个基于的解决方案,floatflt
只需更改前言即可正确包装下一段。其中一个技巧是在\hspace{0pt}
。
\documentclass{article}
\usepackage{mwe}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}
\setlength{\parindent}{0pt}
% begin preamble.tex
\usepackage{floatflt}
\renewenvironment{description}{%
\renewcommand{\includegraphics}[1]{\vspace{-1ex}\Oldincludegraphics[width=4cm]{##1}}
\renewcommand{\item}[1][]{##1\end{floatingfigure}}%
\hspace{0pt}%
\begin{floatingfigure}[l]{4cm}}%
{}
% end preamble.tex
\begin{document}
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-a}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}
\blindtext
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-b}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}
\blindtext
\end{document}
评论
我断断续续花了将近一周的时间才想出这个答案。相比之下,仅仅三秒钟就发出了{float: left}
可以用 CSS 实现从同一页面生成的 HTML 文档中获取相同的结果Pandoc MarkDown。这让我非常感激这样的问题:
答案3
这是一个完全基于小页面、高度测量和序言体操的解决方案。唯一的限制是它不会换行,但会为图像和后续段落适当地腾出空间。尽管如此,我还是提出了后续问题试图在这些限制条件下实现真正的包装。
\documentclass{article}
\usepackage{mwe}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}
\setlength{\parindent}{0pt}
% begin preamble.tex
\usepackage{calc}
\newlength\imgheight
\renewenvironment{description}{%
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=4cm]{##1}}
\renewcommand{\item}[1][]{%
\settototalheight\imgheight{##1}%
\global\imgheight=\imgheight%
##1%
\end{minipage}
\hfill
\begin{minipage}[t][\imgheight][s]{\dimexpr\linewidth-4.2cm}
}
\begin{minipage}[t]{4cm}
\vspace{-\fontcharht\font`(}%
}%
{%
\par\vfill%
\end{minipage}}
% end preamble.tex
\begin{document}
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-a}]
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisis
sem. Nullam nec mi et neque pharetra sollicitudin.
\end{description}
\blindtext
\begin{description}
\itemsep1pt\parskip0pt\parsep0pt
\item[\includegraphics{example-image-b}]
\blindtext
\end{description}
\end{document}