我正在使用该listings
包来标记 C 和汇编代码。遗憾的是缺少列表的标题(命令也是如此caption=...
)。
在文档部分,我使用我的clisting
语句来描述 C 代码和asmlisting
汇编程序(两者都是同一个问题)。
这里是 MWE:
\documentclass[a4paper,10pt]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{times}
\usepackage{graphicx}
\lstnewenvironment{asmlisting}{
\lstset{
language={[x86masm]Assembler},
basicstyle=\ttfamily\small,
breaklines=true,
prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=lines,
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
keywordstyle=\color{blue}\bfseries,
stringstyle=\color{green!50!black},
commentstyle=\color{green}\itshape,
numbers=none,
captionpos=t,
escapeinside={\%*}{*)},
extendedchars=true,
}
}{}
\newcommand{\clistingset}{
\lstset{
language=C,
basicstyle=\ttfamily\small,
breaklines=true,
prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=lines,
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
keywordstyle=\color{blue}\bfseries,
stringstyle=\color{green!50!black},
commentstyle=\color{green}\itshape,
numbers=none,
captionpos=t,
escapeinside={\%*}{*)},}
}
\begin{document}
\section*{Kap. 1}
\subsection*{Aufgabe 1}
\begin{asmlisting}[title=hallo] % this title is not displayed in the comp. doc.
.INCLUDE "usb1287def.inc"
.ORG 0
start:
ldi r16, 4
clr r17
loop:
add r17, r16
cpi r17, 32
brne loop
end:
rjmp end
\end{asmlisting}
\cleardoublepage
\section*{Kap. 14}
\clistingset
% remarked to prevent compilation errors. This title is displayed in the comp. doc.
%\lstinputlisting[title=Aufg1.c]{Ueb14/Aufg1.c}
\end{document}
有人知道我做错了什么吗?
答案1
在下面的 MWE 中您可以看到如何获取标题。
您可以定义自己的样式(clist
)或者您必须重写您的新环境(参见手册第 42 页)。我个人会使用自己的样式,因为我不必记住,我必须在新环境中包含标题而不包含title=
。
梅威瑟:
\RequirePackage{filecontents}
\begin{filecontents*}{HelloWorld.c}
#include <stdio.h>
int main(void)
{
puts("Hallo Welt!");
return 0;
}
\end{filecontents*}
\documentclass[a4paper,10pt]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{listings}
\usepackage{xcolor}
\lstnewenvironment{clisting}[1][]%
{\lstset{
language=C,
title=#1,
basicstyle=\ttfamily\small,
breaklines=true,
prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=lines,
tabsize=2,
showtabs=false,
showspaces=true,
showstringspaces=false,
keywordstyle=\color{blue}\bfseries,
stringstyle=\color{green!50!black},
commentstyle=\color{green}\itshape,
numbers=none,
captionpos=t,
escapeinside={\%*}{*)},
}
}{}
\lstdefinestyle{clist}{%
language=C,
basicstyle=\ttfamily\small,
breaklines=true,
prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=lines,
tabsize=2,
showtabs=false,
showspaces=true,
showstringspaces=false,
keywordstyle=\color{blue}\bfseries,
stringstyle=\color{green!50!black},
commentstyle=\color{green}\itshape,
numbers=none,
captionpos=t,
}
\begin{document}
\lstinputlisting[title=HelloWorld.c]{HelloWorld.c}
\lstinputlisting[%
title=HelloWorld.c
,style=clist
]{HelloWorld.c}
\section*{Kap. 1}
\subsection*{Aufgabe 1}
\begin{clisting}[hallo]
#include <stdio.h>
int main(void)
{
puts("Hallo Welt!");
return 0;
}
\end{clisting}
\end{document}
结果:
答案2
首先,在 后面不能放任何东西\begin{asmlisting}[title=hallo]
,即不允许评论。
此外,像您那样定义新环境asmlisting
是行不通的。
您必须按以下方式定义它:
\lstnewenvironment{asmlisting}[1][]{%
\lstset{
language={[x86masm]Assembler},
basicstyle=\ttfamily\small,
breaklines=true,
prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=lines,
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
keywordstyle=\color{blue}\bfseries,
stringstyle=\color{green!50!black},
commentstyle=\color{green}\itshape,
numbers=none,
captionpos=t,
escapeinside={\%*}{*)},
extendedchars=true,
#1
}
}{}
因此你的修改后的 MWE
\documentclass[a4paper,10pt]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{times}
\usepackage{graphicx}
\lstnewenvironment{asmlisting}[1][]{%
\lstset{
language={[x86masm]Assembler},
basicstyle=\ttfamily\small,
breaklines=true,
prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=lines,
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
keywordstyle=\color{blue}\bfseries,
stringstyle=\color{green!50!black},
commentstyle=\color{green}\itshape,
numbers=none,
captionpos=t,
escapeinside={\%*}{*)},
extendedchars=true,
#1
}
}{}
\newcommand{\clistingset}{
\lstset{
language=C,
basicstyle=\ttfamily\small,
breaklines=true,
prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=lines,
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
keywordstyle=\color{blue}\bfseries,
stringstyle=\color{green!50!black},
commentstyle=\color{green}\itshape,
numbers=none,
captionpos=t,
escapeinside={\%*}{*)},}
}
\begin{document}
\section*{Kap. 1}
\subsection*{Aufgabe 1}
\begin{asmlisting}[title=hallo]
.INCLUDE "usb1287def.inc"
.ORG 0
start:
ldi r16, 4
clr r17
loop:
add r17, r16
cpi r17, 32
brne loop
end:
rjmp end
\end{asmlisting}
\cleardoublepage
\section*{Kap. 14}
\clistingset
% remarked to prevent compilation errors. This title is displayed in the comp. doc.
%\lstinputlisting[title=Aufg1.c]{Ueb14/Aufg1.c}
\end{document}
将产生所需的输出