The problem with the endmarks in amsthm
is a well known issue; the following remark is from the documentation:
Placement of the QED symbol can be problematic if the last part of a
proof
environment is a displayed equation or list environment or
something of that nature. In that case put a \qedhere
command at the
place where the QED symbol should appear...
So one option is to manually put \qedhere
:
\begin{myexample}[This is an example 2]
This is an another example...
\begin{itemize}
\item And the symbol is on the correct line!\qedhere
\end{itemize}
\end{myexample}
However, you said you don't want to manually use \qedhere
, then I am afraid that using amsthm
is not the best choice. One option would be to use ntheorem
as the back-end for thmtools
instead of amsthm
, since the former solves the problem of setting endmarks of theorem-like environments automatically at the right positions (even if the environment ends with a displaymath or list environment).
Now, you have to make a choice: either you stick to amsthm
and accept having to manually put \qedhere
under some circumstances, or you switch to ntheorem
; perhaps Theorem packages: which to use, which conflict? can help you to make a decision.
Here's your example but now using ntheorem
:
\documentclass{book}
\usepackage[thmmarks]{ntheorem}
\usepackage{thmtools}
\declaretheoremstyle[
headpunct={},
qed=\ensuremath{\triangle},
bodyfont=\normalfont
]{myexstyle}
\declaretheorem[style=break]{myexample}
\begin{document}
\begin{myexample}[This is an example 1]
This is an example...
And the symbol is on the correct line!
\end{myexample}
\begin{myexample}[This is an example 2]
This is an another example...
\begin{itemize}
\item And the symbol is on the correct line!
\end{itemize}
\end{myexample}
\end{document}
As you can see, the note for the title appears boldfaced; unfortunately, the notefont
key for thmtools
is not supported with ntheorem
, so if you want to change the note font, you will have to add the following to the preamble:
\makeatletter
\renewtheoremstyle{break}%
{\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont
##1\ ##2\theorem@separator}\hbox{\strut}}}]}%
{\item[\rlap{\vbox{\hbox{\hskip\labelsep \theorem@headerfont
##1\ ##2\ {\normalfont(##3)}\theorem@separator}\hbox{\strut}}}]}
\makeatother
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}
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}
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}
Best Answer
One possible solution, at least for requests 1, 2 3, and 5, using
mdframed
: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 thecaption
orcapt-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 likemdframed
.