是否可以使用两个以 % 开头的命令并以不同的方式着色(包括 %)?

是否可以使用两个以 % 开头的命令并以不同的方式着色(包括 %)?

我想要%macro深蓝色和%put浅蓝色的 SAS 命令,%包括标志。

是否可以?

\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=2.5cm}

\usepackage{upquote}% per avere gli apici dritti nelle stringhe
\usepackage{relsize}

\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}

\usepackage{listings}

\newcommand{\stilestringa}{\color{blue!40!red}\ttfamily}
\newcommand{\stilecomandoa}{\color{blue!60!black}\bfseries}
\newcommand{\stilecomandob}{\color{blue!80!cyan}}
\newcommand{\stilecommenti}{\color[RGB]{77,159,53}\ttfamily}
\newcommand{\stilewarnings}{\color[RGB]{93,138,168}\ttfamily}
\newcommand{\stileerrori}{\color{red}\ttfamily}

\newcommand{\sasinline}[1]{\lstinline[style=saslst, basicstyle=\ttfamily]!#1!}

\lstdefinestyle{saslst}{               
    columns=flexible,
    language=SAS,
    escapechar=|, 
    deletekeywords={SET, KEEP, LIBNAME, OBS, FIRSTOBS, DROP, IF, THEN,
        ELSE, RENAME, WHERE, PUT, DO, OR, AND, NOT, END, LENGTH,
        LABEL, OUTPUT},
    morekeywords=[1]{quit, proc, let, macro,  mend },
    morekeywords=[2]{access, obs, firstobs,  append, sort, sql,
        transpose, compare, in, when, otherwise, select, 
        format, informat, attrib,
        set, keep, libname, drop, if, then, else, rename, where, put, do,
        or, and, not, end, length,  label, output, lenght},
    morecomment=[f]{*}, 
    morecomment=[s]{/*}{*/}, 
    morecomment=[n]{/*}{*/}, 
    morestring=[b]{"},
    otherkeywords={$,\&,\%},
    morekeywords = [3]{\&,\%},
    morekeywords = [4]{$},
    keywordstyle={[1]\stilecomandoa},
    keywordstyle={[2]\stilecomandob},
    keywordstyle={[3]\stilecomandoa},
    keywordstyle={[4]},
    commentstyle=\stilecommenti,
    stringstyle=\stilestringa,
    showstringspaces=false, 
    keepspaces=true, 
    sensitive=false,
    breaklines=true,
    breakindent=0pt,
    aboveskip=-4pt plus 2pt minus 2pt,
    belowskip=-4pt plus 2pt minus 2pt,
    inputencoding=utf8/latin1
}

\newtcblisting{sas}{%
    listing only,
    enhanced,
    opacityfill=0,
    left=0mm,
    boxsep=0pt,
    boxrule=0pt,
    listing engine=listings,
    listing options={style=saslst,            
        basicstyle=\relsize{0}\ttfamily
        }
}

\begin{document} 

I would like to have \sasinline{\%macro} colored like \sasinline{data} and \sasinline{\%put} like \sasinline{set}

\begin{sas}
data
%macro
set
%put
\end{sas}

\end{document}

在此处输入图片描述

答案1

您可以将 \%put 和 \%macro 添加为它们自己的关键字,方法是将 \% 设为字母:(我使用了红色和绿色来使不同的颜色更加明显。):

\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=2.5cm}

\usepackage{upquote}% per avere gli apici dritti nelle stringhe
\usepackage{relsize}

\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}

\usepackage{listings}

\newcommand{\stilestringa}{\color{blue!40!red}\ttfamily}
\newcommand{\stilecomandoa}{\color{green}}
\newcommand{\stilecomandob}{\color{red}}
\newcommand{\stilecommenti}{\color[RGB]{77,159,53}\ttfamily}
\newcommand{\stilewarnings}{\color[RGB]{93,138,168}\ttfamily}
\newcommand{\stileerrori}{\color{red}\ttfamily}

\newcommand{\sasinline}[1]{\lstinline[style=saslst, basicstyle=\ttfamily]!#1!}

\lstdefinestyle{saslst}{
    columns=flexible,
    language=SAS,
    escapechar=|,
    alsoletter={\%},
    deletekeywords={SET, KEEP, LIBNAME, OBS, FIRSTOBS, DROP, IF, THEN,
        ELSE, RENAME, WHERE, PUT, DO, OR, AND, NOT, END, LENGTH,
        LABEL, OUTPUT},
    morekeywords=[1]{quit, proc, let, macro,  mend, \%macro },
    morekeywords=[2]{access, obs, firstobs,  append, sort, sql,
        transpose, compare, in, when, otherwise, select,
        format, informat, attrib,
        set, keep, libname, drop, if, then, else, rename, where, put,\%put, do,
        or, and, not, end, length,  label, output, lenght},
    morecomment=[f]{*},
    morecomment=[s]{/*}{*/},
    morecomment=[n]{/*}{*/},
    morestring=[b]{"},
    otherkeywords={$,\&},
    morekeywords = [3]{\&},
    morekeywords = [4]{$},
    keywordstyle={[1]\stilecomandoa},
    keywordstyle={[2]\stilecomandob},
    keywordstyle={[3]\stilecomandoa},
    keywordstyle={[4]},
    commentstyle=\stilecommenti,
    stringstyle=\stilestringa,
    showstringspaces=false,
    keepspaces=true,
    sensitive=false,
    breaklines=true,
    breakindent=0pt,
    aboveskip=-4pt plus 2pt minus 2pt,
    belowskip=-4pt plus 2pt minus 2pt,
    inputencoding=utf8/latin1
}

\newtcblisting{sas}{%
    listing only,
    enhanced,
    opacityfill=0,
    left=0mm,
    boxsep=0pt,
    boxrule=0pt,
    listing engine=listings,
    listing options={style=saslst,
        basicstyle=\relsize{0}\ttfamily
        }
}

\begin{document}

I would like to have \sasinline{\%macro} colored like \sasinline{data} and \sasinline{\%put} like \sasinline{set}

\begin{sas}
data
%macro
set
%put
\end{sas}

\end{document}

在此处输入图片描述

相关内容