我有以下盒装环境。图像应放置在 marginpar 宽度内。当我的环境变长时,图像应自动增加到我的环境,当变低时,它会自动减小。预期图像高度应从 \begin{keyidea}.... 开始,以 \end{keyidea} 结束。环境将仅在单个页面中开始和结束,不会继续到下一页。
图像marginpar
大小为固定宽度和高度,即 70pt(宽度)和 300pt(高度)。当文本较小时,图片边距应从图形顶部高度(例如 1pt)减小到实际文本宽度。我的 MWE 是:
\documentclass{book}
\usepackage{xcolor,graphics}
\usepackage{fancyhdr}
\usepackage[framemethod=tikz,usetwoside=false]{mdframed}
\usepackage{lipsum}
\definecolor{keyideascolor}{rgb}{0,0.5,0.1}
%%%%\includegraphics{keyiidea} keyicon marginpar image
\makeatletter
\newmdenv[backgroundcolor=keyideascolor,
hidealllines=true,
leftmargin=0pt,
rightmargin=0pt,
innerleftmargin=7pt,
innerrightmargin=7pt,
innertopmargin=1.75ex,
innerbottommargin=1.25ex,
skipabove=0pt,
skipbelow=0pt,
nobreak=false,
splittopskip=2ex,
splitbottomskip=2ex,
roundcorner=0pt
]{@keyidea}
\newenvironment{keyidea}{%
\addvspace{1.75ex}\vspace{\parskip}%
\goodbreak
\@keyidea
}{%
\end@keyidea
\null\goodbreak
}
\makeatother
\begin{document}
\chapter{Chapter Title here}
\lipsum[1]
\section{A Level Head}
\begin{keyidea}%1
\lipsum[2-3]
\includegraphics{keyiidea.pdf}
\end{keyidea}
\lipsum[4]
\begin{keyidea}%2
\lipsum[5-8]
\end{keyidea}
\end{document}
![在此处输入图片描述][1]
答案1
我建议你使用tcolorbox
:
代码:
\documentclass{book}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{marginnote}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}
\definecolor{keyideascolor}{rgb}{0,0.5,0.1}
%%%%\includegraphics{keyiidea} keyicon marginpar image
\newtcolorbox{fkeyidea}[1][]{
breakable,
equal height group=A,
arc=0pt,
outer arc=0pt,
colback=keyideascolor,
#1
}
\newcounter{auxcnt}
\newenvironment{keyidea}
{%
\stepcounter{auxcnt}%
\begin{fkeyidea}[equal height group={\romannumeral\theauxcnt}]
\marginnote{\vskip-1.9\baselineskip\begin{tcolorbox}[
enhanced,
nobeforeafter,
boxrule=0pt,
outer arc=0pt,
arc=0pt,
top=0pt,
bottom=0pt,
colback=red,
width=\marginparwidth,
equal height group={\romannumeral\theauxcnt},
watermark graphics=mushrooms,
watermark overzoom=1
]\end{tcolorbox}
}%
}
{\end{fkeyidea}}
\begin{document}
\section{A Level Head}
\begin{keyidea}
\lipsum[2]
\end{keyidea}
\lipsum[4]
\begin{keyidea}%2
\lipsum[5-6]
\end{keyidea}
\end{document}
评论
这个想法是定义一个由两个tcolorbox
es 组成的环境;第一个tcolorbox
将包含文本,另一个将包含图像(使用键watermark
);使用equal height group
保证每对框具有相同的高度。包含图像的框使用\marginnote
从marginnote
包裹。