Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I define an environment named "example" so that:

  1. can be broken over pages
  2. has a horizontal line above "Example \chap.\no" (e.g. Example 4.10), and vertical line that is spread to the whole content of the example and ends to a square symbol close to the text or formula
  3. tables, figures, and other floats can be inserted in the environment
  4. the last line of this environment can be text or formula; If it is a formula, the square symbol (QED) should be inserted close to it (the same but not the next line)
  5. has a bold horizontal line above "Example 4.8". The line has to be as long as "Example 4.8" length; so it should not have a fixed length.
  6. the bottom horizontal line should come to near of the rightmost word at the last line. For example, in the image below, the rightmost word at the last line, is "5685.", so we want to draw the bottom horizontal line (and consequently the QED symbol), just near to this word.

Just like the following image

EDIT: conditions 5 and 6 added enter image description here

share|improve this question
4  
The mdframed package would certainly help here. –  Peter Grill Jan 24 '13 at 21:36
1  
It has serious flaws. for example, floats (figures, ...) can not be inserted in a 'mdframed' environment –  smh Jan 24 '13 at 21:43
1  
@smh It is true that floats can not go in mdframed environments, but I wouldn't say that it has 'serious flaws'. It does more than any other framing package available. Putting a float in a frame is very trick- putting a frame in a float is much easier –  cmhughes Jan 24 '13 at 21:45
7  
I don't think it makes sense to put a float into a framed environment, if latex floated the table what would you do with the frame? simply use a tabular environment with no surrounding table environment (the caption package will let you still use a caption) –  David Carlisle Jan 24 '13 at 22:18
2  
If the "float" is needed just to provide a caption with "Table 1" (or what number is needed), then \captionof of the caption package is what you're looking for. –  egreg Feb 2 '13 at 22:41

2 Answers 2

One possible solution, at least for requests 1, 2 3, and 5, using mdframed:

\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{booktabs}
\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{calc}
\usepackage{lipsum}

\newcounter{exa}[section]
\renewcommand\theexa{\thesection.\arabic{exa}}

\newlength\mylen% to hols the width of the title

\newmdenv[
  settings={\global\refstepcounter{exa}
            \settowidth\mylen{\bfseries\color{cyan}Example~\theexa}},
  leftline=false,
  linecolor=cyan,
  middlelinewidth=1pt,
  innertopmargin=30pt,
  innerbottommargin=10pt,
  innerleftmargin=0pt,
  skipbelow=1.3\topsep,
  singleextra={
    \path node[font=\bfseries\color{cyan},anchor=west,yshift=-12pt,inner sep=0pt] at (O|-P) {Example~\theexa};
    \draw[ultra thick,cyan] ([yshift=.5\pgflinewidth]O|-P) -- ++(\mylen,0pt);
    \draw[line width=2pt,white] (O) -- ($(P|-O)+(-2cm,0pt)$) node (A){};
    \node[anchor=south west,fill=cyan,inner sep=3pt,outer sep=0pt] at (A) {};
  },
  firstextra={
    \path node[font=\bfseries\color{cyan},anchor=west,yshift=-12pt,inner sep=0pt] at (O|-P) {Example~\theexa};
    \draw[ultra thick,cyan] ([yshift=.5\pgflinewidth]O|-P) -- ++(\mylen,0pt);
  },
  secondextra={
    \draw[line width=2pt,white] (O) -- ($(P|-O)+(-2cm,0pt)$) node (A){};
    \node[anchor=south west,fill=cyan,inner sep=3pt,outer sep=0pt] at (A) {};
  }
]{example}

\begin{document}

\section{Test Section}
\begin{example}
\lipsum[4]
\begin{center}
\captionof{table}{This is a table inside a \texttt{mdframed} environment}
\begin{tabular}{ccc}
\toprule
column1a & column2a & column3a \\
column1a & column2a & column3a \\
column1a & column2a & column3a \\
\bottomrule
\end{tabular}
\end{center}
\end{example}
\lipsum*[2-3]
\setcounter{exa}{23}% just for the example
\begin{example}
\lipsum*[1-4]
\begin{center}
\includegraphics[width=6cm]{ctanlion}
\captionof{figure}{This is a figure inside a \texttt{mdframed} environment}
\end{center}
\lipsum*[5-6]
\end{example}
\lipsum*[2]

