如果我们使用其他字体而不是 courier,则会产生不必要的空白

如果我们使用其他字体而不是 courier,则会产生不必要的空白

我的代码如下:

\documentclass{book}

\usepackage{xltxtra}
\usepackage{xunicode}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\usepackage{unicode-math}%


\setmonofont[Path = ./Fonts/ ,
UprightFont= Iosevka ,]{Iosevka}

\usepackage{textcomp,xcolor,MNsymbol}
\definecolor{mylightgrey}{rgb}{0.98, 0.98, 0.98}
\definecolor{mydarkgrey}{rgb}{0.3, 0.3, 0.3}

\usepackage{listings}

\lstdefinestyle{myterminalstyle}{%
upquote=true,
basicstyle=\ttfamily\small,
commentstyle=\slshape\footnotesize,
frameround = tttt,
frame = trbl,
backgroundcolor = \color{black!16},
showspaces = false,
showtabs = false,
breaklines=true,xleftmargin=4\p@,xrightmargin=3\p@,aboveskip=13\p@,belowskip=10\p@,
postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\rcurvearrowse\space}},
% Comments
morecomment=[s]{"""}{"""},
morecomment=[s]{'''}{'''},
morecomment=[l]{\#},
% Keywords
keywordstyle=\color{mydarkgrey}\normalfont\bfseries,
morekeywords={awk,bash, break,case,cat,cut,cd,cp, chmod, continue,do,done,echo,elif,else,%
      env,esac,eval,exit,export,expr,false,find,function,getopts, grep,%
      hash,history,if,kill,login,ls,newgrp,nice,nohup,ps,pwd,read,rm,%
      readonly,return,set,sed,shift,then,times,trap,true,%
      ulimit,umask,unset,until,wait,while,%
      alias,bg,bind,builtin,caller,compgen,compopt,%
      complete,coproc,declare,disown,dirs,enable,fc,fg,head, help,history,%
      jobs,let,local,logout,mapfile,printf,pushd,popd,readarray,select,%
      set,suspend,sort, source,times,tr, touch, typeset,ulimit,unalias,wc, xargs,%
      mkdir, git, tail, head, uniq, open,%
      commit, merge, status, log, add, clone, init, stash, apply, %
      diff, --amend, pull, push, fetch, reset,  blame, branch, checkout, reset, %
      mv, rmdir, less, for, in, man, file, gedit, -oneline, --graph, --decorate, --all, --hard%
      say, date, cal, sudo, chown, config, geom_smooth
      },%
otherkeywords={},
alsoletter={=},
escapechar = {§},
}
\lstnewenvironment{shorttermcode}{\lstset{style=myterminalstyle}}{}

\begin{document}

\begin{shorttermcode}
# assuming you saved CSB in your home directory
# navigate to the sandbox in the CSB/unix directory
cd ~CSB/unix/sandbox
\end{shorttermcode}

\end{document}

它可以正常工作,但如果出现字符 / ,就会出现一些不必要的垂直空白。请参阅下面的输出:

在此处输入图片描述

我该如何修复此问题?

答案1

您的代码缺少 \makeatletter。另外最好不要加载 xltxtra 和 xunicode。

不要用列表来做框架,最好使用 tcolorbox。这样你就不会得到空白,也不会得到灰色边缘。我停用了你的字体,因为我没有它。

\documentclass{book}
\usepackage{fontspec}
\usepackage{unicode-math}%

%\setmonofont[Path = ./Fonts/ ,
%UprightFont= Iosevka ,]{Iosevka}

\usepackage{textcomp,xcolor,MNsymbol}
\definecolor{mylightgrey}{rgb}{0.98, 0.98, 0.98}
\definecolor{mydarkgrey}{rgb}{0.3, 0.3, 0.3}

\usepackage{listings}

\makeatletter
\lstdefinestyle{myterminalstyle}{%
upquote=true,
basicstyle=\ttfamily\small,
commentstyle=\slshape\footnotesize,
showspaces = false,
showtabs = false,
breaklines=true,xleftmargin=4\p@,xrightmargin=3\p@,aboveskip=13\p@,belowskip=10\p@,
postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\rcurvearrowse\space}},
% Comments
morecomment=[s]{"""}{"""},
morecomment=[s]{'''}{'''},
morecomment=[l]{\#},
% Keywords
keywordstyle=\color{mydarkgrey}\normalfont\bfseries,
morekeywords={awk,bash, break,case,cat,cut,cd,cp, chmod, continue,do,done,echo,elif,else,%
      env,esac,eval,exit,export,expr,false,find,function,getopts, grep,%
      hash,history,if,kill,login,ls,newgrp,nice,nohup,ps,pwd,read,rm,%
      readonly,return,set,sed,shift,then,times,trap,true,%
      ulimit,umask,unset,until,wait,while,%
      alias,bg,bind,builtin,caller,compgen,compopt,%
      complete,coproc,declare,disown,dirs,enable,fc,fg,head, help,history,%
      jobs,let,local,logout,mapfile,printf,pushd,popd,readarray,select,%
      set,suspend,sort, source,times,tr, touch, typeset,ulimit,unalias,wc, xargs,%
      mkdir, git, tail, head, uniq, open,%
      commit, merge, status, log, add, clone, init, stash, apply, %
      diff, --amend, pull, push, fetch, reset,  blame, branch, checkout, reset, %
      mv, rmdir, less, for, in, man, file, gedit, -oneline, --graph, --decorate, --all, --hard%
      say, date, cal, sudo, chown, config, geom_smooth
      },%
otherkeywords={},
alsoletter={=},
escapechar = {§},
}

\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting{shorttermcode}{colframe=black, colback=black!16, listing options={style=myterminalstyle},listing only }
\begin{document}

\begin{shorttermcode}
# assuming you saved CSB in your home directory
# navigate to the sandbox in the CSB/unix directory
cd ~CSB/unix/sandbox
\end{shorttermcode}

\end{document}

在此处输入图片描述

相关内容