C# 的 Listings 包

C# 的 Listings 包

我打算使用该包listings将 C# 代码包含在 LaTeX 文档中。该包支持很多语言,但 C# 完全不支持。是否有扩展可以添加对该语言的支持?或者我应该在哪里添加 C#(版本 4.0)关键字以使其突出显示?请注意,对 Java/C++ 的支持不够,因为这些语言中没有引入一些关键字:ref、out、var 等。

答案1

listings软件包支持它(参见手册,第 12 页)。您的文档可能如下所示:

\documentclass{scrartcl}
\usepackage{listings}
\usepackage{xcolor}

\lstdefinestyle{sharpc}{language=[Sharp]C, frame=lr, rulecolor=\color{blue!80!black}}

\begin{document}

\lstset{style=sharpc}
\begin{lstlisting}
Your c# code here
\end{lstlisting}

\end{document}

由于我不懂 C#,我也不知道的语言定义是否listings对你来说足够,但它提供了morekeywords关键内容,因此你可以添加你缺少的内容

答案2

使用它来更改你想要的参数

\usepackage{listings}
\usepackage{color}
\lstloadlanguages{C,C++,csh,Java}

\definecolor{red}{rgb}{0.6,0,0} 
\definecolor{blue}{rgb}{0,0,0.6}
\definecolor{green}{rgb}{0,0.8,0}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\lstset{
language=csh,
basicstyle=\footnotesize\ttfamily,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
tabsize=2,
extendedchars=true,
breaklines=true,
frame=b,
stringstyle=\color{blue}\ttfamily,
showspaces=false,
showtabs=false,
xleftmargin=17pt,
framexleftmargin=17pt,
framexrightmargin=5pt,
framexbottommargin=4pt,
commentstyle=\color{green},
morecomment=[l]{//}, %use comment-line-style!
morecomment=[s]{/*}{*/}, %for multiline comments
showstringspaces=false,
morekeywords={ abstract, event, new, struct,
as, explicit, null, switch,
base, extern, object, this,
bool, false, operator, throw,
break, finally, out, true,
byte, fixed, override, try,
case, float, params, typeof,
catch, for, private, uint,
char, foreach, protected, ulong,
checked, goto, public, unchecked,
class, if, readonly, unsafe,
const, implicit, ref, ushort,
continue, in, return, using,
decimal, int, sbyte, virtual,
default, interface, sealed, volatile,
delegate, internal, short, void,
do, is, sizeof, while,
double, lock, stackalloc,
else, long, static,
enum, namespace, string},
keywordstyle=\color{cyan},
identifierstyle=\color{red},
backgroundcolor=\color{cloudwhite},
}

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{blue}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

进而:

\begin{lstlisting}[language={[Sharp]C}, caption={C\# exaple}, label={Script}]

%some code here

\end{lstlisting}

相关内容