20

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!

Hendrik Vogt
  • 37,935
Weslei
  • 4,015
  • 8
  • 32
  • 37

6 Answers6

11

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}

sample code output

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.

Matthew Leingang
  • 44,937
  • 14
  • 131
  • 195
8

without loading large packages:

\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}
David Carlisle
  • 757,742
8

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{adjustbox}
\usepackage{dashbox}

\begin{document}
\noindent
My Elaboration

\begin{adjustbox}{minipage=0.85\textwidth,precode=\dbox}
My item list is shown below:
\begin{itemize}
  \item dummy item
  \item dummy item 2
  \item Verbatim: \verb+\section+
\end{itemize}
\end{adjustbox}

\end{document}
Martin Scharrer
  • 262,582
4

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}
Leo Liu
  • 77,365
3

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}

enter image description here enter image description here

0

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.

Martin Scharrer
  • 262,582
  • 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
  • 1
    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