我的代码如下:
\documentclass[11pt]{book}
\usepackage{makeidx}
\usepackage{emptypage}%amd for no page number on empty page
\usepackage{etoc}%amd for multiple tocs
\usepackage{upquote}%amd for upright single quote
\usepackage{textcomp}%amd for upright quote symbols
\usepackage{listings}
\usepackage[usenames]{color}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{tcolorbox}
\tcbuselibrary{most}
\tcbset{mybox/.style={
before={\par\vskip3ex\noindent},
after={\par\vskip1.5ex}}}
\usepackage{MnSymbol}
\definecolor{mylightgrey}{rgb}{0.98, 0.98, 0.98}
\definecolor{mydarkgrey}{rgb}{0.3, 0.3, 0.3}
\makeatletter
\lstdefinestyle{mylatexstyle}{%
%language=LaTeX,
% Basic style
basicstyle=\ttfamily\small,
commentstyle=\slshape\footnotesize, %\rmfamily
stringstyle=\ttfamily\slshape\small,
% Line numbers
numbers=left,
numberstyle=\footnotesize,
numberfirstline=false,
numbersep=1em,
stepnumber=3,
% Margins and box
framextopmargin=2em,
framexbottommargin=2em,
frame=l,
backgroundcolor=\color{mylightgrey},
xleftmargin=1em,
tabsize=4,
% Show spaces
showspaces=false,
showtabs=false,
showstringspaces=false,
columns=fullflexible,
breaklines=true,
% Comments
morecomment=[l]{\%},
% Keywords
keywordstyle=\color{mydarkgrey}\bfseries,
escapechar={§},
%alsoletter={_, .},
morekeywords={},
otherkeywords={end, table, figure, item, description, document, graphicspath, tableofcontents, documentclass,
setends, showsequencelogo, feature, showruler, hidenumbering, texshade, setdoublesep, setatomsep, chemname, chemfig},
sensitive=true,
% Built-in
emphstyle=\color{mydarkgrey}\bfseries,
emph={maketitle, tableofcontents, input, addcontentsline,addpenalty,addtocontents,%
addtocounter,addtolength,addtoversion,addvspace,alph,Alph,%
arabic,array,arraycolsep,arrayrulewidth,arraystretch,%
baselinestretch,begin,bezier,bfseries,bibcite,bibdata,bibitem,%
bibliography,bibliographystyle,bibstyle,bigskip,boldmath,%
botfigrule,bottomfraction,Box,caption,center,chapter, CheckCommand,circle,%
citation,cite,cleardoublepage,clearpage,cline,columnsep,%
columnseprule,columnwidth,contentsline,dashbox,date,dblfigrule,%
dblfloatpagefraction,dblfloatsep,dbltextfloatsep,dbltopfraction,%
defaultscriptratio,defaultscriptscriptratio,depth,Diamond,%
displaymath,documentclass,documentstyle,doublerulesep,%
em,emph,endarray,endcenter,enddisplaymath,enddocument,%
endenumerate,endeqnarray,endequation,endflushleft,endflushright,%
enditemize,endlist,endlrbox,endmath,endminipage,endpicture,%
endsloppypar,endtabbing,endtabular,endtrivlist,endverbatim,%
enlargethispage,ensuremath,enumerate,eqnarray,%equation,%
evensidemargin,extracolsep,fbox,fboxrule,fboxsep,filecontents,%
fill,floatpagefraction,floatsep,flushbottom,%flushleft,flushright,%
fnsymbol,fontencoding,fontfamily,fontseries,fontshape,fontsize,%
fontsubfuzz,footnotemark,footnotesep,footnotetext,footskip,frac,%
frame,framebox,fussy,glossary,headheight,headsep,height,hline,%
hspace,include,includeonly,index,inputlineno,intextsep,%
itemindent,itemize,itemsep,iterate,itshape,Join,kill,label,%
labelsep,labelwidth,LaTeX,LaTeXe,leadsto,lefteqn,leftmargin,%
leftmargini,leftmarginii,leftmarginiii,leftmarginiv,leftmarginv,%
leftmarginvi,leftmark,lhd,lim,linebreak,linespread,linethickness,%
linewidth,list,listfiles,listfiles,listparindent,lrbox,%
makeatletter,makeatother,makebox,makeglossary,makeindex,%
makelabel,MakeLowercase,MakeUppercase,marginpar,marginparpush,%
marginparsep,marginparwidth,markboth,markright,math,mathbf,%
mathellipsis,mathgroup,mathit,mathrm,mathsf,mathsterling,mathtt,%
mathunderscore,mathversion,mbox,mdseries,mho,minipage,%
multicolumn,multiput,NeedsTeXFormat,newcommand,newcounter,%
newenvironment,newfont,newhelp,newlabel,newlength,newline,%
newmathalphabet,newpage,newsavebox,newtheorem,nobreakspace,%
nobreakspace,nocite,nocorr,nocorrlist,nofiles,nolinebreak,%
nonumber,nopagebreak,normalcolor,normalfont,normalmarginpar,%
numberline,obeycr,oddsidemargin,oldstylenums,onecolumn,oval,%
pagebreak,pagenumbering,pageref,pagestyle,paperheight,paperwidth,%
paragraph, paragraphmark,parbox,parsep,partopsep,picture,poptabs,pounds,%
protect,pushtabs,put,qbezier,qbeziermax,raggedleft,raisebox,%
ref,refstepcounter,renewcommand,renewenvironment,restorecr,%
reversemarginpar,rhd,rightmargin,rightmark,rmfamily,roman,Roman,%
rootbox,rule,samepage,sbox,scshape,secdef,section,sectionmark,%
selectfont,setcounter,settodepth,settoheight,settowidth,sffamily,%
shortstack,showoutput,showoverfull,sloppy,sloppypar,slshape,%
smallskip,sqsubset,sqsupset,SS,stackrel,stepcounter,stop,stretch,%
subparagraph, subparagraphmark,subsection, subsubsection, subsectionmark,subsubsectionmark,sum,%
suppressfloats,symbol,tabbing,tabbingsep,tabcolsep,tabular,%
tabularnewline,textasciicircum,textasciitilde,textbackslash,%
textbar,textbf,textbraceleft,textbraceright,textbullet,%
textcircled,textcompwordmark,textdagger,textdaggerdbl,textdollar,%
textellipsis,textemdash,textendash,textexclamdown,textfloatsep,%
textfraction,textgreater,textheight,textit,textless,textmd,%
textnormal,textparagraph,textperiodcentered,textquestiondown,%
textquotedblleft,textquotedblright,textquoteleft,textquoteright,%
textregistered,textrm,textsc,textsection,textsf,textsl,%
textsterling,textsuperscript,texttrademark,texttt,textunderscore,%
textup,textvisiblespace,textwidth,thanks,thefootnote,thempfn,%
thempfn,thempfootnote,thepage,thepage,thicklines,thinlines,%
thispagestyle,today,topfigrule,topfraction,topmargin,%
topsep,totalheight,tracingfonts,trivlist,ttfamily,twocolumn,%
typein,typeout,unboldmath,unitlength,unlhd,unrhd,upshape,usebox,%
usecounter,usefont,usepackage,vector,verb,verbatim,vline,%
vspace,%width,%
normalsize,small,footnotesize,scriptsize,tiny,large,Large,LARGE,%
huge,Huge, moderncvstyle, moderncvcolor, newcites, firstname, familyname, address,%
phone, email, homepage, makecvtitle, cventry, nocitepubs, bibliographystylepubs,%
nociteprep, bibliographypubs, bibliographystyleprep, bibliographyprep, cvitem,%
lim, sum, infty, underline, author, \title, includegraphics, item, quad}
}
\lstnewenvironment{shorttexcode}{\lstset{style=mylatexstyle}}{}
\makeatother
\begin{document}
\begin{shorttexcode}
\documentclass{report}
% load desired packages
\usepackage{amsmath}
\usepackage{graphicx}
% Specify the directory where pictures are stored
\graphicspath{{Pictures/}}
\begin{document}
\title{\textbf{Your short or long thesis §title§}}
\author{Your Name}
% let LaTeX produce the title and §\textit{\footnotesize table}§ of contents
\maketitle
\tableofcontents
\input{introduction}
\input{methods}
\input{results}
\input{discussion}
\end{document}
\end{shorttexcode}
\end{document}
生成的输出为:
但所需的输出为:
您可以看到行号的差异,我该如何解决这个问题?
答案1
您必须明确设置firstnumber
:
\documentclass[11pt]{book}
\usepackage{listings}
\lstdefinestyle{mylatexstyle}{%
% Line numbers
numbers=left,
stepnumber=3,
firstnumber=1
}
\lstnewenvironment{shorttexcode}{\lstset{style=mylatexstyle}}{}
\begin{document}
\begin{shorttexcode}
one
two
three
four
five
six
seven
eight
\end{shorttexcode}
\end{document}