使用 mdframed 淡化阴影

使用 mdframed 淡化阴影

我想创建一个这样的环境:

带有褪色阴影的盒子

它是我用这段代码创建的:

%
% Boxed environment with semi-transparent shadow.
%
\newlength{\boxw}
\newlength{\boxh}
\newlength{\shadowsize}
\newlength{\boxroundness}
\newlength{\tmpa}
\newsavebox{\shadowblockbox}

\setlength{\shadowsize}{6pt}
\setlength{\boxroundness}{3pt}

\newenvironment{shadowblock}[1]%
{\begin{lrbox}{\shadowblockbox}\begin{minipage}{#1}}%
{\end{minipage}\end{lrbox}%
\settowidth{\boxw}{\usebox{\shadowblockbox}}%
\settodepth{\tmpa}{\usebox{\shadowblockbox}}%
\settoheight{\boxh}{\usebox{\shadowblockbox}}%
\addtolength{\boxh}{\tmpa}%
\begin{tikzpicture}
\addtolength{\boxw}{\boxroundness * 2}
\addtolength{\boxh}{\boxroundness * 2}

\foreach \x in {0,.05,...,1}
{
\setlength{\tmpa}{\shadowsize * \real{\x}}
\fill[xshift=\shadowsize - 1pt,yshift=-\shadowsize + 
1pt,black,opacity=.04,rounded corners=\boxroundness] 
(\tmpa, \tmpa) rectangle +(\boxw - \tmpa - \tmpa, \boxh - \tmpa - 
\tmpa);
}

\filldraw[fill=yellow!50, draw=black!50, rounded corners=\boxroundness] (0, 
0) rectangle (\boxw, \boxh);
\draw node[xshift=\boxroundness,yshift=\boxroundness,inner sep=0pt,outer 
sep=0pt,anchor=south west] (0,0) {\usebox{\shadowblockbox}};
\end{tikzpicture}}

如您所见,如果您编译它,它就会出现淡入淡出的阴影,但是,由于它用于\minipage防止文本很长,因此它不会分成 2 页。我曾想过这可以通过环境来实现mdframed,但是,虽然它有阴影,但没有任何淡入淡出效果。例如,我尝试过:

\begin{mdframed}[backgroundcolor=yellow!50,linecolor=black!50,roundcorner=10pt, % shadow=true,
     tikzsetting={shade,
      top color=yellow!50,
      bottom color=yellow!10,
      rounded corners=1pt,
      blur shadow={shadow blur steps=5,shadow blur extra rounding=1.3pt,shadow yshift=-0.5cm}
      }]
\lipsum[4-5]
\end{mdframed}

但是当你编译时它不能正确绘制阴影:

在此处输入图片描述

有任何想法吗?

答案1

更新:

如果帧分裂,mdframed则产生的默认阴影不会产生所需的效果;但是,这两个软件包都足够强大,可以轻松解决问题。tcolorbox

这是一个可能的解决方案mdframed

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\newmdenv[
backgroundcolor=yellow!60,
roundcorner=4ptem,
middleextra={
\foreach \sshift/\svalue in {6/gray!10,5/gray!20,4/gray!30,3/gray!40,2/gray!50,1/gray!60}
  {
  \fill[\svalue] 
    (P) --
    ([xshift=\sshift pt]P) -- 
    ([xshift=\sshift pt]P|-O) --
    (P|-O) -- cycle;
  }
},
secondextra={
\foreach \sshift/\svalue in {6/gray!10,5/gray!20,4/gray!30,3/gray!40,2/gray!50,1/gray!60}
  {
  \fill[\svalue,rounded corners] 
    ([xshift=\sshift]O) --
    ([xshift=\sshift,yshift=-\sshift]O) --
    ([xshift=\sshift,yshift=-\sshift]P|-O)
    {[sharp corners] -- ([xshift=\sshift]P) -- 
    (P)} -- 
    (P|-O) -- ([xshift=\sshift]O);
  }
},
firstextra={
\foreach \sshift/\svalue in {6/gray!10,5/gray!20,4/gray!30,3/gray!40,2/gray!50,1/gray!60}
  {
  \fill[\svalue,rounded corners] 
    ([yshift=-\sshift]P) --
    ([xshift=\sshift,yshift=-\sshift]P) 
    {[sharp corners] -- ([xshift=\sshift]P|-O) -- 
    (P|-O)} -- ([yshift=-\sshift]P);
  }
},
singleextra={
\foreach \sshift/\svalue in {6/gray!10,5/gray!20,4/gray!30,3/gray!40,2/gray!50,1/gray!60}
  {
  \fill[\svalue,rounded corners] 
    ([xshift=\sshift]O) --
    ([xshift=\sshift,yshift=-\sshift]O) --
    ([xshift=\sshift,yshift=-\sshift]P|-O) --
    ([xshift=\sshift,yshift=-\sshift]P) -- +(-6pt,0) --
    (P) -- 
    (P|-O) -- ([xshift=\sshift]O);
  }
},
]{mymdbox}

\begin{document}

\begin{mymdbox}
\lipsum[2]
\end{mymdbox}
\begin{mymdbox}
\lipsum[2]\lipsum[2]\lipsum[2]
\end{mymdbox}

\end{document}

在此处输入图片描述

这是一个可能的解决方案tcolorbox

\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox{mytcbox}{
breakable,
enhanced,
colback=yellow!60,
overlay middle={
\foreach \sshift/\svalue in {6/gray!10,5/gray!20,4/gray!30,3/gray!40,2/gray!50,1/gray!60}
  {
  \fill[\svalue] 
    (frame.north east) --
    ([xshift=\sshift pt]frame.north east) -- 
    ([xshift=\sshift pt]frame.south east) --
    (frame.south east) -- cycle;
  }
},
overlay last={
\foreach \sshift/\svalue in {6/gray!10,5/gray!20,4/gray!30,3/gray!40,2/gray!50,1/gray!60}
  {
  \fill[\svalue,rounded corners] 
    ([xshift=\sshift]frame.south west) --
    ([xshift=\sshift,yshift=-\sshift]frame.south west) --
    ([xshift=\sshift,yshift=-\sshift]frame.south east)
    {[sharp corners] -- ([xshift=\sshift]frame.north east) -- 
    (frame.north east)} -- 
    (frame.south east) -- ([xshift=\sshift]frame.south west);
  }
},
overlay first={
\foreach \sshift/\svalue in {6/gray!10,5/gray!20,4/gray!30,3/gray!40,2/gray!50,1/gray!60}
  {
  \fill[\svalue,rounded corners] 
    ([yshift=-\sshift]frame.north east) --
    ([xshift=\sshift,yshift=-\sshift]frame.north east) 
    {[sharp corners] -- ([xshift=\sshift]frame.south east) -- 
    (frame.south east)} -- ([yshift=-\sshift]frame.north east);
  }
},
overlay unbroken={
\foreach \sshift/\svalue in {6/gray!10,5/gray!20,4/gray!30,3/gray!40,2/gray!50,1/gray!60}
  {
  \fill[\svalue,rounded corners] 
    ([xshift=\sshift]frame.south west) --
    ([xshift=\sshift,yshift=-\sshift]frame.south west) --
    ([xshift=\sshift,yshift=-\sshift]frame.south east) --
    ([xshift=\sshift,yshift=-\sshift]frame.north east) -- +(-8pt,0) --
    (frame.north east) -- 
    (frame.south east) -- ([xshift=\sshift]frame.south west);
  }
},
before={\vskip1em},
after={\vskip1em},
}

\begin{document}

\begin{mytcbox}
\lipsum[4]
\end{mytcbox}

\begin{mytcbox}
\lipsum[4]\lipsum[4]\lipsum[4]\lipsum[4]
\end{mytcbox}

\end{document}

在此处输入图片描述

答案2

感谢 Gonzalo Medida 之前的回答,我找到了一个很好的解决方案:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage[most]{tcolorbox}

\usepackage{tikz}
\usetikzlibrary{arrows,shadows} % for pgf-umlsd
\usetikzlibrary{shadows.blur}
\usetikzlibrary{shadings}
\usepackage{lipsum}

\newtcolorbox{myboxdos}{
    breakable,enhanced,
    boxrule=0.5pt,
    colback=yellow!50,
    %skin=freelance, % esto "estropea" los bordes cuando se divide un colorbox
    drop fuzzy shadow,
    before=\vspace{1em},
    after=\vspace{1em},
}

\begin{document}

\begin{myboxdos}
\lipsum[1-7]
\end{myboxdos}

\end{document}

他的回答存在问题,缺少“增强”属性,并且不应该写“skin=XXX”,因为这会覆盖“增强”皮肤。

关于阴影,不需要编写单独的阴影,可以使用“模糊”选项,然后使用“放置”将其自动放置在正确的位置。

此外,我在盒子前后添加了一些额外的空间。

这是最终版本: 在此处输入图片描述

相关内容