参见此代码:
\documentclass[svgnames,12pt]{report}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{tcolorbox}
\tcbuselibrary{listings,skins,theorems}
\lstdefinelanguage{Maple}%
{aboveskip={0pt},belowskip={0pt},basicstyle=\bfseries,
morekeywords={and,assuming,break,by,catch,description,do,done,%
elif,else,end,error,export,fi,finally,for,from,global,if,%
implies,in,intersect,local,minus,mod,module,next,not,od,%
option,options,or,proc,quit,read,return,save,stop,subset,then,%
to,try,union,use,uses,while,xor},%
sensitive=true,%
morecomment=[l]\#,%
morestring=[b]",%
morestring=[d]"%
}[keywords,comments,strings]%
\makeatletter
\tcbset{every listing line/.code={%
\def\lst@NewLine{%
\ifx\lst@OutputBox\@gobble\else
\par\noindent \hbox{}#1%
\fi
\global\advance\lst@newlines\m@ne
\lst@newlinetrue}}}
\makeatother
\newtcblisting{code}[1]{
colback=white,
skin=bicolor,
colbacklower=white,
colupper=red!40!black,
collower=blue,
frame style={draw=white,left color=white,right color=white},
fontupper=\bfseries,
fontlower=\bfseries,
width=\linewidth,
boxrule=0mm,
outer arc=1mm,arc=1mm,
leftupper=0cm,leftlower=0cm,rightupper=0cm,rightlower=0cm,
top=0mm,bottom=0mm,middle=0mm,
center lower,
nobeforeafter,
listing and comment,
ams nodisplayskip lower,
% comment={#1},
comment={\begin{align*}#1\end{align*}},
every listing line={\textcolor{red!40!black}{\ttfamily> }}
}
\begin{document}
\begin{code}{h := proc(t) t * (t-1) end proc}
h := proc(t) t*(t-1) end proc;
\end{code}
\end{document}
输出为:
我该如何改变它直到得到这个输出?
我的意思是代码环境,不在 \begin{code}{} ... \end{code} 中。
提前致谢。
答案1
如果我正确理解了你的问题,你想要在数学环境中自动检测关键字proc
和;这里, 。我不认为这是不可能的。end proc
align*
您可以在数学环境中手动标记关键字,也可以将带有一些数学内容的转义序列插入代码中。这在使用 的文档listings
中有所记录。listings
mathescape
第一个变体更接近您现有的代码。我删除了定义every listing line
,因为它是 的一部分tcolorbox 2.72 (2014/03/18)
,但您可以将其保留在旧tcolorbox
版本中。数学代码中手动选择关键字的修改示例是:
\documentclass[svgnames,12pt]{report}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{tcolorbox}
\tcbuselibrary{listings,skins,theorems}
\lstdefinelanguage{Maple}%
{aboveskip={0pt},belowskip={0pt},basicstyle=\bfseries,
morekeywords={and,assuming,break,by,catch,description,do,done,%
elif,else,end,error,export,fi,finally,for,from,global,if,%
implies,in,intersect,local,minus,mod,module,next,not,od,%
option,options,or,proc,quit,read,return,save,stop,subset,then,%
to,try,union,use,uses,while,xor},%
sensitive=true,%
morecomment=[l]\#,%
morestring=[b]",%
morestring=[d]"%
}[keywords,comments,strings]%
\newtcblisting{code}[1]{
listing and comment,
blank,nobeforeafter,
colupper=red!40!black,collower=blue,
fontupper=\bfseries,fontlower=\bfseries,
center lower,
listing options={language=Maple},
ams nodisplayskip lower,
comment={\begin{align*}#1\end{align*}},
every listing line={\textcolor{red!40!black}{\ttfamily> }}
}
\begin{document}
\begin{code}{h := \text{\bfseries proc}(t) t * (t-1)\,\text{\bfseries end proc}}
h := proc(t) t*(t-1) end proc;
\end{code}
\end{document}