LaTeX備忘——使用listings宏包顯示google test終端內容

目前我使用的源代碼顯示宏包有minted與listings,前者功能更強大,但需要python方面的支持。最近需要在latex文檔中插入Google Test的終端顯示內容,minted雖然可以自定義語言,但需要使用python,我目前還未學習,故暫時只能使用listings宏包來實現。


操作系統:Ubuntu 20.04

編輯工具:TeXstudio 2.12.22

編譯方式:LuaLaTeX


完整源代碼如下:

% 開源中國,陸巍的博客
\documentclass[oneside]{article}%

% 注意宏包順序,有可能會報錯
\usepackage{ctex}% 中文支持
\usepackage{geometry}% 用於頁面設置
\usepackage[dvipsnames, svgnames, x11names]{xcolor} % 顏色支持
\usepackage{graphics}% 圖形支持
\usepackage[
  colorlinks=true,
  linkcolor=Navy,
  urlcolor=Navy,
  citecolor=Navy,
  anchorcolor=Navy
]{hyperref}
\usepackage{enumerate}% 枚舉支持
\usepackage{tcolorbox}% 支持更好的文本框
\tcbuselibrary{skins, breakable}% 支持文本框跨頁
\usepackage[english]{babel}% 載入美式英語斷字模板
\usepackage{listings}% 傳統的代碼顯示支持

% 設置爲A4紙,邊距適中模式(永中office)
\geometry{%
  width = 210mm,%
  height = 297mm,
  left = 19.1mm,%
  right = 19.1mm,%
  top = 25.4mm,%
  bottom = 25.4mm%
}

\hyphenpenalty = 1000% 斷字設置,值越大,斷字越少。
\setmainfont{Ubuntu Mono}% 設置全局英文字體
\setlength{\parindent}{2em}% 縮進
\setlength{\parskip}{2ex} % 段間距
\setcounter{secnumdepth}{3} % 顯示到第3級section的編號

% 以下兩行命令用於解決LuaLaTeX編譯模式下listings宏包不能正常工作的問題
\newdimen\cht%
\newdimen\cdp%

% listings宏包代碼顯示樣式設置
\lstset{%
  breaklines,% 自動換行
  basicstyle=\small,% 設置字體大小
  frame=single,% 單線框
  numbers=left,% 行號在左邊
  numberstyle=\tiny,% 行號字體大小
  %keywordstyle = \color[RGB]{160, 0, 0},% 關鍵字顏色
  %commentstyle = \color[RGB]{0, 160, 0},% 註釋顏色
  %stringstyle  = \color[RGB]{0, 0, 255},% 字符串顏色
}%

% listings宏包自定義語言
\lstdefinelanguage{googletest} {
  basicstyle=\color{white},
  backgroundcolor=\color{black},
  % 關鍵字指定
  %morekeywords={FAILED,}, keywordstyle=\color{red},classoffset=1,
  %morekeywords={PASSED, RUN}, keywordstyle=\color{green},classoffset=0,
  % 註釋格式
  morecomment = [s][\color{green}]{[-}{-]},
  morecomment = [s][\color{green}]{[=}{=]},
  morecomment = [s][\color{red}]{[\ \ FAILED}{]},
  morecomment = [s][\color{green}]{[\ \ PASSED}{]},
  morecomment = [s][\color{green}]{[\ RUN}{]},
  morecomment = [s][\color{green}]{[\ \ \ \ \ \ \ OK}{]},
}


\begin{document}

\begin{lstlisting}[language=googletest]
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from SoundexEncoding
[ RUN      ] SoundexEncoding.RetainSoleLetterOfOneLetterWord
[       OK ] SoundexEncoding.RetainSoleLetterOfOneLetterWord (0 ms)
[----------] 1 test from SoundexEncoding (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.
\end{lstlisting}

\begin{lstlisting}[language=googletest]
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from SoundexEncoding
[ RUN      ] SoundexEncoding.RetainSoleLetterOfOneLetterWord
/home/starry/tree/lakeside/exercise1/src/soundex_test.cpp:13: Failure
Value of: encoded
Expected: is equal to 0x55b9c3cfd13d pointing to "A"
  Actual: ""
[  FAILED  ] SoundexEncoding.RetainSoleLetterOfOneLetterWord (0 ms)
[----------] 1 test from SoundexEncoding (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] SoundexEncoding.RetainSoleLetterOfOneLetterWord

 1 FAILED TEST
\end{lstlisting}


\end{document}

效果如下: listings宏包顯示google test終端內容示例

說明:

1、之所以不使用morekeywords來表示FAILED、PASSED等內容,是因爲其中的空格會被忽略掉,從而導致不能識別;

2、在morecomment中,“\ “裏面的空格會被保留,空格數量要與實際顯示的數量相等;

3、在處理“OK“顯示內容時,並沒有把OK與後面的中括號放在一起,是因爲顯示效果有問題,還是要放在前面才正常。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章