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}
Here's an approach I've used to get something working. Not the neatest...
I copied the @tufte@float environment definition and the \@tufte@float@fullwidth command definition into my preamble, renaming them as @TC@float and \@TC@float@fullwidth, respectively.
I altered my plate* environment definition to use @TC@float instead of the standard tufte-class figure* environment.
This allows me to alter the width of the caption text box (altering the line \begin{minipage}[\floatalignment]{2\marginparwidth}\hbox{} makes it twice the normal width) and the location by forcing \@TC@float@fullwidth to only use the option I want (commenting out the unwanted options, effectively).
As my \textwidth = 2\marginparwidth, and an option in \@TC@float@fullwidth allows the position to be bottom left, I can have a long caption appear below my figure and in the same space as the body text would normally fill.
MWE:
\documentclass[a4paper,justified,twoside,openright]{tufte-book}
\usepackage{layouts}
\usepackage{showframe}
\geometry{landscape,top=1in,bottom=0.75in,textheight=7in,textwidth=6in,marginparsep=0.5in,marginparwidth=3in}
\usepackage{tikz}
%% Plate environment %%%%%%%%%%%%%%% from http://tex.stackexchange.com/questions/113631/caption-placement-for-new-float-in-tufte-book-class
\newcounter{plate}
\newcommand\platename{Plate}
\newcommand\listplatename{List of Plates}
\makeatletter
\newcommand\listofplates{%
\ifthenelse{\equal{\@tufte@class}{book}}%
{\chapter*{\listplatename}}%
{\section*{\listplatename}}%
% \begin{fullwidth}%
\@starttoc{lop}%
% \end{fullwidth}%
}
\renewcommand\theplate
% {\ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@plate}
{\@arabic\c@plate} % alter numbering to be continuous
\def\fps@plate{tbp}
\def\ftype@plate{1}
\def\ext@plate{lop}
\def\fnum@plate{\platename\nobreakspace\theplate}
\newenvironment{plate}[1][htbp]
{\begin{@tufte@float}[#1]{plate}{}}
{\end{@tufte@float}}
% \newenvironment{plate*}[1][htbp]%
% {\begin{@tufte@float}[#1]{plate}{star}} % need to define a new @tufte@float environment - copy the existing one and alter all the names then change the position of the caption box. Something like @TC@float
% {\end{@tufte@float}}
\newenvironment{plate*}[1][htbp]%
{\begin{@TC@float}[#1]{plate}{star}} % need to define a new @tufte@float environment - copy the existing one and alter all the names then change the position of the caption box. Something like @TC@float
{\end{@TC@float}}
\newcommand{\@tufte@lop@line}[2]{%
% #1 is the figure/table number and its caption text
% #2 is the page number on which the figure/table appears
\leftskip 0.0em
\rightskip 0em
\parfillskip 0em plus 1fil
\parindent 0.0em
\@afterindenttrue
\interlinepenalty\@M
\leavevmode
\@tempdima 2.0em
\advance\leftskip\@tempdima
\null\nobreak\hskip -\leftskip
{#1}\nobreak\qquad\nobreak#2%
\par%
}
\let\l@plate\l@figure
\newenvironment{@TC@float}[3][htbp]%
{% begin @tufte@float
% Should this float be full-width or just text-width?
\ifthenelse{\equal{#3}{star}}%
{\gsetboolean{@tufte@float@star}{true}}%
{\gsetboolean{@tufte@float@star}{false}}%
% Check page side (recto/verso) and store detected value -- can be overriden in environment contents
\@tufte@checkoddpage%
\ifthenelse{\boolean{@tufte@odd@page}}%
{\gsetboolean{@tufte@float@recto}{true}\@tufte@float@debug{Detected page: [recto/odd]}}%
{\gsetboolean{@tufte@float@recto}{false}\@tufte@float@debug{Detected page: [verso/even]}}%
% If the float placement specifier is 'b' and only 'b', then bottom-align the mini-pages, otherwise top-align them.
\renewcommand{\@tufte@fps}{#1}%
\@tufte@float@debug{Allowed positions: [#1]}
\ifthenelse{\equal{#1}{b}\OR\equal{#1}{B}}%
{\renewcommand{\floatalignment}{b}\@tufte@float@debug{Presumed position: [bottom]}}%
{\renewcommand{\floatalignment}{t}\@tufte@float@debug{Presumed position: [top]}}%
% Capture the contents of the \caption and \label commands to use later
\global\let\@tufte@orig@caption\caption%
\global\let\@tufte@orig@label\label%
\renewcommand{\caption}{\optparams{\@tufte@caption}{[][0pt]}}%
\renewcommand{\label}[1]{\@tufte@label{##1}}%
% Handle subfigure package compatibility
\ifthenelse{\boolean{@tufte@packages@subfigure}}{%
% don't move the label while inside a \subfigure or \subtable command
\global\let\label\@tufte@orig@label%
}{}% subfigure package is not loaded
\@tufte@orig@float{#2}[#1]%
\ifthenelse{\boolean{@tufte@float@star}}%
{\setlength{\@tufte@float@contents@width}{\@tufte@fullwidth}}%
{\setlength{\@tufte@float@contents@width}{\textwidth}}%
\begin{lrbox}{\@tufte@figure@box}%
\begin{minipage}[\floatalignment]{\@tufte@float@contents@width}\hbox{}%
}{% end @tufte@float
\par\hbox{}\vspace{-\baselineskip}\ifthenelse{\prevdepth>0}{\vspace{-\prevdepth}}{}% align baselines of boxes
\end{minipage}%
\end{lrbox}%
% build the caption box
\begin{lrbox}{\@tufte@caption@box}%
% EDIT THE WIDTH OF THE CAPTION HERE
\begin{minipage}[\floatalignment]{2\marginparwidth}\hbox{}%
\ifthenelse{\NOT\equal{\@tufte@stored@caption}{}}{\@tufte@orig@caption[\@tufte@stored@shortcaption]{\@tufte@stored@caption}}{}%
\ifthenelse{\NOT\equal{\@tufte@stored@label}{}}{\@tufte@orig@label{\@tufte@stored@label}}{}%
\par\vspace{-\prevdepth}%% TODO: DOUBLE-CHECK FOR SAFETY
\end{minipage}%
\end{lrbox}%
% now typeset the stored boxes
\begin{fullwidth}%
\begin{minipage}[\floatalignment]{\linewidth}%
\ifthenelse{\boolean{@tufte@float@star}}%
{\@TC@float@fullwidth[\@tufte@caption@vertical@offset]{\@tufte@figure@box}{\@tufte@caption@box}}%
{\@TC@float@textwidth[\@tufte@caption@vertical@offset]{\@tufte@figure@box}{\@tufte@caption@box}}%
\end{minipage}%
\end{fullwidth}%
\@tufte@orig@endfloat% end original LaTeX float environment
% output debug info
\ifthenelse{\boolean{@tufte@debug}}{%
\typeout{^^J^^J----------- Tufte-LaTeX float information ----------}%
\ifthenelse{\equal{\@tufte@stored@label}{}}%
{\typeout{Warning: Float unlabeled!}}%
{\typeout{Float label: [\@tufte@stored@label]}}%
\typeout{Page number: [\thepage]}%
\def\MessageBreak{^^J}%
\typeout{\@tufte@float@debug@info}%
\ifthenelse{\boolean{@tufte@symmetric}}%
{\typeout{Symmetric: [true]}}%
{\typeout{Symmetric: [false]}}%
\typeout{----------------------------------------------------^^J^^J}%
}{}%
% reset commands and temp boxes and captions
\gdef\@tufte@float@debug@info{}%
\let\caption\@tufte@orig@caption%
\let\label\@tufte@orig@label%
\begin{lrbox}{\@tufte@figure@box}\hbox{}\end{lrbox}%
\begin{lrbox}{\@tufte@caption@box}\hbox{}\end{lrbox}%
\gdef\@tufte@stored@shortcaption{}%
\gdef\@tufte@stored@caption{}%
\gdef\@tufte@stored@label{}%
\gsetlength{\@tufte@caption@vertical@offset}{0pt}% reset caption offset
}
% COMMENT OUT ALL OPTIONS, THEN SELECT THE OPTION YOU WANT AND PLACE AT SHOWN POINT
%
\newcommand{\@TC@float@fullwidth}[3][0pt]{%
% \ifthenelse{\equal{\floatalignment}{b}}%
% {% place caption above figure
% \ifthenelse{\NOT\boolean{@tufte@symmetric}\OR\boolean{@tufte@float@recto}}%
% {\hfill\smash{\raisebox{#1}{\usebox{#3}}}\par\usebox{#2}\@tufte@float@debug{Caption position: [above right]}}% caption on the right
% {\smash{\raisebox{#1}{\usebox{#3}}}\hfill\par\usebox{#2}\@tufte@float@debug{Caption position: [above left]}}% caption on the left
% }{% place caption below figure
% \ifthenelse{\NOT\boolean{@tufte@symmetric}\OR\boolean{@tufte@float@recto}}%
% {\usebox{#2}\par\hfill\smash{\raisebox{#1}{\usebox{#3}}}\@tufte@float@debug{Caption position: [below right]}}% caption on the right
% {\usebox{#2}\par\smash{\raisebox{#1}{\usebox{#3}}}\hfill\@tufte@float@debug{Caption position: [below left]}}% caption on the left
% }%
% CHOSEN OPTION GOES HERE
\usebox{#2}\par\smash{\raisebox{#1}{\usebox{#3}}}\hfill\@tufte@float@debug{Caption position: [below left]}% caption on the left
}
\makeatother
%% End plate environment %%%%%%%%%%%
\begin{document}
\setuplayouts
\listofplates
\chapter{Plate Environment}
\begin{plate}
\tikz{\path[draw=red,fill=blue] (0,0) rectangle (\textwidth-1cm,\textheight-1cm);}
\caption{Non-starred}
\label{plate:1}
\end{plate}
\begin{plate}
\tikz{\path[draw=red,fill=blue] (0,0) rectangle (\textwidth-1cm,\textheight-1cm);}
\caption[Non-starred - long caption.]{Non-starred - long caption. Lorem ipsum, lorem ipsum,Lorem ipsum, lorem ipsum,Lorem ipsum. Lorem ipsum, lorem ipsum,Lorem ipsum, lorem ipsum,Lorem ipsum. Lorem ipsum, lorem ipsum,Lorem ipsum, lorem ipsum,Lorem ipsum. Lorem ipsum, lorem ipsum,Lorem ipsum, lorem ipsum,Lorem ipsum. Lorem ipsum, lorem ipsum,Lorem ipsum, lorem ipsum,Lorem ipsum.}
\label{plate:2}
\end{plate}
\begin{plate*}
\tikz{\path[draw=red,fill=blue] (0,0) rectangle (\textwidth-1cm,\textheight-1cm);}
\caption{Starred}
\label{plate:3}
\end{plate*}
\begin{plate*}
\tikz{\path[draw=red,fill=blue] (0,0) rectangle (\textwidth-1cm,\textheight-1cm);}
\caption[Starred - long caption.]{Starred - long caption. Lorem ipsum, lorem ipsum,Lorem ipsum, lorem ipsum,Lorem ipsum. Lorem ipsum, lorem ipsum,Lorem ipsum, lorem ipsum,Lorem ipsum. Lorem ipsum, lorem ipsum,Lorem ipsum, lorem ipsum,Lorem ipsum. Lorem ipsum, lorem ipsum,Lorem ipsum, lorem ipsum,Lorem ipsum. Lorem ipsum, lorem ipsum,Lorem ipsum, lorem ipsum,Lorem ipsum.}
\label{plate:4}
\end{plate*}
\clearpage
\begin{figure}
\centering
%\setlayoutscale{0.3}
\currentpage
\drawparametersfalse
\drawpage
\caption{Test}
\end{figure}
%\setlayoutscale{0.3}
%\drawpage
\end{document}
Best Answer
Using
fancybox
package you have tools to define customizedverbatim
environments, such as the one you are requesting. In this case, the trick is to save the wholeverbatim
into a box whose width is the width of its longest line (BVerbatim
environment does this), and then put that box as the argument of a\centerline
command, which basically centers the content between the margins, even if the contents are larger than\textwidth
(allowing these contents protruding both margins the same amount).The following MWE defines a new enviroment
wideverb
which does all this:Note that the contents of
wideverbatim
are not restricted in length. You can have a line which is longer than the page width, and in this case part of its contents would not be visible (but the line will be centered anyway).