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与后面的中括号放在一起,是因为显示效果有问题,还是要放在前面才正常。

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