使用 mathmode 在註解中插入 Maple 程式碼,使用 tcolorbox 套件建立

使用 mathmode 在註解中插入 Maple 程式碼,使用 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]%

\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

如果我正確理解你的問題,你希望擁有關鍵字procend 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}

在此輸入影像描述

相關內容