评论后编辑:

评论后编辑:

我有以下boxed环境。图像应放置在marginpar宽度内。当我environment变长时image,应自动增加到我的环境,当变低时,它会自动减少。最后,图像高度应从开始到\begin{keyidea}....结束\end{keyidea}。如何实现这一点。我的 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

这是不可破坏的,并且使用tcolorbox

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}
\definecolor{keyideascolor}{rgb}{0,0.5,0.1}
\makeatletter
\newtcolorbox{keyidea}[1][]{
  enhanced jigsaw,
  left=7pt,right=7pt,top=1.75ex,bottom=1.25ex,
  boxrule=0pt,
  arc=0pt,
  before=\par\noindent\vspace{2em},
  after=\par\noindent\vspace{2em},
  colback= keyideascolor ,
  overlay={
     \node[anchor=north west,inner sep=0pt,outer sep=0pt]
       (d) at (frame.north east)
       {\includegraphics[width=\marginparwidth,height=\tcb@height]{example-image-a}};
    },
  #1}
\makeatother
\begin{document}
\begin{keyidea}
\lipsum[1-3]
\end{keyidea}
\begin{keyidea}
\lipsum[4]
\end{keyidea}
\end{document}

在此处输入图片描述

如果你想使用不同的图像,

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}
\definecolor{keyideascolor}{rgb}{0,0.5,0.1}
\makeatletter
\newtcolorbox{keyidea}[2][]{
  enhanced jigsaw,
  left=7pt,right=7pt,top=1.75ex,bottom=1.25ex,
  boxrule=0pt,
  arc=0pt,
  before=\par\noindent\vspace{2em},
  after=\par\noindent\vspace{2em},
  colback= keyideascolor ,
  overlay={
     \node[anchor=north west,inner sep=0pt,outer sep=0pt]
       (d) at (frame.north east)
       {\includegraphics[width=\marginparwidth,height=\tcb@height]{#2}};
    },
  #1}
\makeatother
\begin{document}
\begin{keyidea}{logo}
\lipsum[1-3]
\end{keyidea}
\begin{keyidea}{example-image-a}
\lipsum[4]
\end{keyidea}
\end{document}

在此处输入图片描述

评论后编辑:

或许这就是所需要的。

\documentclass{book}
\usepackage[many]{tcolorbox}
\usepackage{ifoddpage}
\usepackage[margin=2cm,marginparwidth=0cm,marginparsep=0cm,inner=2.8in,outer=1in,]{geometry}
%% marginparwidth=0cm,marginparsep=0cm, because you want picture on the inner side and margin notes are usually on the outer side. Also you need more space on inner sides.
\usepackage{lipsum}
\definecolor{keyideascolor}{rgb}{0,0.5,0.1}
\makeatletter
\newtcolorbox{keyidea}[1][]{
  enhanced jigsaw,
  left=7pt,right=7pt,top=1.75ex,bottom=1.25ex,
  boxrule=0pt,
  arc=0pt,
  before=\par\noindent\vspace{2em},
  after=\par\noindent\vspace{2em},
  colback= keyideascolor ,
  overlay={
     \checkoddpage
     \ifoddpage
     \begin{scope}
     \clip ([xshift=-0.2in]frame.south west) rectangle ([xshift=-1.8in]frame.north west);
     \node[inner sep=0pt,outer sep=0pt,anchor = north east] (a)
       at ([xshift=-0.1in]frame.north west)
       {\includegraphics[height=\tcb@height]{example-image-a}};
     \node[anchor=north east,text width=1.7in] at (a.north east) {This is image on odd page};
     \end{scope}
     \else
     \begin{scope}
     \clip ([xshift=0.2in]frame.south east) rectangle ([xshift=1.8in]frame.north east);
     \node[inner sep=0pt,outer sep=0pt,anchor = north west](b)
       at ([xshift=0.1in]frame.north east)
       {\includegraphics[height=\tcb@height]{example-image-a}};
     \node[anchor=north west,text width=1.7in] at (b.north west) {This is image on even page};
     \end{scope}
     \fi
    },
  #1}
\makeatother
\begin{document}
\null
\newpage
\clearpage
\begin{keyidea}
\lipsum[1-2]
\end{keyidea}
\begin{keyidea}
\lipsum[4]
\end{keyidea}
\clearpage
\begin{keyidea}
\lipsum[1-3]
\end{keyidea}
\begin{keyidea}
\lipsum[4]
\end{keyidea}
\end{document}

在此处输入图片描述

相关内容