到目前为止,我都使用该listings
软件包来编写代码清单。今天,我尝试了一下minted
,对默认外观很满意。由于在论文的这个阶段,我有很多事情要切换listings
,所以minted
我想模仿这个外观。
listingz
在下图中,您可以看到和中的相同源代码minted
。您能帮我确定适当的listings
设置,使左侧示例看起来像右侧示例吗?我可以自己弄清楚颜色,但其余的(间距、字体等)让我感到困惑。
以下是我的列表样式设置:
% javascript code listing
\newcommand{\inputjscode}[2][]{{%
\renewcommand{\lstlistingname}{JavaScript Code}%
\renewcommand{\addcontentsline}[3]{\oldaddcontentsline{los}{##2}{##3\enskip(\lstlistingname)}}%
\lstset{
tabsize=2,
lineskip=-1pt,
rulecolor=,
basicstyle=\footnotesize,
columns=fullflexible,
upquote=true,
aboveskip={1.3\baselineskip},
columns=fixed,
showstringspaces=false,
extendedchars=true,
breaklines=false,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
escapechar=@,
frame=single,
showtabs=false,
showspaces=false,
showstringspaces=false,
identifierstyle=\ttfamily,
keywordstyle=\color[rgb]{1.0,0,0},
keywordstyle=[1]\color[rgb]{0,0,0.75},
keywordstyle=[2]\color[rgb]{0.5,0.0,0.0},
keywordstyle=[3]\color[rgb]{0.127,0.427,0.514},
keywordstyle=[4]\color[rgb]{0.4,0.4,0.4},
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\color[rgb]{0.639,0.082,0.082},
}
\lstinputlisting[
numbers=left,
frame=single,
stepnumber=1,
inputencoding=latin1,
language=JavaScript,
basicstyle=\scriptsize,
#1]{#2}%
}}
答案1
最终,使其变得棘手的部分是您的命令中存在冗余\lstset
(并且可能还有您为 Javascript 定义关键字的地方):例如,您为不同关键字样式提供的颜色被已定义的其他颜色覆盖。
我从 Javascript 的语言定义开始listings
,它来自这个 TeX.sx 帖子;但是,我们将修改颜色,因此我删除了我们不会使用的定义。
\lstdefinelanguage{JavaScript}{
keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
ndkeywords={class, export, boolean, throw, implements, import, this},
sensitive=false,
comment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]',
morestring=[b]"
}
keywordstyle
然后,我们定义样式,它来自设置、等中的颜色(和注释的斜体)commentstyle
,以及一些间距调整。我总是按字母顺序排列我的\lstset
调用,这样我就可以快速找出我是否已经添加了命令,而不是重复它。
\lstset{
aboveskip={1.3\baselineskip},
basicstyle=\scriptsize\ttfamily\linespread{4},
breaklines=false,
columns=flexible,
commentstyle=\color[rgb]{0.127,0.427,0.514}\ttfamily\itshape,
escapechar=@,
extendedchars=true,
frame=single,
identifierstyle=\color{black},
inputencoding=latin1,
keywordstyle=\color[HTML]{228B22}\bfseries,
language=JavaScript,
ndkeywordstyle=\color[HTML]{228B22}\bfseries,
numbers=left,
numberstyle=\tiny,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
showstringspaces=false,
stringstyle=\color[rgb]{0.639,0.082,0.082}\ttfamily,
upquote=true
}
最后,minted
用灰色突出显示数字。这个 TeX.sx 答案向我们展示了如何做到这一点listings
:
\definecolor{darkgray}{rgb}{.4,.4,.4}
\lstset{literate=%
*{0}{{{\color{darkgray}0}}}1
{1}{{{\color{darkgray}1}}}1
{2}{{{\color{darkgray}2}}}1
% ... and so on
{9}{{{\color{darkgray}9}}}1
}
这是我的整个工作示例。
\documentclass{article}
\usepackage{xcolor}
\usepackage{textcomp}
\usepackage{listings}
\definecolor{darkgray}{rgb}{.4,.4,.4}
\lstdefinelanguage{JavaScript}{
keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
ndkeywords={class, export, boolean, throw, implements, import, this},
sensitive=false,
comment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]',
morestring=[b]"
}
\lstset{
aboveskip={1.3\baselineskip},
basicstyle=\scriptsize\ttfamily\linespread{4},
breaklines=false,
columns=flexible,
commentstyle=\color[rgb]{0.127,0.427,0.514}\ttfamily\itshape,
escapechar=@,
extendedchars=true,
frame=single,
identifierstyle=\color{black},
inputencoding=latin1,
keywordstyle=\color[HTML]{228B22}\bfseries,
language=JavaScript,
ndkeywordstyle=\color[HTML]{228B22}\bfseries,
numbers=left,
numberstyle=\tiny,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
stringstyle=\color[rgb]{0.639,0.082,0.082}\ttfamily,
upquote=true,
showstringspaces=false,
}
\lstset{literate=%
*{0}{{{\color{darkgray}0}}}1
{1}{{{\color{darkgray}1}}}1
{2}{{{\color{darkgray}2}}}1
{3}{{{\color{darkgray}3}}}1
{4}{{{\color{darkgray}4}}}1
{5}{{{\color{darkgray}5}}}1
{6}{{{\color{darkgray}6}}}1
{7}{{{\color{darkgray}7}}}1
{8}{{{\color{darkgray}8}}}1
{9}{{{\color{darkgray}9}}}1
}
\begin{document}
\lstinputlisting{filename.js}
\end{document}