我滥用了包含枚举环境的框架列表环境(通过转义到 latex)。这可以很好地编译,但在生成的 pdf 中,列表周围的框架被中断了。
最小工作示例:
\documentclass{report}
\usepackage[english]{babel}
\usepackage{float}
\usepackage[margin=10pt,font=small,labelfont=bf,justification=centering]{caption}
\usepackage{listings}
\renewcommand{\lstlistingname}{Box}
% Setup for listings:
\lstset{extendedchars=true,
tabsize=3,
frame=single,
frameround=tttt,
showspaces=false,
framesep=10pt,
boxpos=c,
float=h,
captionpos=b,
escapechar=\%
}
\lstdefinestyle{Normaltext}{language=,numbers=none,basicstyle=\normalfont}
\lstnewenvironment{textbox}[2]
{%
\centering
\minipage{0.9\textwidth}
\lstset{style=Normaltext,label=#1,caption=#2}
}
{%
\endminipage
\endcenter
}
\begin{document}
\begin{textbox}{box:somelabel}{some caption}
%{Test 1 2 3, here comes the enumeration:
\begin{itemize}
\item item 1
\item item 2
\item item 3
\item item 4
\end{itemize}
}%
\end{textbox}
\end{document}
这会产生类似这样的结果:
有什么想法可以让我避免帧中断?
或者也许有人可以建议一个更好的替代方案,而不是滥用列表将纯乳胶文本放在框架框中,因为这就是我真正想要的。基本上我想要实现的是:
- 我希望文档中包含 3 种类型的“对象”:图形、表格和框。
- 图形有“图形”标签,并在图形列表中列出。
- 表格有“表格”标签,并在表格列表中列出。
- 同样,我希望盒子有“Box”标签并列在盒子列表中(我打算为此使用列表列表)。
- 框可以包含源代码(使用 listings 包)或纯乳胶文本,但无论哪种方式,它们都应该具有相同的圆形框架并位于页面中央并覆盖 90% 的文本宽度(我为此使用了 minipage,正如您在我的代码中所看到的)。
答案1
这framed
或者mdframed
包允许您将文本放入框架中。以下是使用 执行所需操作的一种方法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}
盒子需要两个额外的参数:一个标签和一个标题,就像问题中定义的环境一样。它们使用caption
包的标题魔法。所以这应该使制作盒子列表变得更容易。标签现在应该粘在盒子的底部。这可能会导致一些难看的空白,但除了让盒子浮动之外,这可能是最好的了。
答案2
由于 Seamus 的答案得到了改进,这要感谢 Seamus 和 Axel Sommerfeldt 的额外投入(参见评论中的讨论),我认为值得发布一个单独的合并答案。
\documentclass{report}
\usepackage{color}
\usepackage{xcolor}
\usepackage[style=1]{mdframed}
\usepackage[font=small,labelfont=bf,justification=centering,belowskip=\baselineskip]{caption}
\usepackage[pdftex,bookmarks=true]{hyperref}%pagebackref=true
\usepackage{enumitem}
\usepackage{cleveref} %[nameinlink,noabbrev]
%%Paragraph indentation & spacing
\usepackage{parskip} %to disable paragraph indentation but still keep paragraphs separated (with \parskip)
\setlength{\parskip}{2.75ex plus 0.5ex minus 0.2ex}
\usepackage{listings}
\lstset{extendedchars=true,
tabsize=3,
frame=none,
breaklines=true,
breakautoindent=true,
postbreak=\space,
showspaces=false,
keywordstyle=\color{blue},
commentstyle=\color{red},
stringstyle=\color{gray},
identifierstyle=\color{black},
framesep=0pt,
boxpos=c,
float=h,
aboveskip=0pt,
belowskip=0pt,
escapechar=\%
}
\lstdefinestyle{Java}{language=Java, numbers=none, numberstyle=\tiny ,numbersep=5pt, basicstyle=\linespread{0.85}\scriptsize\ttfamily, showstringspaces=true}
\lstnewenvironment{java}{\lstset{style=Java}}{}
\usepackage{lipsum}
\usepackage{needspace} %\needspace{2\baselineskip}% %instead of \nopagebreak
\DeclareCaptionType[fileext=lob,placement=b,within=chapter]{boxx}[Box][List of Boxes]
\makeatletter
\caption@setbool{needfreeze}{true} %Tell the caption package that we would like to use \caption@freeze and \caption@defrost
\renewenvironment{boxx}
{%
\pagebreak[2] %suggest pagebreak here
\captionsetup{type=boxx,position=b} %skip BELOW the caption
\nopagebreak
\begin{mdframed}[%
linewidth=1pt,
linecolor=black,
innerlinecolor=black,
middlelinecolor=black,
outerlinecolor=black,
roundcorner=10pt,
leftmargin=.05\textwidth,
rightmargin=.05\textwidth,
skipabove=\baselineskip, %skip above the frame
skipbelow=0.25\baselineskip, %skip between the frame and the caption
innerleftmargin=10pt,
innerrightmargin=10pt,
innertopmargin=10pt,
innerbottommargin=10pt]%
\nopagebreak
\caption@freeze %"freeze" caption related commands
}{%
\end{mdframed}%
\nopagebreak
\caption@defrost %typeset caption, if necessary
}
\makeatother
\crefname{boxx}{box}{boxes}
\crefname{req}{requirement}{requirements}
\begin{document}
\chapter{Chapter}
\noindent This is a reference to \cref{box:one}.
\begin{boxx}
\lipsum[2]
\begin{enumerate}[label=\textbf{Req. \arabic*},ref=Req. \arabic*,leftmargin=*]
\item one\label{req:one}
\item two generalised.
\end{enumerate}
\caption[shorter]{This is a box with text}\label{box:one}
\end{boxx}
\noindent \lipsum[1-2]
Some more text, ref to item: \cref{req:one}
\begin{boxx}
\begin{java}
class HelloWorldApp
{
public float pi = 3.14159;
public int i = 0;
public static void main(String[] args)
{
System.out.println("Hello World!"); //Display the string
}
}
\end{java}
\caption{This is a box with Java code}\label{box:two}
\end{boxx}
\noindent Double uppercase reference: \Cref{box:one,box:two}.
\noindent \lipsum[3]
\pagebreak
\listofboxxs
\end{document}