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!

6 Answers 6

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.

0
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}
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}
4
  • \linewidth may be too large.
    – Leo Liu
    Feb 17, 2011 at 13:38
  • then it may be reduced ...
    – user2478
    Feb 17, 2011 at 13:40
  • Yes. And I like this solution, comparing with ones using tikz or pstricks.
    – Leo Liu
    Feb 17, 2011 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. Feb 23, 2011 at 13:15
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}
1
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.

3
  • The reason for formatting as \begin and \end is the English grammar. For example, "Happy End" translated to English is "Happy Ending". Feb 17, 2011 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. Feb 17, 2011 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 ...) Feb 17, 2011 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .