我的标签如下:
\documentclass[11pt]{book}
\usepackage{listings}
\lstdefinestyle{mylatexstyle}{%
% Line numbers
numbers=left,
stepnumber=3,
firstnumber=2
}
\lstnewenvironment{shorttexcode}{\lstset{style=mylatexstyle}}{}
\begin{document}
\begin{shorttexcode}
one
two
three
four
five
six
seven
eight
\end{shorttexcode}
\end{document}
我需要从 开始行号,2
并且以下数字如下5, 8, 11
...
但即使我提到,行号也没有改变firstnumber=2
,我该如何实现呢?
答案1
对于复杂的编号系统,我会重新定义\thelstnumber
。这比内置选项更灵活(我不太确定我是否理解了你想要的编号,但你可以根据自己的需要进行调整)。
\documentclass[11pt]{book}
\usepackage{listings}
\lstdefinestyle{mylatexstyle}{%
% Line numbers
numbers=left,
}
\usepackage{expl3}
\ExplSyntaxOn\makeatletter
\newcommand\setmynumberstyle[2]
{% #1=start #2=step
\renewcommand*\thelstnumber
{
\int_compare:nF
{\c@lstnumber < #1}
{ \int_compare:nT
{ \int_mod:nn {\c@lstnumber -#1} {#2} = 0 }
{ \arabic{lstnumber}}
}
}
}
\ExplSyntaxOff
\lstnewenvironment{shorttexcode}{\setmynumberstyle{2}{3}\lstset{style=mylatexstyle}}{}
\begin{document}
\begin{shorttexcode}
one
two
three
four
five
six
seven
eight
\end{shorttexcode}
\end{document}
编辑
重新阅读这个问题,你也许想要一个偏移:
\documentclass[11pt]{book}
\usepackage{listings}
\lstdefinestyle{mylatexstyle}{%
% Line numbers
numbers=left,
}
\usepackage{expl3}
\ExplSyntaxOn\makeatletter
\newcommand\setmynumberstyle[3]
{% #1=start #2=step #3 offset
\renewcommand*\thelstnumber
{
\int_compare:nF
{\c@lstnumber < #1}
{ \int_compare:nT
{ \int_mod:nn {\c@lstnumber -#1} {#2} = 0 }
{ \int_eval:n{ \c@lstnumber + #3 }}
}
}
}
\ExplSyntaxOff
\lstnewenvironment{shorttexcode}{\setmynumberstyle{1}{3}{1}\lstset{style=mylatexstyle}}{}
\begin{document}
\begin{shorttexcode}
one
two
three
four
five
six
seven
eight
\end{shorttexcode}
\end{document}
答案2
numberfirstline=true
按照此处所述添加:http://texdoc.net/texmf-dist/doc/latex/listings/listings.pdf部分5.6 行号