![tcolorbox 之后的文本与 wrapfig 内的图形重叠](https://linux22.com/image/384533/tcolorbox%20%E4%B9%8B%E5%90%8E%E7%9A%84%E6%96%87%E6%9C%AC%E4%B8%8E%20wrapfig%20%E5%86%85%E7%9A%84%E5%9B%BE%E5%BD%A2%E9%87%8D%E5%8F%A0.png)
我一直尝试在环境wrapfig
内使用tcolorbox
一定量的文本,但这些文本不会覆盖环境内图形的高度。
以下代码导致环境后的文本与图形重叠。更准确地说,环境无法识别其中包含的图形的高度。
\documentclass[draft]{article}
\usepackage{showframe}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{tikz}
%% To wrap text around figures
\usepackage{wrapfig}
\usepackage{etoolbox}
\BeforeBeginEnvironment{wrapfigure}{%
\setlength{\intextsep}{-0.5\baselineskip}%
}
%% Measuring Figure Width and Height
\newbox\mybox
\def\mysaver{%
\pgfmathparse{\pgfpositionnodelatermaxx-\pgfpositionnodelaterminx}%
\xdef\savednodewidth{\pgfmathresult pt}%
\pgfmathparse{\pgfpositionnodelatermaxy-\pgfpositionnodelaterminy}%
\xdef\savednodeheight{\pgfmathresult pt}%
}
\usepackage[skins,breakable]{tcolorbox}
\newenvironment{myEnvironment}{%
\begin{tcolorbox}[blanker,width=\textwidth-20pt,enlarge left by=20pt,before skip=2em,breakable,overlay unbroken and first={%
\node[inner sep=0pt,outer sep=0pt,anchor=north east] at ([shift= {(-10pt,1.5pt)}]frame.north west) {\textcolor{olive}{\rule{10pt}{10pt}}};}]%
\setlength{\parskip}{1.25em}%
}{\vspace{\baselineskip}
\end{tcolorbox}}
\newcommand{\myFigure}[1]{%
\tikz{{\pgfpositionnodelater{\mysaver}\node[inner sep=0pt,outer sep=0pt,align=center]{#1};}}%
\begin{wrapfigure}{R}{\savednodewidth+20pt}
\hfill \parbox{\savednodewidth}{\hfill #1}
\end{wrapfigure}
}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales.
\begin{myEnvironment}
\textbf{Lorem Ipsum}
\myFigure{\includegraphics[scale=0.6]{Fig1}\\
\includegraphics[scale=0.6]{Fig2}\\
\includegraphics[scale=0.6]{Fig3}}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales. Nunc pharetra est sit amet ligula sodales tempor.
Vestibulum facilisis, enim sed tempor luctus, arcu dolor iaculis ante, semper fringilla magna nibh fermentum tortor.
Nulla eu libero vulputate, dapibus est in, molestie nunc. Morbi ut iaculis lectus.
\end{myEnvironment}
\myFigure{\includegraphics[scale=0.6]{Fig4}}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales. Nunc pharetra est sit amet ligula sodales tempor. Vestibulum facilisis, enim sed tempor luctus, arcu dolor iaculis ante, semper fringilla magna nibh fermentum tortor. Nulla eu libero vulputate, dapibus est in, molestie nunc. Morbi ut iaculis lectus.
\end{document}
这是我需要的输出。我不得不使用一种变通方法来实现它。但不幸的是,我不能将它用作永久解决方案。
答案1
对于您修改后的 MWE,您可以在 tcolorbox 结束前添加所需的空格。计数器WF@wrappedlines
是剩余行数加一。
看来 wrapfig 处理得不是\parskip>0pt
很好。缩进的行数是在知道段落数之前计算出来的。
为了演示目的,我将规则保留可见;实际上,应该将宽度设置为 0pt。
顺便说一句,\includegraphics
真的不喜欢使用未定义的文件,即使在草稿模式下。
\documentclass[draft]{article}
\usepackage{showframe}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{tikz}
%% To wrap text around figures
\usepackage{wrapfig}
\usepackage{etoolbox}
\BeforeBeginEnvironment{wrapfigure}{%
\setlength{\intextsep}{-0.5\baselineskip}%
}
%% Measuring Figure Width and Height
\newbox\mybox
\def\mysaver{%
\pgfmathparse{\pgfpositionnodelatermaxx-\pgfpositionnodelaterminx}%
\xdef\savednodewidth{\pgfmathresult pt}%
\pgfmathparse{\pgfpositionnodelatermaxy-\pgfpositionnodelaterminy}%
\xdef\savednodeheight{\pgfmathresult pt}%
}
\usepackage[skins,breakable]{tcolorbox}
\newenvironment{myEnvironment}{%
\begin{tcolorbox}[blanker,width=\textwidth-20pt,enlarge left by=20pt,before skip=2em,breakable,overlay unbroken and first={%
\node[inner sep=0pt,outer sep=0pt,anchor=north east] at ([shift= {(-10pt,1.5pt)}]frame.north west) {\textcolor{olive}{\rule{10pt}{10pt}}};}]%
\setlength{\parskip}{1.25em}%
}{\vspace{\baselineskip}
\end{tcolorbox}}
\newcommand{\myFigure}[1]{%
\tikz{{\pgfpositionnodelater{\mysaver}\node[inner sep=0pt,outer sep=0pt,align=center]{#1};}}%
\begin{wrapfigure}{R}{\savednodewidth+20pt}
\hfill \parbox{\savednodewidth}{\hfill #1}
\end{wrapfigure}
}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales.
\begin{myEnvironment}
\textbf{Lorem Ipsum}
\myFigure{\includegraphics[width=3cm]{example-image}\\
\includegraphics[width=4cm]{example-image-a}\\
\includegraphics[width=4cm]{example-image-b}}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales. Nunc pharetra est sit amet ligula sodales tempor.
Vestibulum facilisis, enim sed tempor luctus, arcu dolor iaculis ante, semper fringilla magna nibh fermentum tortor.
Nulla eu libero vulputate, dapibus est in, molestie nunc. Morbi ut iaculis lectus.
\par\rule[-\dp\strutbox]{1pt}{\dimexpr \arabic{WF@wrappedlines}\baselineskip-\baselineskip-4\parskip}\WFclear% add extra sapace
\end{myEnvironment}
\myFigure{\includegraphics[width=4cm]{example-image-c}}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer bibendum dui a ex faucibus sodales. Nunc pharetra est sit amet ligula sodales tempor. Vestibulum facilisis, enim sed tempor luctus, arcu dolor iaculis ante, semper fringilla magna nibh fermentum tortor. Nulla eu libero vulputate, dapibus est in, molestie nunc. Morbi ut iaculis lectus.
\end{document}