我有以下可以正常工作的 LaTeX 代码:
\documentclass[12pt,a4paper]{article}
\usepackage[a5paper]{geometry}% just for the example
\usepackage[utf8x]{inputenc}
\usepackage{listings}
\usepackage[labelfont=bf, font=small]{caption}
\usepackage[framemethod=tikz]{mdframed}
\lstset{
breaklines=true,
breakatwhitespace=false,
xleftmargin=1em,
frame=single,
numbers=left,
numbersep=5pt,
}
\newcommand\mylstcaption{}
\surroundwithmdframed[
hidealllines=true,
middleextra={
\node[anchor=west] at (O|-P)
{\lstlistingname~\thelstlisting\ (Cont.):~\mylstcaption};},
secondextra={
\node[anchor=west] at (O|-P)
{\lstlistingname~\thelstlisting\ (Cont.):~\mylstcaption};},
splittopskip=2\baselineskip
]{lstlisting}
\begin{document}
\renewcommand\mylstcaption{Example listing of code}
\begin{lstlisting}[language=C, caption=\mylstcaption, label=lst:c1]
struct safe_buffer {
struct list_head node;
/* original request */
void *ptr;
size_t size;
int direction;
/* safe buffer info */
struct dmabounce_pool *pool;
void *safe;
dma_addr_t safe_dma_addr;
};
struct dmabounce_pool {
unsigned long size;
struct dma_pool *pool;
#ifdef STATS
unsigned long allocs;
#endif
};
struct dmabounce_device_info {
struct device *dev;
struct list_head safe_buffers;
#ifdef STATS
unsigned long total_allocs;
unsigned long map_op_count;
unsigned long bounce_count;
int attr_res;
#endif
struct dmabounce_pool small;
struct dmabounce_pool large;
rwlock_t lock;
int (*needs_bounce)(struct device *, dma_addr_t, size_t);
};
#ifdef STATS
static ssize_t dmabounce_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
return sprintf(buf, "%lu %lu %lu %lu %lu %lu\n",
device_info->small.allocs,
device_info->large.allocs,
device_info->total_allocs - device_info->small.allocs -
device_info->large.allocs,
device_info->total_allocs,
device_info->map_op_count,
device_info->bounce_count);
}
static DEVICE_ATTR(dmabounce_stats, 0400, dmabounce_show, NULL);
#endif
\end{lstlisting}
\end{document}
部分结果如下:
第 1 页
第2页
现在我想要有类似的标题(相同的字体大小等),但目前情况并非如此。
答案1
标题标签的样式
列表标题的标题样式不适用于“继续”列表的标题。只需替换即可实现
{\captionfont{\captionlabelfont\lstlistingname~\thelstlisting\ (Cont.):}~\mylstcaption};},
为了
{\lstlistingname~\thelstlisting\ (Cont.):~\mylstcaption};},
在您的代码中(出现两次)。
标题居中
您的后续列表的标题未居中。这可以通过对 定义的节点坐标执行一些算术运算来实现mdframed
。根据mdframed
源代码,O
和分别对应于边界框P
左下角和右上角的坐标。因此对应于左上角。您想要构造位于点和中间的点(参见下图);这就是标题应该位于的位置。tikz
O|-P
(O|-P)
(P)
O|-P point of interest P
*-----------------*----------------*
| |
| |
| |
| |
| |
| |
| |
| |
*----------------------------------*
O
要构建该中点,您可以使用tikz
名为的库calc
,如下所示:
($.5*(O|-P) + .5*(P)$)
最后,删除anchor
不再需要的规范。
笔记:请记住,如果您caption
稍后更改设置(例如,如果您决定将列表标题放在左侧),您还必须相应地更改继续列表标题的位置。更自动化的方法是检查标题对齐方式并相应地放置“继续”列表的标题。也许我会这样做...稍后。
输出的屏幕截图
第 1 页:
第2页:
代码
\documentclass[12pt,a4paper]{article}
\usepackage[a5paper]{geometry}% just for the example
\usepackage[utf8x]{inputenc}
\usepackage{listings}
\usepackage[labelfont=bf, font=small]{caption}
\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{calc} % needed for arithmetic operations on coordinates
\lstset{
breaklines=true,
breakatwhitespace=false,
xleftmargin=1em,
frame=single,
numbers=left,
numbersep=5pt,
}
\newcommand\mylstcaption{}
\surroundwithmdframed[
hidealllines=true,
middleextra={
\node at ($.5*(O|-P) + .5*(P)$)
{\captionfont{\captionlabelfont\lstlistingname~\thelstlisting\ (Cont.):}~\mylstcaption};},
secondextra={
\node at ($.5*(O|-P) + .5*(P)$)
{\captionfont{\captionlabelfont\lstlistingname~\thelstlisting\ (Cont.):}~\mylstcaption};},
splittopskip=2\baselineskip
]{lstlisting}
\begin{document}
\renewcommand\mylstcaption{Example listing of code}
\begin{lstlisting}[language=C, caption=\mylstcaption, label=lst:c1]
struct safe_buffer {
struct list_head node;
/* original request */
void *ptr;
size_t size;
int direction;
/* safe buffer info */
struct dmabounce_pool *pool;
void *safe;
dma_addr_t safe_dma_addr;
};
struct dmabounce_pool {
unsigned long size;
struct dma_pool *pool;
#ifdef STATS
unsigned long allocs;
#endif
};
struct dmabounce_device_info {
struct device *dev;
struct list_head safe_buffers;
#ifdef STATS
unsigned long total_allocs;
unsigned long map_op_count;
unsigned long bounce_count;
int attr_res;
#endif
struct dmabounce_pool small;
struct dmabounce_pool large;
rwlock_t lock;
int (*needs_bounce)(struct device *, dma_addr_t, size_t);
};
#ifdef STATS
static ssize_t dmabounce_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct dmabounce_device_info *device_info = dev->archdata.dmabounce;
return sprintf(buf, "%lu %lu %lu %lu %lu %lu\n",
device_info->small.allocs,
device_info->large.allocs,
device_info->total_allocs - device_info->small.allocs -
device_info->large.allocs,
device_info->total_allocs,
device_info->map_op_count,
device_info->bounce_count);
}
static DEVICE_ATTR(dmabounce_stats, 0400, dmabounce_show, NULL);
#endif
\end{lstlisting}
\end{document}