带条纹背景的框架环境

带条纹背景的框架环境

各位朋友,我在一个文档中,有以下环境:

环境 1

产生了以下代码,除了深绿色的线,我把它保留下来以保留我的最小我的 MWE 中的组件。:)

\documentclass{article}

\usepackage[framemethod=TikZ]{mdframed}

\newmdenv[
   nobreak,
   middlelinewidth=.8pt,
   roundcorner=8pt
]{myframe}

\begin{document}

\begin{myframe}
One line\\
Two lines\\
Three lines
\end{myframe}

\end{document}

我想给它添加条纹背景,类似于:

环境 2

如果我们可以指定这些条的粗细和颜色,那就太好了。我mdframed目前正在使用,但tcolorbox如果有解决方案,那也欢迎。:)我认为可以利用这个patterns库,但我不确定在这里该怎么做。

有什么提示吗?:)

答案1

下面我给你一些使用和的mdframed可能性tcolorbox

tcolorbox选项:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usetikzlibrary{patterns}

% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         hatchthickness/.code={\setlength{\hatchthickness}{#1}},
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
         hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
\tikzset{hatchspread=3pt,
         hatchthickness=0.4pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black}
% declaring the pattern
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
   {custom north east lines}% name
   {\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
   {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr\hatchspread+2\hatchthickness}}% upper right corner
   {\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}% tile size
   {% shape description
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{\dimexpr\hatchspread+\hatchshift}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+\hatchshift}{-0.15pt}}
    \ifdim \hatchshift > 0pt
      \pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
      \pgfpathlineto{\pgfqpoint{\dimexpr0.15pt+\hatchshift}{-0.15pt}}
    \fi
    \pgfsetstrokecolor{\hatchcolor}
%    \pgfsetdash{{1pt}{1pt}}{0pt}% dashing cannot work correctly in all situation this way
    \pgfusepath{stroke}
   }
   
\newtcolorbox{stripbox}{
  enhanced,
  frame code={},
  interior code={
    \path[
      draw=green!80!black,
      rounded corners,
      pattern=custom north east lines,
      hatchspread=12pt,
      hatchthickness=4pt,
      hatchcolor=gray!20
    ]
    (interior.south east) rectangle (interior.north west);
  }
}

\begin{document}

\begin{stripbox}
test text
\end{stripbox}

\end{document}

输出:

在此处输入图片描述

mdframed选项:

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{patterns,backgrounds}

% defining the new dimensions and parameters
\newlength{\hatchspread}
\newlength{\hatchthickness}
\newlength{\hatchshift}
\newcommand{\hatchcolor}{}
% declaring the keys in tikz
\tikzset{hatchspread/.code={\setlength{\hatchspread}{#1}},
         hatchthickness/.code={\setlength{\hatchthickness}{#1}},
         hatchshift/.code={\setlength{\hatchshift}{#1}},% must be >= 0
         hatchcolor/.code={\renewcommand{\hatchcolor}{#1}}}
% setting the default values
\tikzset{hatchspread=3pt,
         hatchthickness=0.4pt,
         hatchshift=0pt,% must be >= 0
         hatchcolor=black}
% declaring the pattern
\pgfdeclarepatternformonly[\hatchspread,\hatchthickness,\hatchshift,\hatchcolor]% variables
   {custom north east lines}% name
   {\pgfqpoint{\dimexpr-2\hatchthickness}{\dimexpr-2\hatchthickness}}% lower left corner
   {\pgfqpoint{\dimexpr\hatchspread+2\hatchthickness}{\dimexpr\hatchspread+2\hatchthickness}}% upper right corner
   {\pgfqpoint{\dimexpr\hatchspread}{\dimexpr\hatchspread}}% tile size
   {% shape description
    \pgfsetlinewidth{\hatchthickness}
    \pgfpathmoveto{\pgfqpoint{0pt}{\dimexpr\hatchspread+\hatchshift}}
    \pgfpathlineto{\pgfqpoint{\dimexpr\hatchspread+0.15pt+\hatchshift}{-0.15pt}}
    \ifdim \hatchshift > 0pt
      \pgfpathmoveto{\pgfqpoint{0pt}{\hatchshift}}
      \pgfpathlineto{\pgfqpoint{\dimexpr0.15pt+\hatchshift}{-0.15pt}}
    \fi
    \pgfsetstrokecolor{\hatchcolor}
%    \pgfsetdash{{1pt}{1pt}}{0pt}% dashing cannot work correctly in all situation this way
    \pgfusepath{stroke}
   }
   
\tikzset{
} 
    
\newmdenv[
  nobreak,
  roundcorner=6pt,
  linewidth=0pt,
  apptotikzsetting={
    \tikzset{mdfbackground/.append style=
      {
        draw=green!80!black,
        rounded corners,
        pattern=custom north east lines,
        hatchspread=12pt,
        hatchthickness=4pt,
        hatchcolor=gray!20
      }
    }
  }
]{stripbox}

\begin{document}

\begin{stripbox}
test text
\end{stripbox}

\end{document}

输出:

在此处输入图片描述

评论

  • 图案的定制是使用菲利普·古特的代码他的回答自定义和内置 TikZ 填充图案

  • 由于问题中的代码包含nobreak我在回答中假设的选项,即环境不需要承认分页符;如果不是这种情况,则所需的修改不会太大,并且可以轻松完成。

相关内容