在 lstinputlisting 中加入被忽略的特殊關鍵字

在 lstinputlisting 中加入被忽略的特殊關鍵字

我想知道如何新增特殊關鍵字被忽略是在包\lstinputlisting中使用的listings

螢幕截圖顯示了我的問題。我正在寫ROOT資料分析框架的教學文件(https://root.cern.ch)它有自己的 C++ 解釋器,使用者提示符號root [n]如下所示。

在此輸入影像描述

在大多數情況下,使用lstlisting環境和c++選項對於 ROOT 解釋器來說效果很好。但當root (cont'ed, cancel with .@)在解釋器上列印時,該訊息中的單引號被視為C字串的開頭,因此代碼的顏色變為綠色。

\begin{lstlisting}[language=c++,breaklines=true]
root [0] TH2D* h2 = new TH2D("h2", "2D Gaussian Distribution;#it{x};#it{y};Entries", 100, -10, 10, 100, -10, 10)
(TH2D *) 0x7fe8c3615eb0
root [1] const Double_t kSigma = 2.
(const Double_t) 2.00000
root [2] for(Int_t i = 0; i < 100000; i++){
root (cont'ed, cancel with .@) [3] Double_t x = gRandom->Gaus(0, kSigma);
root (cont'ed, cancel with .@) [4] Double_t y = gRandom->Gaus(0, kSigma);
root (cont'ed, cancel with .@) [5] h2->Fill(x, y);
root (cont'ed, cancel with .@) [6] }
root [7] TCanvas* can = new TCanvas("can", "can", 600, 600)
(TCanvas *) 0x7fe8c359a9a0
root [8] h2->Draw("colz")
\end{lstlisting}

我想知道如何命令listings包忽略這個單引號。有什麼方法可以將字串添加cont'ed到“忽略”列表之類的內容中嗎?

答案1

我找到了一個使用 的暫定解決方案mathescape,但仍在尋找更好的解決方案。

\newcommand{\conted}{\texttt{cont'ed}} % for ROOT 6 prompt

\begin{lstlisting}[language=c++,breaklines=true,mathescape]
root [0] TH2D* h2 = new TH2D("h2", "2D Gaussian Distribution;#it{x};#it{y};Entries", 100, -10, 10, 100, -10, 10)
(TH2D *) 0x7fe8c3615eb0
root [1] const Double_t kSigma = 2.
(const Double_t) 2.00000
root [2] for(Int_t i = 0; i < 100000; i++){
root ($\conted$, cancel with .@) [3] Double_t x = gRandom->Gaus(0, kSigma);
root ($\conted$, cancel with .@) [4] Double_t y = gRandom->Gaus(0, kSigma);
root ($\conted$, cancel with .@) [5] h2->Fill(x, y);
root ($\conted$, cancel with .@) [6] }
root [7] TCanvas* can = new TCanvas("can", "can", 600, 600)
(TCanvas *) 0x7fe8c359a9a0
root [8] h2->Draw("colz")
\end{lstlisting}

相關內容