如何设置列表中评论的样式(SAS 语言)?

如何设置列表中评论的样式(SAS 语言)?

前提:我必须使用 ShareLaTeX(或 Overleaf),因为我无法在我使用的计算机上安装任何东西。

我正在编写一个包含大量 SAS 代码片段的文档,我正在使用minted并且对结果非常满意,但我想在预定义的关键字中添加更多关键字。

我看到了这篇文章:如何添加自定义 C++ 关键字以供 Minted 识别?但这个解决方案对我来说太难了,我甚至不知道是否可以在 ShareLaTeX 中做这样的事情。

我尝试了一种解决方法,但它并不方便,因为我必须更正代码,而不是简单地从 SAS 编辑器中剪切和粘贴它。

因此,我决定尝试一下listings。我快完成了,但我还没有找到自己喜欢的\color{green}\ttfamily评论风格。

\documentclass[a4paper,12pt, twoside]{book}
\usepackage{microtype}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}
\geometry{verbose,tmargin=3cm,bmargin=3.5cm,lmargin=4cm,rmargin=3cm,marginparwidth=70pt}
\usepackage{mwe}

\usepackage{minted}
\usemintedstyle{vs}
\newcommand{\saskeyword}[1]{\textcolor{blue}{#1}}
\newenvironment{sasminted}{\VerbatimEnvironment\begin{minted}[escapeinside=||,fontsize=\small,baselinestretch=1, samepage=true]{sas}}
 {\end{minted}}

\usepackage{listings}
\lstset{% 
    basicstyle=\small\ttfamily\bfseries,
    columns=flexible,
    language=SAS,
    keywordstyle=\color{blue}\bfseries,
    morecomment=[s]{/*}{*/},
    morecomment=[s]{*}{;},
    morecomment=[n]{/*}{*/},
    morecomment=[n]{*}{;},
    escapechar=|,
    commentstyle=\color{green}\ttfamily,
    stringstyle=\color[rgb]{0.639,0.082,0.082}\ttfamily,
    showstringspaces=false,
    keepspaces=true,
    sensitive=false,
    otherkeywords={*,/},
}

\newenvironment{saslst}{\VerbatimEnvironment\noindent\begin{minipage}{\linewidth}\begin{lstlisting}}
 {\end{lstlisting}\end{minipage}}


\begin{document}
\noindent Pure \texttt{minted}:
\begin{sasminted}
/* comment */
data pippo; |\emph{within escape chars}|
set  pluto (firstobs=10 obs=14);
keep paperino;
minnie='a string';
* another comment;
run;
\end{sasminted}
This is what I would like to have (without my workaround):
\begin{sasminted}
/* comment */
data pippo; |\emph{within escape chars}|
set  pluto (|\saskeyword{firstobs}|=10 |\saskeyword{obs}|=14); 
keep paperino; 
minnie='a string';
* another comment;
run;
\end{sasminted}

\noindent with \texttt{listing}: 

\begin{lstlisting}
/* comment */
data pippo; |\emph{within escape chars}|
set  pluto (firstobs=10 obs=14);
keep paperino; 
minnie='a string';
* another comment;
run;
\end{lstlisting}
\noindent\texttt{minted} also recognizes when the \texttt{*} has to be put in black or in green:
\begin{sasminted}
/* this with minted */
proc freq data = pippo; 
tables pluto * paperino; 
run;
* another comment;
\end{sasminted}

\noindent I prefer minted highlighting also here: 

\begin{lstlisting}
/* this with listings */
proc freq data = pippo; 
tables pluto * paperino; 
run;
* another comment;
\end{lstlisting}

\end{document}

在此处输入图片描述

答案1

正如评论中所讨论的,问题在于otherkeywordsSAS 语言定义的定义,它将*和都定义/为关键字。这覆盖了它们作为注释标记的用途。

由于没有宏,解决此问题的最简单方法是删除这两个字符,然后deleteotherkeywords覆盖 列表。但由于也用作 SAS 中的乘法运算符,因此单独执行此操作会导致其中带有 的行从 开始显示为注释,因为您已将其定义为分隔注释。otherkeywords****...;

解决这个问题的方法是删除注释的定义,而*只在注释出现在第 1 列时使用[f]注释定义的类型将其定义为注释。

这是一个完整的例子:

\documentclass[a4paper,12pt, twoside]{book}
\usepackage{microtype}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
%\usepackage[italian]{babel}
\usepackage[a4paper]{geometry}
\geometry{verbose,tmargin=3cm,bmargin=3.5cm,lmargin=4cm,rmargin=3cm,marginparwidth=70pt}
\usepackage{mwe}

\usepackage{xcolor}
\usepackage{listings}
\lstset{% 
    basicstyle=\small\ttfamily\bfseries,
    columns=flexible,
    language=SAS,
    keywordstyle=\color{blue}\bfseries,
    commentstyle=\color{green},
    morecomment=[f]{*},
    morecomment=[s]{/*}{*/},
    morecomment=[n]{/*}{*/},  
    escapechar=|,
    otherkeywords={!,!=,~,$,\&,_,<,>=,=<,>},
    stringstyle=\color[rgb]{0.639,0.082,0.082}\ttfamily,
    showstringspaces=false,
    keepspaces=true,
    sensitive=false,
}

\newenvironment{saslst}{\VerbatimEnvironment\noindent\begin{minipage}{\linewidth}\begin{lstlisting}}
 {\end{lstlisting}\end{minipage}}


\begin{document}
\noindent
With \texttt{listings}:

\noindent\begin{minipage}{\linewidth}% I don't want to break my listing across pages
\begin{lstlisting}
/* comment */
data pippo; |\emph{within escape chars}|
set  pluto (firstobs=10 obs=14);
keep paperino; 
pippo = pippo*pippo;
minnie='a string';
* another comment;
run;
\end{lstlisting}
\end{minipage}
\end{document}

代码输出

相关内容