LaTex-listing 环境设置

总览

% 代码块listing设置
\lstdefinestyle{mppl-listing-style}{
    language         = java,                   % 语言类型
    backgroundcolor  = \color{{HTML}{F7F7F7}}, % 背景色
    numbers          = left,              % 行号显示位置
    xleftmargin      = 5em,               % 左边距
    xrightmargin     = 0em,               % 右边距
    aboveskip        = 2em,               % 上方留白
    belowskip        = 0em,               % 下方留白
    frame            = single,            % 代码块边框是单线条
    rulecolor        = \color{black},    % 边框颜色
    frameround       = tttt,              % 边框圆角
    framesep         = 0.19em,            % 边框与代码间距
    rulesepcolor     = \color{black},    % 阴影颜色
    framexleftmargin    = -2em,           % 左边框与代码距离
    framexrightmargin   = -5em,           % 右边框与代码距离
    framexbottommargin  = 2em,            % 下边框与代码距离
    framextopmargin     = 2em,            % 上边框与代码距离
    breaklines          = true,           % 代码超出边界换行
    tabsize             = 4,              % tab缩进
    numberstyle         = \color{red},    % 行号颜色
    keywordstyle        = \color{red},    % 关键字颜色
    identifierstyle     = \color{listing-identifier},   % 变量颜色
    commentstyle        = \color{listing-comment},      % 注释颜色

  basicstyle       = \color{listing-text-color}\small\ttfamily{}\linespread{1.15}, % print whole listing small
  abovecaptionskip = 0em,               % 上标题边距
  belowcaptionskip = 1.0em,             % 下标颜边距
  classoffset      = 0,                 % 类名偏移量
  sensitive        = true,              % 是否区分大小写
  morecomment      = [s][\color{listing-javadoc-comment}]{/**}{*/},     
  stringstyle      = \color{listing-string},
  showstringspaces = false,             % 是否显示字符串空格
  escapeinside     = {/*@}{@*/}, % Allow LaTeX inside these special comments
  literate         =
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\'e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\EUR}}1 {£}{{\pounds}}1 {«}{{\guillemotleft}}1
  {»}{{\guillemotright}}1 {ñ}{{\~n}}1 {Ñ}{{\~N}}1 {¿}{{?`}}1
  {…}{{\ldots}}1 {≥}{{>=}}1 {≤}{{<=}}1 {„}{{\glqq}}1 {“}{{\grqq}}1
  {”}{{''}}1    
}       

backgroundcolor 背景颜色

numbers 代码行号

\lstset{
    numbers             = left,             % 行号靠左
    basicstyle          = \ttfamily,        % 基本代码风格
    keywordstyle        = \bfseries,        % 关键字风格
    commentstyle        = \ttfamily,        % 注释的风格
    frame       = single,                   % 阴影效果
    escapeinside=``,                        % 英文分号中可写入中文
    xleftmargin=2em,xrightmargin=2em, aboveskip=1em,
    breaklines          =   true,
    language            = C,                % 语言选C
} 

\lstset{
    numbers             = right,            % 行号靠左
    basicstyle          = \ttfamily,        % 基本代码风格
    keywordstyle        = \bfseries,        % 关键字风格
    commentstyle        = \ttfamily,        % 注释的风格
    frame       = single,                   % 阴影效果
    escapeinside=``,                        % 英文分号中可写入中文
    xleftmargin=2em,xrightmargin=2em, aboveskip=1em,
    breaklines          =   true,
    language            = C,                % 语言选C
} 

stepnumber 间隔显示行号

\lstset{
    numbers             = right,            % 行号靠左
    stepnumber          = 2,                % 每两行显示一次行号
}

firstnumber 开始行号

  • firstnumber = 10 开始行号为 10
  • firstnumber = last 开始行号为上一段 listing 的结束行号

xleftmargin/xrightmargin/aboveskip/below 距离外部元素距离

设置代码块上下左右的距离,与外部元素的距离,而不是代码与边框的距离。

\lstset{
    basicstyle          = \ttfamily,        % 基本代码风格
    numbers             = left,            % 行号靠左
    keywordstyle        = \bfseries,        % 关键字风格
    commentstyle        = \ttfamily,        % 注释的风格
    frame       = single,                   % 阴影效果
    escapeinside=``,                        % 英文分号中可写入中文
    xleftmargin=0em,xrightmargin=0em, aboveskip=0em,belowskip=0em,
    breaklines          =   true,
    language            = C,                % 语言选C
} 

\lstset{
    basicstyle          = \ttfamily,        % 基本代码风格
    numbers             = left,            % 行号靠左
    keywordstyle        = \bfseries,        % 关键字风格
    commentstyle        = \ttfamily,        % 注释的风格
    frame               = single,                   % 线框
    escapeinside        =``,                        % 英文分号中可写入中文
    xleftmargin         =5em,
    xrightmargin        =0em, 
    aboveskip           =2em,
    belowskip           =0em,
    breaklines          =   true,
    language            = C,                % 语言选C
} 

frame 边框样式

设置边框样式:

  • none:无框
  • single:单线框
  • leftline,topline,rightline,bottomline:上下左右的线
  • ltrb:上面参数的缩写,frame=lr 表示左右有线
  • LTRB:大写表示双线
\lstset{
  frame               = single,        % 线框
} 

\lstset{
  frame               = shadowbox,        % 阴影
} 

\lstset{
  frame               = LR,        % 左右边框双线
} 

rulesepcolor 阴影颜色

\lstset{
  frame               = shadowbox,        % 阴影
  rulesepcolor= \color{ red!20!green!20!blue!20} , % 阴影颜色
} 

rulecolor 边框颜色

\lstset{
    rulecolor           = \color{red},      % 边框颜色
} 

frameround 边框倒角

\lstset{
  frameround          = fftt,        % 边框倒角,f表示尖角,t表示倒角,顺序是第一个字母表示右上角,顺时针
} 

framesep 边框与代码的距离

代码不会移动,动的是边框。

\lstset{
    framesep           = 6em,              % 边框与代码的距离
} 

\lstset{
    framesep           = 6em,              % 边框与代码的距离
} 

framexleftmargin/framexrightmargin/frameytopmargin/frameybottommargin 边框与代码距离

\lstset{
    framexleftmargin    = -2em,           % 左边框与代码距离
    framexrightmargin   = -5em,           % 右边框与代码距离
    framexbottommargin  = 2em,            % 下边框与代码距离
    framextopmargin     = 2em,            % 上边框与代码距离
} 

\lstset{
    framexleftmargin    = 1em,           % 左边框与代码距离
    framexrightmargin   = 1em,           % 右边框与代码距离
    framexbottommargin  = 0em,            % 下边框与代码距离
    framextopmargin     = 0em,            % 上边框与代码距离
} 

breaklines 强制换行

设置代码超长时自动换行:

\lstset{
    breaklines         = false,           % 不换行
} 

\lstset{
    breaklines         = true,           % 不换行
} 

numberstyle/keywordstyle/identifierstyle/commentstyle/commentstyle 行号、关键字、标识符、注释的样式

\definecolor{listing-background}{HTML}{F7F7F7}
\definecolor{listing-rule}{HTML}{B3B2B3}
\definecolor{listing-numbers}{HTML}{B3B2B3}
\definecolor{listing-text-color}{HTML}{000000}
\definecolor{listing-keyword}{HTML}{435489}
\definecolor{listing-identifier}{HTML}{435489}
\definecolor{listing-string}{HTML}{00999A}
\definecolor{listing-comment}{HTML}{8E8E8E}
\definecolor{listing-javadoc-comment}{HTML}{006CA9}

\lstset{
    numberstyle         = \color{listing-numbers},      % 行号颜色
    keywordstyle        = \color{listing-keyword},      % 关键字颜色
    identifierstyle     = \color{listing-identifier},   % 变量颜色
    commentstyle        = \color{listing-comment},      % 注释颜色
}