2

I would like to know how to add a special keyword to be ignored in \lstinputlisting that is used in the listings package.

The screen shot shows my problem. I am writing a tutorial document of the ROOT data analysis framework (https://root.cern.ch) which has its own C++ interpreter with a user prompt of root [n] as shown below.

enter image description here

In most cases, using the lstlisting environment and the c++ option work fine for the ROOT interpreter. But when root (cont'ed, cancel with .@) is printed on the interpreter, the single quotation in this message is regarded as the start of a C string, and thus the color of the code is changed to green.

\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}

I would like to know how to command the listings package to ignore this single quotation. Is there any way to add the string cont'ed to something like "ignore" list?

1 Answer 1

1

I have found a tentative solution that uses mathescape, but still looking for a better solution.

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .