9

how can i define a new Example environment like the image shown in the following?

this environment should have the following attributes:

  • endmarks must be positioned at the last line, whether it includes text or formula; but in formula mode, the red square must be positioned at left side.
  • if the word Example and its red sub-line arrive at the end of page, and there was no more space to include the body of example, the whole example must be inserted at next page (the section, subsection, ... have this property by default).

enter image description here

smh
  • 1,271
  • 2
    IMHO, having the end-marks in different positions introduces inconsistencies in your document. I would suggest you to use just one positioning option for the end-marks. – Gonzalo Medina Dec 13 '12 at 01:52
  • Could you show what you have so far? Positioning the end-marks conditionally will be tricky- the algorithm to get the correct when they are on just one side is pretty long- see the ntheorem package for details – cmhughes Dec 13 '12 at 02:18
  • @smh I've updated my answer providing some code to achieve what you want. – Gonzalo Medina Dec 14 '12 at 21:40
  • Just out of curiosity about the spell checking: What PDF viewer are you using here? – bluenote10 Feb 13 '15 at 17:06

1 Answers1

15

This is just a partial answer (for a complete answer, go to the last exampe). I used the thmtools package as a front-end for amsthm to define a theorem-like structure.

The use of

  \interlinepenalty 10000

in postheadhook prevents pagebreaks as requested.

\documentclass{book}
\usepackage{xcolor}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{thmtools}
\usepackage{lipsum}

\colorlet{myred}{red!80!black}

\declaretheoremstyle[
spaceabove=\topsep, spacebelow=\topsep,
headfont=\normalfont\color{myred},
notefont=\mdseries\color{myred}, notebraces={(}{)},
bodyfont=\normalfont,
postheadspace=\newline,
headpunct=,
numberwithin=chapter,
postheadhook=\leavevmode%
  \interlinepenalty 10000%
  \vskip-1.3\baselineskip%
  \noindent{\color{myred}\rule{\textwidth}{1pt}}%
  \interlinepenalty 10000%
  \vskip0.3\baselineskip\noindent,
qed=\textcolor{myred}{$\blacksquare$}
]{mystyle}
\declaretheorem[style=mystyle]{example}

\renewcommand\theexample{\thechapter-\arabic{example}}

\begin{document}

\chapter{Test Chapter}
%\lipsum[1-3]
\begin{example}
\lipsum*[2]
\end{example}

\end{document}

enter image description here

The only thing that remains unsolved is the positioning of the end-mark when the last line in the environment belongs to a displayed math expression. Quite frankly I wouldn't know how to conditionally change the end-mark placement.

Here's another possibility using this time the mdframed package; the end-mark is provided using the \xqed macro defined in the document linked in the page Theorems in AMS-LaTeX of the AMS-FAQ.:

\documentclass{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{amssymb}
\usepackage{lipsum}
\usepackage{etoolbox}

\newcommand{\xqed}[1]{%
  \leavevmode\unskip\penalty9999 \hbox{}\nobreak\hfill
  \quad\hbox{\ensuremath{#1}}}

\AtBeginEnvironment{example}{\stepcounter{example}}
\AtEndEnvironment{example}{\xqed{\textcolor{myred}{\blacksquare}}}

\colorlet{myred}{red!80!black}

\newmdenv[
  hidealllines=true,
  frametitle={Example~\theexample},
  frametitlerule=true,
  frametitlerulecolor=myred,
  frametitlerulewidth=2pt,
  frametitlefont=\bfseries\color{myred},
  innerleftmargin=0pt,
  innerrightmargin=0pt
]{example}

\newcounter{example}[chapter]
\renewcommand\theexample{\thechapter-\arabic{example}}

\begin{document}

\chapter{Test Chapter}
\begin{example}
\lipsum*[2]
\end{example}

\end{document}

enter image description here

Here's now a possible complete solution; instead of conditionally redefining the behaviour of the end-marks (which could be really difficult), the user can manually select the lexample environment which behaves like example, but placing the end-mark to the left:

\documentclass{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{etoolbox}

\newlength\mylen

\newcommand{\xxqed}[1]{%
  \settowidth\mylen{$#1$}%
  \vskip-2\baselineskip\hspace*{-\mylen}\llap{\hbox{\ensuremath{#1}}}%
}

\newcommand{\xqed}[1]{%
  \leavevmode\unskip\penalty9999 \hbox{}\nobreak\hfill
  \quad\hbox{\ensuremath{#1}}}

\AtBeginEnvironment{example}{\stepcounter{example}}
\AtEndEnvironment{example}{\xqed{\textcolor{myred}{\blacksquare}}}

\AtBeginEnvironment{lexample}{\stepcounter{example}}
\AtEndEnvironment{lexample}{\xxqed{\textcolor{myred}{\blacksquare}}}

\colorlet{myred}{red!80!black}

\newmdenv[
  hidealllines=true,
  frametitle={Example~\theexample},
  frametitlerule=true,
  frametitlerulecolor=myred,
  frametitlerulewidth=2pt,
  frametitlefont=\bfseries\color{myred},
  innerleftmargin=0pt,
  innerrightmargin=0pt,
  skipabove=.6\topskip,
  skipbelow=.6\topskip
]{lexample}

\newmdenv[
  hidealllines=true,
  frametitle={Example~\theexample},
  frametitlerule=true,
  frametitlerulecolor=myred,
  frametitlerulewidth=2pt,
  frametitlefont=\bfseries\color{myred},
  innerleftmargin=0pt,
  innerrightmargin=0pt,
  skipabove=.6\topskip,
  skipbelow=.6\topskip
]{example}

\newcounter{example}[chapter]
\renewcommand\theexample{\thechapter-\arabic{example}}

\newcommand\Text{% just to generate text for the example
Quisque ullamcorper placerat ipsum. Cras nibh. Morbi vel justo vitae lacus
tincidunt ultrices. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In
hac habitasse platea dictumst. Integer tempus convallis augue. Etiam facilisis.
Nunc elementum fermentum wisi. Aenean placerat. Ut imperdiet, enim sed
gravida sollicitudin, felis odio placerat quam, ac pulvinar elit purus eget enim.
Nunc vitae tortor. Proin tempus nibh sit amet nisl.
}

\begin{document}

\chapter{Test Chapter}
\begin{lexample}
\Text
\begin{align}
a &= b \\
&= c 
\end{align}
\end{lexample}
\Text
\begin{example}
\Text
\end{example}

\end{document}

enter image description here

Moriambar
  • 11,466
Gonzalo Medina
  • 505,128
  • 1
    Initially, I deleted this answer since it was not complete, but since no other answers have been provided, perhaps this one can help. – Gonzalo Medina Dec 13 '12 at 23:15
  • 1
    Actually my only idea is, that the human being aka the author must decide manually, whether the endmark has to be left or right. This could either be done with two very similar environments or better with a key driven variant as in keycommand. And how to get the square in ltr mode to left of the last line before the last part of the equation!?!? Something like \llap{\xqed\hspace*{\linewidth}} (with calc: \linewidth minus width of \xqed)? – Speravir Dec 14 '12 at 01:00
  • @Speravir yes; I've implemented your suggestion in my last example code; now the user selects which environment to use: example (with end-mark to the right) or lexample (with end-mark to the left). – Gonzalo Medina Dec 14 '12 at 21:40
  • 1
    @GonzaloMedina -- it looks like something has gone wrong here; the gap between text and the align is larger than i'm used to seeing it, especially since there's such tight spacing between the end of it and the following text, and the end marker looks like it might be positioned a hair above that last baseline. (ams classes default to put display numbers on the left, so there's no conflict with a right-aligned "tombstone".) – barbara beeton Dec 14 '12 at 22:05
  • @barbarabeeton you're right about the larger gap; I'll have to check that. Thanks for pointing that out. – Gonzalo Medina Dec 14 '12 at 22:09
  • @barbarabeeton I did some tests, suppressing the lexample environment and the gap remains the same, so I think the behaviour is the standard one. Do you get a different result suppressing the environment? – Gonzalo Medina Dec 14 '12 at 22:12
  • @barbarabeeton I added some spacing (\topskip) above and below the environments to improve the layout; I hope things look better now. – Gonzalo Medina Dec 14 '12 at 22:26
  • i've just tried a much simpler test, with input text (not \lipsum} before and after the same align as in your example. there's a much larger space above the align when \lipsum is used. so i think the problem is in the way \lipsum terminates -- possibly a \par. as we know, that would have this effect. (i don't have the current lipsum package, using the one from 2010, but i suspect this is something that wouldn't have changed unless someone reported it before, and i don't remember seeing that mentioned anywhere.) – barbara beeton Dec 14 '12 at 22:28
  • @GonzaloMedina -- with \topskip at least it looks more balanced. but really spacier than i think is ideal. but see my previous comment re \lipsum. – barbara beeton Dec 14 '12 at 22:31
  • @barbarabeeton I changed to 0.6\topskip and it looks better (I hope so); I also dropped lipsum and used some custom text instead. – Gonzalo Medina Dec 14 '12 at 22:37
  • yes, better. i have to leave now; i'll check tomorrow. – barbara beeton Dec 14 '12 at 22:53
  • How can you add a similar finishing line to the example? – Oliver Amundsen Dec 28 '15 at 20:51