8

Dear friends, I have the following environment in a document:

Environment 1

which is produced the following code, except the dark green line, I kept it back for keeping my the minimal component in my 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}

I would like to add a striped background to it, something along the lines of:

Environment 2

It would be great if we could specify both thickness and colour of such bars. I am using mdframed at the moment, but a tcolorbox solution would also be welcoming. :) I think one could exploit the patterns library, but I am not sure on what to do here.

Any hints? :)

Paulo Cereda
  • 44,220
  • er, um, i don't see any stripes there. but, unrelated to that, the "frame" isn't equally spaced on top and bottom, most likely because the last line doesn't have any descenders. adding a \strut to the last line should solve that. – barbara beeton Jun 11 '15 at 17:48
  • @barbara: you mean my second image? :) Sorry for the unequal spacing, I wrote a quick and dirty example! You are absolutely right, \strut does solve it; I can also add a innerbottommargin option to my mdframed environment and make things display as expected. – Paulo Cereda Jun 11 '15 at 17:54
  • but you didn't say anything about the absence of stripes? (the example may be quick & dirty, but i really did expect a "smudge" or two.) or maybe it's just my monitor that's misbehaving? – barbara beeton Jun 11 '15 at 18:11
  • @barbara: Oh I am so sorry, I had no idea on how to put the stripes in the first place, hence my poor example. :) – Paulo Cereda Jun 11 '15 at 19:09
  • looks fancy :) is this for arara v4 manual? :) – cmhughes Jun 12 '15 at 09:28
  • 1
    @cmhughes: Oh no, I've been spotted! :) I'm planning something along these lines, but a huge block with a striped background doesn't look so pleasant to the eye (it works with smaller chunks), so I have to use it carefully. :) By the way, we are making some huge progress on the tool, it's more stable than ever and with very cool features, I am sure you will love it. :) – Paulo Cereda Jun 12 '15 at 10:25

1 Answers1

8

Below I give you some possibilities using both mdframed and tcolorbox:

The tcolorbox option:

\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}

The output:

enter image description here

The mdframed option:

\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}

The output:

enter image description here

Remarks

  • The customization for the pattern was done using Philippe Goutet's code in his answer to Custom and built in TikZ fill patterns.

  • Since the code in the question includes the nobreak option I assumed in my answer that the environment is not required to admit page breaks; if this is not the case, the modifications required are not too big and can be easily made.

Gonzalo Medina
  • 505,128