社区,
我想让我的 C#listings
语法像在 Visual Studio 中一样突出显示,但目前我还没有得到它。
这是 Visual Studio 语法高亮的图像:
这是我的代码:
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\usepackage{courier}
\lstloadlanguages{% Check Dokumentation for further languages ...
%[Visual]Basic
%Pascal
C,
C++,
csh,
%XML
%HTML
Java
}
\definecolor{red}{rgb}{0.6,0,0} % for strings
\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, % Standardschrift
numbers=left, % Ort der Zeilennummern
numberstyle=\tiny, % Stil der Zeilennummern
%stepnumber=2, % Abstand zwischen den Zeilennummern
numbersep=5pt, % Abstand der Nummern zum Text
tabsize=2, % Groesse von Tabs
extendedchars=true, %
breaklines=true, % Zeilen werden Umgebrochen
%keywordstyle=\color{red}\bfseries,
frame=b,
%keywordstyle=[1]\textbf, % Stil der Keywords
% keywordstyle=[2]\textbf, %
% keywordstyle=[3]\textbf, %
% keywordstyle=[4]\textbf, \sqrt{\sqrt{}} %
stringstyle=\color{blue}\ttfamily, % Farbe der String
showspaces=false, % Leerzeichen anzeigen ?
showtabs=true, % Tabs anzeigen ?
xleftmargin=17pt,
framexleftmargin=17pt,
framexrightmargin=5pt,
framexbottommargin=4pt,
commentstyle=\color{blue},
morecomment=[s][\color{green}]{//}{},
%backgroundcolor=\color{grey},
showstringspaces=false, % Leerzeichen in Strings anzeigen ?
%morekeywords={__global__} % CUDA specific keywords
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},% list your attributes here
keywordstyle=\color{cyan},
identifierstyle=\color{red},
}
%\DeclareCaptionFont{blue}{\color{blue}}
%\captionsetup[lstlisting]{singlelinecheck=false, labelfont={blue}, textfont={blue}}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{8}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}
\lstset{ % keyword style
language=csh,
}
\begin{document}
\begin{lstlisting}[label=some-code,caption=Some Code]
using System.Diagnostics;
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Factorization;
namespace App
{
public partial class Form_Main : Form
{
private void saveObjectToTemp()
{
//path to the tempfolder of this application.
string path = Path.GetTempPath() + @"\temperatureapp";
//check if the folder "temperatureapp" exist in the local temp folder (if not create it)
bool folderExist = System.IO.Directory.Exists(path);
if (!folderExist)
System.IO.Directory.CreateDirectory(path);
}
private void saveObjectToTemp(object chart, string chartname)
{
//path to the tempfolder of this application.
string path = Path.GetTempPath() + @"\temperatureapp";
//check if the folder "temperatureapp" exist in the local temp folder (if not create it)
bool folderExist = System.IO.Directory.Exists(path);
if (!folderExist)
System.IO.Directory.CreateDirectory(path);
Chart tmpChart = (Chart)chart;
tmpChart.SaveImage(path + @"\" + chartname, ChartImageFormat.Png);
}
}
}
\end{lstlisting}
\end{document}
关键字未正确识别和着色。注释也未正确识别和着色。(C# 中的注释行以“//”开头)
我将非常感激任何提示!
答案1
您可以尝试我的解决方案(我猜是我在这个主页上的某个地方找到的):
\setmonofont{Consolas} %to be used with XeLaTeX or LuaLaTeX
\definecolor{bluekeywords}{rgb}{0,0,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{redstrings}{rgb}{0.64,0.08,0.08}
\definecolor{xmlcomments}{rgb}{0.5,0.5,0.5}
\definecolor{types}{rgb}{0.17,0.57,0.68}
\usepackage{listings}
\lstset{language=[Sharp]C,
captionpos=b,
%numbers=left, %Nummerierung
%numberstyle=\tiny, % kleine Zeilennummern
frame=lines, % Oberhalb und unterhalb des Listings ist eine Linie
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
commentstyle=\color{greencomments},
morekeywords={partial, var, value, get, set},
keywordstyle=\color{bluekeywords},
stringstyle=\color{redstrings},
basicstyle=\ttfamily\small,
}
与以下产品配合使用:
\begin{lstlisting}[caption=a test for a C$^\sharp$ code, label=lst:test]
\end{lstlisting}
关键字工作正常。但是,有些东西到现在还不起作用:
#region
未设置为关键字。尝试过但没有成功。- 所有亮蓝色的东西(物体)都无法识别,这将是一个难点。也许有一个解决方案,只需识别例如
Math.
或其他常用路径即可。由于大多数对象都是由用户定义的,因此需要大量手动操作。 - 字符串中的引号与所选字体不相符
这里有可以改进的地方(但我没有时间去做):
- 设置
///
,如果之后没有出现则<...>
显示为灰色(它应该保持绿色)。 XML 注释本身应该保持绿色。<.../>
//
- 查找对象并将其打印为浅蓝色。例如
private enum Object
,,public class Object : Object
但这些都是关键字。private virtual Object
private virtual double
- 查找属性并将其打印为浅蓝色。例如
[Serializable, CLSCompliant(false)]
。 - 记住找到的对象并在整个文档中以相同的方式处理它们。
- 可以手动定义对象或识别 MSDN 对象,例如
Math.
到目前为止看起来像这样:
答案2
您的代码中存在几个问题。首先,我删除了所有无效代码和您的注释,以便指出我的更改:
您的morecomment
定义
morecomment=[s][\color{green}]{//}{}
应该是morecomment=[l][\color{green}]{//}
\DeclareCaptionFormat{listing}{\colorbox{8}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
我不知道颜色名称8
。- 改为blue
附加内容\lstset{ language=csh }
覆盖了大部分格式。
\documentclass{article}
\usepackage{color}
\usepackage{listings}
%\usepackage{courier} %caused problems for me
\lstloadlanguages{% Check Dokumentation for further languages ...
C,
C++,
csh,
Java
}
\definecolor{red}{rgb}{0.6,0,0} % for strings
\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=true,
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},
}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{blue}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}} %changed \colorbox{8} to \colorbox{blue} cause 8 is not a color!
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}
%remove that, it caused most of the problems
%\lstset{ % keyword style
% language=csh,
% }
\begin{document}
\begin{lstlisting}[label=some-code,caption=Some Code]
using System.Diagnostics;
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Factorization;
namespace App
{
/*
Multiline comment
*/
public partial class Form_Main : Form
{
private void saveObjectToTemp()
{
//path to the tempfolder of this application.
string path = Path.GetTempPath() + @"\temperatureapp";
//check if the folder "temperatureapp" exist in the local temp folder (if not create it)
bool folderExist = System.IO.Directory.Exists(path);
if (!folderExist)
System.IO.Directory.CreateDirectory(path);
}
private void saveObjectToTemp(object chart, string chartname)
{
//path to the tempfolder of this application.
string path = Path.GetTempPath() + @"\temperatureapp";
//check if the folder "temperatureapp" exist in the local temp folder (if not create it)
bool folderExist = System.IO.Directory.Exists(path);
if (!folderExist)
System.IO.Directory.CreateDirectory(path);
Chart tmpChart = (Chart)chart;
tmpChart.SaveImage(path + @"\" + chartname, ChartImageFormat.Png);
}
}
}
\end{lstlisting}
\end{document}
答案3
由于我无法找到真正令人满意的 Latex 中 C# 列表解决方案,我花了几个小时创建了一套非常完美的列表设置。我知道这个帖子真的很老了,但我希望它能帮助一些自己寻找解决方案的人。享受我的示例,如果我能帮到你,请留下赞 :)
我从 Visual Studio for C# 重新创建了暗黑模式样式。这是一个示例:
这是文档标题中 \begin{document} 之前的代码:
\usepackage{color}
\usepackage{cascadia-code}
\usepackage{listings}
%Color scheme in Visual Studio darkmode
\definecolor{black}{cmyk}{0,0,0,1}
\definecolor{white}{cmyk}{0,0,0,0}
\definecolor{darkgrey}{cmyk}{0,0,0,0.97} %background in Visual Studio
\definecolor{green}{cmyk}{0.6,0,0.84,0} %comments in Visual Studio
\definecolor{blue}{cmyk}{0.65,0.33,0,0.05} %keywords in Visual Studio
\definecolor{rose}{cmyk}{0,0.26,0.38,0} %strings in Visual Studio
\definecolor{lavender}{cmyk}{0,0.42,0,0.1} %if/else,switch/case etc. in Visual Studio
\definecolor{lightblue}{cmyk}{0.35,0,0,0} %local variables in Visual Studio
\definecolor{aqua}{cmyk}{0.65,0,0.23,0} %class types in Visual Studio
\definecolor{lightgreen}{cmyk}{0.12,0,0.33,0.05} %enumerations and methods in Visual Studio
\lstset{
language=[Sharp]C,
numbers=left,
numberstyle=\tiny\color{black},
rulesep=10pt,
xleftmargin=12pt,
framexleftmargin=-2pt,
framexrightmargin=-5pt,
showtabs=false,
showspaces=false,
showstringspaces=true,
breaklines=true,
breakatwhitespace=true,
backgroundcolor=\color{darkgrey},
rulecolor=\color{darkgrey},
commentstyle=\color{green},
morecomment=[s][\color{green}]{/*+}{*/},
morecomment=[s][\color{green}]{/*-}{*/},
basicstyle=\ttfamily\footnotesize\color{white},
stringstyle=\color{rose},
frame=trbl,
framesep=5pt,
numbersep=7pt,
belowcaptionskip=1\baselineskip,
%**********************
%ADD YOUR KEYWORDS HERE
%**********************
%-------------------------------------------
%keywords like string, int, false, true ...
keywordstyle=\color{blue},
morekeywords={partial, var, value, get, set},
%-------------------------------------------
%keywords like if/else, switch/cas ...
emphstyle=\color{lavender},
emph={if, else, return, throw, switch, case},
%-------------------------------------------
%Collection of your local and global variables
emphstyle={[2]\color{lightblue}},
emph={[2]someLocalVariable, someGlobalVariable},
%-------------------------------------------
%Collection of your class types
emphstyle={[3]\color{aqua}},
emph={[3]SomeOwnClassType},
%-------------------------------------------
%Collection of your methods
emphstyle={[4]\color{lightgreen}},
emph={[4]ToLower},
}
%Optional
%Define a new floating environment to be able to generate an own list of codes in the document
\usepackage{newfloat}
\DeclareFloatingEnvironment[fileext=frm,placement={!ht},name=Codeexample]{myCode}
这是文档里面的代码示例的实现:
\begin{myCode} %Only when using the optional floating environment
\begin{lstlisting}
//This is a test for Latex listing C# with Visual Studio darkmode style
var someLocalVariable = "This is a test string";
if(someLocalVariable.ToLower() == "this is a test string")
{
someGlobalVariable = new SomeOwnClassType();
}
\end{lstlisting}
\captionsetup{width=.95\textwidth}
\caption[This is the caption in the list of code examples]{This is the caption below the code example in the document}
\label{list:CodeExample}
\end{myCode}%Only when using the optional floating environment
这是代码示例清单的实现:
\begin{addmargin}[-1.5em]{0cm}
\listof{myCode}{\hspace{1.1em}List of Codes}
\end{addmargin}