The framed
or mdframed
packages allow you to put text in frames. Here's a way to do what you want with mdframed
.
\documentclass{article}
\usepackage{caption}
\usepackage{listings}
\lstset{extendedchars=true,
tabsize=3,
frame=none,
showspaces=false,
boxpos=c,
float=h,
escapechar=\%
}
\usepackage[style=1]{mdframed}
\newcounter{boxctr}
\DeclareCaptionType{boxx}
\usepackage{needspace}
\newenvironment{Boxx}[2]{%
\captionsetup{type=boxx}
\begin{mdframed}[%
linewidth=1pt,
roundcorner=10pt,%
leftmargin=.05\textwidth,
rightmargin=.05\textwidth,
skipabove=.7\baselineskip]%
\refstepcounter{boxctr}%
\label{#1}%
\gdef\boxxcaption{\caption{#2}}
\needspace{2\baselineskip}
}{%
\end{mdframed}%
\boxxcaption%
}
\usepackage{lipsum}
\begin{document}
This is a reference to Box~\ref{box:one}!
\vspace{1.5cm}
\lipsum[1-4]
\begin{Boxx}{box:one}{This is a caption}
\begin{enumerate}
\item One
\item Two
\item Three
\end{enumerate}
\end{Boxx}
\lipsum[1]
\begin{Boxx}{box:two}{This is also a caption}
\begin{lstlisting}
Also works with listings
\foo is escaped!
\end{lstlisting}
\end{Boxx}
\lipsum[2]
\end{document}
Boxes take two extra arguments: a label and a caption, just like the environment defined in the question.
They use the caption
package's captioning magic. So this should make doing lists of boxes easier. The label should stick with the bottom of the box now. This might lead to some ugly whitespace, but other than making the boxes float, this is probably as good as it gets.
If you don't load tocloft
your problem goes away.
But since you have loaded it, you are probably going to use its features. Be aware that tocloft
changes the way the titles of Table of Contents (ToC), List of Figures (LoF) and List of Tables (LoT) are typeset, in respect to the standard way defined by the document class.
If you want to use your custom titles and tocloft
together, you have to inform tocloft
about that. This can be done by passing the option titles
when loading it, that is, use
\usepackage[titles]{tocloft}
Also, as a side note, you have loaded the tocbibind
package with the options notlot
and notlof
, which causes the LoF and the LoT not to be included in the ToC, but later you issue the following commands
\phantomsection
\addcontentsline{toc}{chapter}{List of Tables}
\phantomsection
\addcontentsline{toc}{chapter}{List of Figures}
which instead add the LoF and the LoT in the ToC.
Since the tocbibind
is specifically designed to do these things, simply load it as
\usepackage[nottoc]{tocbibind}
and delete the above lines from your document.
One more thing... Instead of using the commands
\hoffset = 0pt
\oddsidemargin = 36pt
\evensidemargin = 36pt
\marginparwidth = 0pt
\marginparsep = 0pt
\textwidth = 418pt
...
to modify your page layout, I suggest you to have a look at the geometry
package, which is specifically designed to handle the page layout.
Best Answer
I assume you are using a documentclass like
book
which implicitly contains the optionopenright
as default, which in turn makes every chapter (and everything like a chapter, i.e.\listoffigures
) start on a right page.The command
\cleardoublepage
which is used internally will insert an empty page if neccessary to start a new right page.On
\nopagebreak[0]
\nopagebreak
is not a very useful command in LaTeX. Don't use it; it will make your document markup inelegant.Incidentally, the version
\nopagebreak[0]
you've been using has almost no effect at all; it might even create a new page breaking possibility where none used to be before.But even the strongest version
\nopagebreak
(without option) would have no effect here as it can't counteract the explicit\cleardoublepage
used to insert the blank page.No blank pages
If you never want a blank page inserted in front of a chapter(-like thing) to make it start in a right page, the answer is easy: Add the option
openany
to the book class like this:Avoid blank page only for
\listoffigures
/\listoftables
If you want to avoid the inserting of blank pages only for
\listoffigures
and\listoftables
, you can achieve this by locally redefining\cleardoublepage
to just mean\clearpage
: