如何为我的文档中的每个 \lstinputlisting 环境定制 \lstset?

如何为我的文档中的每个 \lstinputlisting 环境定制 \lstset?

这些是我的 overleaf 项目的设置(我的教授给我的):

% Listings and related stuff

\usepackage[hyper]{listings}

%%%%%%%%%%%%%%%%%%%%%%%% pacchetto caricato da G.Donnarumma
\usepackage{listingsutf8} % to write accented words in listing environment
%%%%%%%%%%%%%%%%%%%%%%%5

\renewcommand{\lstlistingname}{Listato}
\newcommand{\lstInCaption}[1]{\lstinline[basicstyle=\ttfamily\footnotesize]|#1|}

% MATLAB as default 
\lstset{%
    inputencoding=utf8/latin1,
    float=hbp,%
    language=Matlab,%
    style=Matlab-editor,   %richiede il pacchetto matlab-prettifier (inserito da G.Donnarumma)
    basicstyle=\ttfamily\small, %font size/family/etc.for source \small
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys},%
    stringstyle=\color{colString},%
    commentstyle=\color{colComments},%
    columns=[l]fixed,% flexible,%
    keepspaces=false,%        sposta la linea di separazione delle sezioni 
    %tabsize=2,%
    frame=single,% single,lines, none
    rulecolor= \color{boxgray},%specify the color of  the frame-box 
    extendedchars=true,%
    showspaces=false,%
    showstringspaces=false,%
    escapechar         = ",      %inserito da G.Donnarumma
    mlshowsectionrules = true,   %inserito da G.Donnarumma
    numbers=left,% left, none    %visualizza num. identificat. linea codice
    numberstyle=\tiny,%
    breaklines=true,     %automatic line-breaking
    lineskip=-2pt,       %
    backgroundcolor=\color{mylightgray}, %
    breakautoindent=true,       %
    numberbychapter=true,       %
    captionpos=false,    %position of caption (t/b)
    belowcaptionskip=0.2\baselineskip%\medskipamount%
}
\lstset{%
   morekeywords={cell,interp1,interp3,interpn,fnplt,fneval%
      ,odeset,ode113,ode15s,ode23s,ode23t,ode23tb,ode45,cell2mat,convangvel,convvel,convang,convforce,convlength,deg2rad,atmoscoesa,atmosisa,atmosnonstd,%
      datcomimport,importdata,fieldnames,assignin%
      ,fnval,csaps,fmincon%
      ,classdef,properties,methods%
   }
}
\lstset{%
   emph={myfun,myfunction2x1,f_prandtl_a_b,%
      DSVAircraft,myAC,costLongEquilibriumStaticStickFixed,myNonLinearConstraint,dummyNonLinearConstraint,%
      eqLongDynamicStickFixed,calc_CD0_k_Tmax,MassStickFree,cla,eps,},%
   emphstyle={%
      \color{red!80!black}%
         \ttfamily\bfseries%
   }
}

\lstset{% "interp1" and "ode45" were in this list 
    emph=[1]{pi,norm,sin,cos,disp,cd,set,get,gca,gradient,length,tan,gamma,sqrt,diag,acos,atan,asin,imag,real,subplot,sound,zeros,interp2,meshgrid,input,sprintf,xlabel,ylabel,plot,print, legend,grid,figure,hold},emphstyle=[1]\color{black}, %some words to emphasise
}


