# Dashed box environment

I am new to LaTeX and I'm having a hard time trying to create a dashed box environment without success. Can someone please share a code for a dashed box environment such as the one in the image?

I would like to use it the following way:

My Elaboration
\begin{elaboration}{My item list is shown below:}
\item dummy item
\item dummy item 2
\item dummy item 3
\end{elaboration}


By the way, how could be the code for the same environment but with a continuous box (not dashed)? Thanks in advance!

-

Welcome, and good question. I don't know of a LaTeX package that can easily do what you want out of the box. The fancybox package does a lot of fancy boxes (naturally) but not dashed lines around them.

The reason might be that TeX doesn't have native mechanisms for drawing dashed/dotted lines, only solid ones. So to create dashed lines one needs to calculate the number of dashes and draw each one.

LaTeX's built-in picture environment can do this, but I never learned too much about it. TikZ can do it:

\documentclass{article}
\usepackage{environ}
\usepackage{tikz}

\NewEnviron{elaboration}{
\par
\begin{tikzpicture}
\node[rectangle,minimum width=0.9\textwidth] (m) {\begin{minipage}{0.85\textwidth}\BODY\end{minipage}};
\draw[dashed] (m.south west) rectangle (m.north east);
\end{tikzpicture}
}

\begin{document}
My Elaboration
\begin{elaboration}
{My item list is shown below:}
\begin{itemize}
\item dummy item
\item dummy item 2
\item dummy item 3
\end{itemize}
\end{elaboration}

\end{document}


There is probably an improvement to the TikZ code that can be made by using a decoration to draw the shape's border, thereby reducing it to one line of TikZ. But since all you want is a dashed line this will do the trick. You can play around with the settings to get the separation you want or make it customizable.

-

\documentclass{article}
\usepackage{arydshln,paralist}
\makeatletter
\newenvironment{elaboration}[1]
{\par\tabular{:p{\linewidth}:}\hdashline
\rule{0pt}{4ex}#1\\
\compactitem}
{\endcompactitem\\\hdashline\endtabular}
\makeatother

\begin{document}

\begin{elaboration}{My item list is shown bellow:}
\item dummy item
\item dummy item 2
\item dummy item 3
\end{elaboration}

\end{document}

-
\linewidth may be too large. – Leo Liu Feb 17 '11 at 13:38
then it may be reduced ... – Herbert Feb 17 '11 at 13:40
Yes. And I like this solution, comparing with ones using tikz or pstricks. – Leo Liu Feb 17 '11 at 13:53
When you have a big hammer, everything looks like a nail. And TikZ is a huge hammer. :-) I agree that non-TikZ solutions are useful too, but if you're already using it for something else in the document you can avoid loading another package. – Matthew Leingang Feb 23 '11 at 13:15

Here a solution based on my adjustbox package and dashbox which provides \dbox and \dashbox. These work like \fbox and \framebox but with dashed lines.

\documentclass{article}
\usepackage{dashbox}

\begin{document}
\noindent
My Elaboration

My item list is shown below:
\begin{itemize}
\item dummy item
\item dummy item 2
\item Verbatim: \verb+\section+
\end{itemize}

\end{document}

-

mdframed may be a better solution. It allowes page breaks. (PSTricks is needed.)

\documentclass{article}
\usepackage[style=3]{mdframed}
\mdfsetup{pstrickssetting={linestyle=dashed}}

\begin{document}

\begin{mdframed}
Foobaz
\begin{itemize}
\item foo
\item bar
\end{itemize}
\end{mdframed}

\end{document}

-
Now you can use style=1. Please see tex.stackexchange.com/questions/26896/… – Marco Daniel Aug 29 '11 at 18:15
@Marco: Thank you for your update. – Leo Liu Aug 30 '11 at 5:42

I agree with all what Matthew said. One additional package which should be noted here is framed. However in your special case it won't help you more than fancybox.

However, note that TikZ goes to some trouble to render the node content as a box, not as a macro argument. Therefore the { } can be replaced by \bgroup and \egroup and these then placed into the \begin and \end of a normal environment, respectively. There is then no need for environ, which doesn't support verbatim or similar content.

Based on Matthew's code:

\documentclass{article}
\usepackage{tikz}

\newenvironment{elaboration}{%
\par
\begin{tikzpicture}
\node[rectangle,minimum width=0.9\textwidth] (m)
\bgroup\begin{minipage}{0.85\textwidth}%
}{%
\end{minipage}\egroup;
\draw[dashed] (m.south west) rectangle (m.north east);
\end{tikzpicture}
}

\begin{document}
My Elaboration
\begin{elaboration}
My item list is shown below:
\begin{itemize}
\item dummy item
\item dummy item 2
\item Other TikZ code: \tikz \draw (0,0) circle (10pt);
\item Verbatim: \verb+\section+
\end{itemize}
\end{elaboration}

\end{document}


Personally I might also put an \ignorespaces and an \unskip around the content to get rid of any spaces.

-
The reason for formatting as \begin and \end is the English grammar. For example, "Happy End" translated to English is "Happy Ending". – Hendrik Vogt Feb 17 '11 at 14:00
@Hendrik: Thanks for pointing that out. I should have known better. But I really didn't realized that "Happy End" is Denglish. – Martin Scharrer Feb 17 '11 at 14:07
As it seems that I can't teach you any TeX, I'm switching to teach you some English :-) (Not that I'm professional, but I like teaching ...) – Hendrik Vogt Feb 17 '11 at 14:12

Here, there is another solution using the package tcolorbox. The created dashed box is breakable:

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

\newtcolorbox{elaboration}[2][]{%
enhanced,breakable,frame hidden,interior hidden,
arc=0pt,outer arc=0pt,borderline={0.4pt}{0pt}{dashed},
before upper={#2\begin{itemize}},after upper={\end{itemize}}
}

\begin{document}
\noindent My Elaboration
\begin{elaboration}{My item list is shown below:}
\item dummy item
\item dummy item 2
\item dummy item 3
\end{elaboration}

\begin{elaboration}{My item list is shown below:}
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\item dummy item
\end{elaboration}

\end{document}


-