\end{document}

enter image description here

Floats won't be allowed inside the mdframed environment; as has been mentioned in comments, this is not a restriction; in fact, it wouldn't make much sense. However, as the example demonstrates, one can still have tabular material and images with captions, using the \captionof from the caption or capt-of packages.

CTAN lion drawing by Duane Bibby.


SMH: Due to a reference issue, I edited the code.


Another approach is the suggest package tcolorbox. With the newest version of the package you can use the following code. It has the same limitation like mdframed.

\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage{mwe}%load graphix,lipsum,...
\usepackage{xcolor}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\tcbuselibrary{breakable}

\newcounter{example}[section]
\renewcommand\theexample{\thesection.\arabic{example}}

\tcbset{examplebox/.style={%
 title={Example~\refstepcounter{example}\theexample},
 breakable,
 leftrule=0pt,
 arc=0pt,
 colback=white,
 colframe=cyan,
 enhanced,
 colbacktitle=white,
 coltitle=cyan,
 titlerule=0pt,
 enlarge left by=-4mm,
 width=\linewidth+4mm,
 enlarge top by=2pt,
 overlay unbroken={\draw[cyan,line width=2pt] (frame.north west)++(0,0.25mm) --++(4cm,0pt);
                      \draw[white,line width=10mm] (frame.south west) --++(8cm,0pt) node (P) {};
                      \fill[cyan] (P) rectangle ++(6pt,6pt) ;},%
 overlay first={\draw[cyan,line width=2pt] (frame.north west)++(0,1pt) --++(4cm,0pt);},%
 overlay last={\draw[white,line width=10mm] (frame.south west) --++(8cm,0pt) node (P) {};
                       \fill[cyan] (P) rectangle ++(6pt,6pt) ;},%
 }%
}

\newenvironment{example}{%
  \tcolorbox[examplebox]}%
 {\endtcolorbox}

\begin{document}

\section{Test Section}

\begin{example}
\lipsum[4]
\begin{center}
\captionof{table}{This is a table inside a \texttt{tcolorbox} environment}
\begin{tabular}{ccc}
\toprule
column1a & column2a & column3a \\
column1a & column2a & column3a \\
column1a & column2a & column3a \\
\bottomrule
\end{tabular}
\end{center}
\end{example}
\lipsum*[2-3]
\begin{example}
\lipsum*[1-4]
\begin{center}
\includegraphics[width=6cm]{example-image-a}
\captionof{figure}{This is a figure inside a \texttt{tcolorbox} environment}
\end{center}
\lipsum*[5-6]
\end{example}
\lipsum*[2]

\end{document}
share|improve this answer
1  
+1 but there are two defects on the bottom frame. Please zoom in. –  Please don't touch Apr 13 '13 at 3:39
    
@Karl'sstudents do you mean the alignment of the little blue square? –  Gonzalo Medina Apr 13 '13 at 3:40
    
Please see this screenshot. –  Please don't touch Apr 13 '13 at 3:42
    
@Karl'sstudents I think now it's fixed, but I'd like to be sure (I can only zoom to 400%). Could you please let me know if there's still a problem with the alignment? –  Gonzalo Medina Apr 13 '13 at 4:05
    
No problem now. Thank you! –  Please don't touch Apr 13 '13 at 4:30

I don't like unanswered questions

related to request 1:

Can be done by packages like:

  • mdframed
  • tcolorbox

alphabetical order

related to request 2:

Can be done by packages like:

  • mdframed
  • tcolorbox

alphabetical order

related to request 3:

You can't use floating objects inside frame environments! You can use tables and figures inside such environments. Please note these environments can't be broken by any of the listed packages above.

related to request 4:

Maybe it's possible, but I have no idea.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.