% DATCOM
\lstdefinelanguage{Datcom}{%
    emph={%
        \$FLTCON,\$OPTINS,\$SYNTHS,\$BODY,%
        \$WGPLNF,\$HTPLNF,\$VTPLNF,\$SYMFLP,\$ASYMFLP,%
        \$},%
    emphstyle={%
          \color{red!80!black}%
            \ttfamily\bfseries%
        },%
    morekeywords={%
        WT,\$,NMACH,MACH,NALT,ALT,NALPHA,ALSCHD,%
        SREF,CBARR,BLREF,%
        XCG,XW,ZW,ALIW,XH,ZH,ALIH,XV,ZV,VERTUP,%
        NX,X,R,%
        CHRDTP,SSPNE,SSPN,CHRDR,SAVSI,CHSTAT,%
        TWISTA,SSPNDD,DHDADI,DHDADO,TYPE,%
        NACA,%
        CASEID,%
        DAMP,DIM,DERIV,TRIM,PART,SAVE,NEXT,CASE%
        WT,LOOP,NMACH,MACH,VINF,NALPHA,ALSCHD,RNNUB,NALT,%
            ALT,PINF,TINF,HYPERS,STMACH,TSMACH,TR,GAMMA,%
        SREF,CBARR,BLREF,ROUGFC,XCG,ZCG,XW,ZW,ALIW,XH,ZH,ALIH,XV,XVF,ZV,ZVF,SCALE,%
        VERTUP,HINAX,NX,X,S,P,R,ZU,ZL,BNOSE,BTAIL,BLN,BLA,DS,%
        ITYPE,METHOD,CHRDR,CHRDBP,CHRDTP,SSPN,SSPNE,SSPNOP,SAVSI,SAVSO,CHSTAT,%
        TWISTA,SSPNDD,DHDADI,DHDADO,TYPE,TOVC,DELTAY,XOVC,CLI,ALPHAI,CLALPA,CLMAX,CMO,%
        LERI,LERO,CAMBER,TOVCO,XOVCO,CMOT,CLMAXL,CLAMO,TCEFF,KSHARP,SLOPE,ARCL,%
        XAC,DWASH,YCM,CLD,TYPEIN,NPTS,XCORD,YUPPER,YLOWER,MEAN,THICK,NGH,%
        GRDHT,FTYPE,NDELTA,DELTA,PHETE,PHETEP,CHRDFI,CHRDFO,SPANFI,SPANFO,%
        CPRMEI,CPRMEO,CAPINS,CAPOUT,DOSDEF,DOBCIN,DOBCOT,SCLD,SCMD,%
        CB,TC,NTYPE,JETFLP,CMU,DELJET,EFFJET,STYPE,NDELTA,DELTAL,DELTAR,%
        SPANFI,SPANFO,PHETE,CHRDFI,CHRDFO,DELTAD,DELTAS,XSOC,XSPRME,%
        HSOC,AIETLP,NENGSP,THSTCP,PHALOC,PHVLOC,PRPRAD,ENGFCT,%
        BWAPR3,BWAPR6,BWAPR9,NOPBPE,BAPR75,YP,CROT,%
        AIETLJ,AMBSTP,AMBTMP,JEALOC,JEANGL,JELLOC,JERAD,JESTMP,%
        JETOTP,JEVELO,JEVLOC,JIALOC,JINLTA,NENGSJ,THSTCJ%
    },
    sensitive=true,
    morecomment=[l]{\#},
    morestring=[b]"
}

\lstdefinelanguage{DatcomOutput}{%
}

\lstdefinelanguage{MyInputForMatlab}{%
}

\lstdefinelanguage{MyNoSpecificLanguage}{%
    emph={},%
    emph={[2]},%
    emph={[3]},%
    emph={[4]}%
}

\lstloadlanguages{Matlab,Datcom,DatcomOutput,MyInputForMatlab,MyNoSpecificLanguage}

% see: http://tex.stackexchange.com/questions/18532/listing-language-for-screen-session
\lstnewenvironment{myCommandLine}
    {\lstset{%
        basicstyle=\ttfamily,
        backgroundcolor=\color{mylightgray},% \color{hellgelb}, %
        frame=none,%
        columns=[l]fixed,%flexible, %
        prebreak=\makebox[1.4ex][r]{\color{blue}\raisebox{-0.9ex}[0ex][0ex]{\scriptsize\ensuremath{\hookleftarrow}}},
        keepspaces=true,%
        escapechar=~,% use this for output
        morekeywords={ls,pwd,cp,mv},
        identifierstyle=\color{colIdentifier}, %
        keywordstyle=\color{red!60!black}\bfseries,% \color{colKeys},%\fontseries{b}\selectfont, %
        stringstyle=\color{colString}, %
        literate={\$}{{\textcolor{blue}{\$}}}1}% prompt symbol
    }
    {}
\lstnewenvironment{myCommandLineOutput}
    {\lstset{%
        language=MyNoSpecificLanguage,%
        basicstyle=\ttfamily\footnotesize,%
        backgroundcolor=\color{mylightgray},% \color{hellgelb}, %
        lineskip=-1pt,%
        frame=none,%
        columns=[l]fixed,%flexible, %
        prebreak=\makebox[1.4ex][r]{\color{blue}\raisebox{-0.9ex}[0ex][0ex]{\scriptsize\ensuremath{\hookleftarrow}}},
        keepspaces=true,%
        escapechar=~% use this for output
        }
    }
    {}
\lstnewenvironment{myCommandLineOutputSmaller}
    {\lstset{%
        language=MyNoSpecificLanguage,%
        basicstyle=\ttfamily\scriptsize,%
        backgroundcolor=\color{mylightgray},% \color{hellgelb}, %
        lineskip=-1pt,%
        frame=none,%
        columns=[l]fixed,%flexible, %
        prebreak=\makebox[1.4ex][r]{\color{blue}\raisebox{-0.9ex}[0ex][0ex]{\scriptsize\ensuremath{\hookleftarrow}}},
        keepspaces=true,%
        escapechar=~% use this for output
        }
    }
    {}

\lstinputlisting[]{code.m}如果我只想更改文档中不同列表的某些设置(例如,我想指定语言是 Matlab 还是 DATCOM,或者我想选择是否显示代码侧面的数字等等),您能告诉我如何使用环境吗?

所以,我想知道如何调用所有设置(例如有关 matlab 的设置),然后如何根据环境的不同用途随时定制其中的一些设置\lstinputlisting[]{code.m}

我尝试写类似这样的内容:

\lstinputlisting[language=Matlab,style=Matlab-editor,captionpos=b,numbers=none,caption=title}.,label=test,firstline=30,lastline=56]{code.m}

