方框或小页面中的边框

方框或小页面中的边框

我想在盒子或迷你页面中描画边框,就像这个模型一样。边框可自定义顶部左角和底部右角。有人能帮我吗?非常感谢 JPierre

盒子

答案1

这里我只是围绕颜色堆叠了一些规则\parbox。语法是

\specialbox{<box-color>}{<frame-color>}{<frame-length>}{<box-width>}{<content>}

\fboxsep它可以内联使用,也可以作为自己的段落使用。遵循和的值\fboxrule。作为一个框,它不会跨页。

这是 MWE。

\documentclass{article}
\usepackage{xcolor,stackengine,lipsum}
\newcommand\specialbox[5]{%
  \kern\fboxrule%
  \stackinset{r}{}{b}{0pt}{\color{#2}\rule{\fboxrule}{#3}}{%
  \stackinset{r}{}{b}{-\fboxrule}{\color{#2}\rule{#3}{\fboxrule}}{%
  \stackinset{l}{-\fboxrule}{t}{0pt}{\color{#2}\rule{\fboxrule}{#3}}{%
  \stackinset{l}{-\fboxrule}{t}{-\fboxrule}{\color{#2}\rule{#3}{\fboxrule}}{%
  \colorbox{#1}{\parbox{\dimexpr#4-2\fboxsep-2\fboxrule}{#5}}%
  }}}%
  \kern\fboxrule}%
}
\begin{document}
\lipsum[1]

\fboxrule=3pt\relax%
\noindent\specialbox{yellow!95!orange}{yellow!50!orange}{40pt}{\linewidth}{\lipsum[3]}

\lipsum[2]

\fboxsep=10pt\relax\fboxrule=5pt\relax%
Testing\specialbox{cyan!10}{red}{50pt}{3in}{\lipsum[4]}inline
\end{document}

在此处输入图片描述

答案2

一个不可避免的答案,tcolorbox并定义一些样式键:它们...lengths是从左上角和右下角计算的,颜色如图所示。

\documentclass{article}

\usepackage{blindtext}

\usepackage[most]{tcolorbox}
\usetikzlibrary{calc}
\begin{document}

\tcbset{
  toplength/.store in={\tcbcornerruletoplength},
  leftlength/.store in={\tcbcornerruleleftlength},
  toplength=3cm,
  leftlength=2cm,
  bottomlength/.store in={\tcbcornerrulebottomlength},
  rightlength/.store in={\tcbcornerrulerightlength},
  bottomlength=3cm,
  rightlength=2cm,
  cornerruleshift/.store in={\tcbcornerruleshift},
  cornerruleshift=1pt,
  topcornercolor/.store in={\tcbtopcornercolor},
  bottomcornercolor/.store in={\tcbbottomcornercolor},
  topcornercolor=green!40!blue,
  bottomcornercolor=blue!40!green,
}

\newtcolorbox{cornerbox}[1][]{%
  enhanced jigsaw,
  sharp corners,
  boxrule=0pt,
  underlay={
    \coordinate (topend) at ($(frame.north west) + (0:\tcbcornerruletoplength)$);
    \coordinate (leftend) at ($(frame.north west) - (90:\tcbcornerruleleftlength)$);
    \coordinate (bottomend) at ($(frame.south east) - (0:\tcbcornerrulebottomlength)$);
    \coordinate (rightend) at ($(frame.south east) + (90:\tcbcornerrulerightlength)$);
    \draw[line width=2pt,\tcbtopcornercolor] ([xshift=-\tcbcornerruleshift]leftend) -- ([shift={(-\tcbcornerruleshift,\tcbcornerruleshift)}]frame.north west) -- ([shift={(-\tcbcornerruleshift,\tcbcornerruleshift)}] topend);
    \draw[line width=2pt,\tcbbottomcornercolor] ([xshift=\tcbcornerruleshift]rightend) -- ([shift={(\tcbcornerruleshift,-\tcbcornerruleshift)}]frame.south east) -- ([shift={(-\tcbcornerruleshift,-\tcbcornerruleshift)}] bottomend);
  },
  #1,
}


\begin{cornerbox}
\blindtext
\end{cornerbox}

\begin{cornerbox}[topcornercolor={yellow!40!red}]
\blindtext
\end{cornerbox}


\end{document}

在此处输入图片描述

更新具有可配置的颜色和线宽:

\documentclass{article}

\usepackage{blindtext}

\usepackage[most]{tcolorbox}
\usetikzlibrary{calc}
\begin{document}

\tcbset{
  % Lengths of the rules
  northwestleft/.store in={\chnorthwestleftlength},
  southwestleft/.store in={\chsouthwestleftlength},
  northeastright/.store in={\chnortheastrightlength},
  southeastright/.store in={\chsoutheastrightlength},
  northwestleft=2cm,
  southwestleft=2cm,
  northeastright=2cm,
  southeastright=2cm,
  northleft/.store in={\chnorthleftlength},
  northright/.store in={\chnorthrightlength},
  southleft/.store in={\chsouthleftlength},
  southright/.store in={\chsouthrightlength},
  northleft=2cm,
  southleft=2cm,
  northright=2cm,
  southright=2cm,
  toplength/.forward to=/tcb/northright,
  toplength/.forward to=/tcb/northleft,
  leftlength/.forward to=/tcb/northwestleft,
  leftlength/.forward to=/tcb/southwestleft,
  rightlength/.forward to=/tcb/northeastright,
  rightlength/.forward to=/tcb/southeastright,
  bottomlength/.forward to=/tcb/southright,
  bottomlength/.forward to=/tcb/southleft,
  % Widths of the rules
  northwestleftrule/.store in={\chnorthwestleftrule},
  northwesttoprule/.store in={\chnorthwesttoprule},
  northwestrule/.forward to=/tcb/northwestleftrule,
  northwestrule/.forward to=/tcb/northwesttoprule,
  northeastrightrule/.store in={\chnortheastrightrule},
  northeasttoprule/.store in={\chnortheasttoprule},
  northeastrule/.forward to=/tcb/northeastrightrule,
  northeastrule/.forward to=/tcb/northeasttoprule,
  southwestleftrule/.store in={\chsouthwestleftrule},
  southwestbottomrule/.store in={\chsouthwestbottomrule},
  southwestrule/.forward to=/tcb/southwestleftrule,
  southwestrule/.forward to=/tcb/southwestbottomrule,
  southeastrightrule/.store in={\chsoutheastrightrule},
  southeastbottomrule/.store in={\chsoutheastbottomrule},
  southeastrule/.forward to=/tcb/southeastrightrule,
  southeastrule/.forward to=/tcb/southeastbottomrule,
  northwestleftrule=2pt,
  northwesttoprule=2pt,
  southwestleftrule=2pt,
  southwestbottomrule=2pt,
  northeastrightrule=2pt,
  northeasttoprule=2pt,
  southeastrightrule=2pt,
  southeastbottomrule=2pt,
  northrules/.forward to=/tcb/northwestrule,
  northrules/.forward to=/tcb/northeastrule,
  southrules/.forward to=/tcb/southwestrule,
  southrules/.forward to=/tcb/southeastrule,
  allrules/.forward to=/tcb/northrules,
  allrules/.forward to=/tcb/southrules,
  % Colours
  upperleftcolor/.store in={\chupperleftcolor},
  upperrightcolor/.store in={\chupperrightcolor},
  lowerrightcolor/.store in={\chlowerrightcolor},
  lowerleftcolor/.store in={\chlowerleftcolor},
  lowerleftcolor={blue},
  lowerrightcolor={blue},
  upperrightcolor={blue},
  upperleftcolor={blue},
  northcolor/.forward to=/tcb/upperleftcolor,
  northcolor/.forward to=/tcb/upperrightcolor,
  southcolor/.forward to=/tcb/lowerleftcolor,
  southcolor/.forward to=/tcb/lowerrightcolor,
  allcolors/.forward to=/tcb/northcolor,
  allcolors/.forward to=/tcb/southcolor,
  northwest/.style={%
    sharp corners=northwest,
    overlay app={
      \coordinate (north west corner) at ([shift={(-0.5*\chnorthwestleftrule,\chnorthwesttoprule)}]frame.north west);
      \coordinate (west start) at ([shift={(-0.5*\chnorthwestleftrule,-\chnorthwestleftlength)}]frame.north west);
      \coordinate (north end) at ([shift={(\chnorthleftlength,\chnorthwesttoprule))}]frame.north west);
      \draw[line width=\chnorthwestleftrule,\chupperleftcolor] (west start) -- (north west corner);
      \draw[line width=\chnorthwesttoprule,\chupperleftcolor] ([yshift={(-0.5*\chnorthwesttoprule)}]north west corner) -- ([yshift={(-0.5*\chnorthwesttoprule)}]north end);
    }
  },
  southeast/.style={
    sharp corners=southeast,
    overlay app={
      \coordinate (south east corner)   at ([shift={(0.5*\chsoutheastrightrule,-\chsoutheastbottomrule)}]frame.south east);
      \coordinate (east end) at ([shift={(-\chsouthrightlength,-\chsoutheastbottomrule)}]frame.south east);
      \coordinate (east start)  at ([shift={(0.5*\chsoutheastrightrule,\chsoutheastrightlength)}]frame.south east);
      \draw[line width=\chsoutheastrightrule,\chlowerrightcolor] (east start) -- (south east corner);
      \draw[line width=\chsoutheastbottomrule,\chlowerrightcolor] ([yshift={(0.5*\chsoutheastbottomrule)}]south east corner) -- ([yshift={(0.5*\chsoutheastbottomrule)}]east end);
    }
  },    
  northeast/.style={
    sharp corners=northeast,
    overlay app={
      \coordinate (north east corner)   at ([shift={(0.5*\chnortheastrightrule,\chnortheasttoprule)}]frame.north east);
      \coordinate (north start)  at ([shift={(0.5*\chnortheastrightrule,-\chnortheastrightlength)}]frame.north east);
      \coordinate (north end) at ([shift={(-\chnorthrightlength,\chnortheasttoprule))}]frame.north east);
      \draw[line width=\chnortheastrightrule,\chupperrightcolor] (north start) -- (north east corner);
      \draw[line width=\chnortheasttoprule,\chupperrightcolor] ([yshift={(-0.5*\chnortheasttoprule)}]north east corner) --  ([yshift={(-0.5*\chnortheasttoprule)}]north end);
    }
  },
  southwest/.style={
    sharp corners=southwest,
    overlay app={
      \coordinate (south west corner)   at ([shift={(-0.5*\chsouthwestleftrule,-\chsouthwestbottomrule)}]frame.south west);
      \coordinate (south start)  at ([shift={(-0.5*\chsouthwestleftrule,\chsouthwestleftlength)}]frame.south west);
      \coordinate (south end) at ([shift={(\chsouthleftlength,-\chsouthwestbottomrule)}]frame.south west);
      \draw[line width=\chsouthwestleftrule,\chlowerleftcolor] (south start) -- (south west corner);
      \draw[line width=\chsouthwestbottomrule,\chlowerleftcolor] ([yshift={(0.5*\chsouthwestbottomrule)}]south west corner) -- ([yshift={(0.5*\chsouthwestbottomrule)}]south end);
    }
  },
  northwestsoutheast/.style={overlay={},northwest,southeast},
  northeastsouthwest/.style={overlay={},northeast,southwest},
  allcorners/.style={overlay={},northeastsouthwest,northwestsoutheast},
}


\newtcolorbox{cornerbox}[1][]{%
  enhanced jigsaw,
  boxrule=0pt,
  northwestsoutheast,
  #1
}


\makeatother


\begin{cornerbox}[northeastsouthwest,allcolors=yellow!20!red,allrules=2pt]
\blindtext
\end{cornerbox}

\begin{cornerbox}[allcolors=green!40!blue,northwestleftrule=5pt]
\blindtext
\end{cornerbox}


\end{document}

在此处输入图片描述

相关内容