为什么我必须使用样式解决方法来在列表中设置 C# 语言?
为什么
\begin{lstlisting}[language=[Sharp]C]
不能被LaTeX处理?
\documentclass[a4paper,12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{inconsolata}
\usepackage{xcolor}
\definecolor{bluekeywords}{rgb}{0.0,0.0,0.9}
\definecolor{greencomments}{rgb}{0,0.6,0}
\definecolor{redidentifiers}{rgb}{0.9,0,0}
\definecolor{orangestrings}{rgb}{0.6,0.3,0}
% Formatting for any language
\lstset{columns=flexible,
basicstyle=\ttfamily,
commentstyle=\color{greencomments},
keywordstyle=\color{bluekeywords},
stringstyle=\color{orangestrings},
identifierstyle=\color{redidentifiers},
showstringspaces=false
}
\lstdefinestyle{CSstyle} {
language=[Sharp]C
}
\begin{document}
Java
\begin{lstlisting}[language=Java]
// HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
\end{lstlisting}
and C\#
%\begin{lstlisting}[language=[Sharp]C]
\begin{lstlisting}[style=CSstyle]
// Hello1.cs
public class Hello1 {
public static void Main() {
System.Console.WriteLine("Hello, World");
}
}
\end{lstlisting}
look similar.
Python looks different.
\begin{lstlisting}[language=Python]
# Hello.py
s = 'Hello, World'
print s
\end{lstlisting}
\end{document}
答案1
手册listings
第 12 页(第 2.3 节末尾附近)对此进行了解释:
language=[77]Fortran
在可选参数内不起作用。
如果在可选参数内使用带有可选参数的值,则必须在值周围加上括号。在这种情况下,请写入language={[77]Fortran}
选择 Fortran 77。
所以
\begin{lstlisting}[language={[Sharp]C}]