但我发现该风格与这个(我以前使用的通用命令)不同:

\lstinputlisting[caption=title,label=test,firstline=30,lastline=56]{code.m}

我的目标只是删除数字并插入标题,但我想保留此处定义的样式:

\lstset{%
   inputencoding=utf8/latin1,
   float=hbp,%
   language=Matlab,%
   style=Matlab-editor,   %richiede il pacchetto matlab-prettifier (inserito da G.Donnarumma)
   basicstyle=\ttfamily\small, %font size/family/etc.for source \small
   identifierstyle=\color{colIdentifier}, %
   keywordstyle=\color{colKeys},%
   stringstyle=\color{colString},%
   commentstyle=\color{colComments},%
   columns=[l]fixed,% flexible,%
   keepspaces=false,%        sposta la linea di separazione delle sezioni 
   %tabsize=2,%
   frame=single,% single,lines, none
   rulecolor= \color{boxgray},%specify the color of  the frame-box 
   extendedchars=true,%
   showspaces=false,%
   showstringspaces=false,%
   escapechar         = ",      %inserito da G.Donnarumma
   mlshowsectionrules = true,   %inserito da G.Donnarumma
   numbers=left,% left, none    %visualizza num. identificat. linea codice
   numberstyle=\tiny,%
   breaklines=true,     %automatic line-breaking
   lineskip=-2pt,       %
   backgroundcolor=\color{mylightgray}, %
   breakautoindent=true,       %
   numberbychapter=true,       %
   captionpos=false,    %position of caption (t/b)
   belowcaptionskip=0.2\baselineskip%\medskipamount%
}

相关内容