20 在本站下载的数学建模竞赛论文模板中lstlisting环境不会调参数

发布于 2023-08-05 21:38:13

具体问题是不能正常设置自动换行,代码高亮也有异常,注释样式也做不出来。模板选用的是这个:
https://www.latexstudio.net/index/details/index?mid=762
下面是出现问题的部分代码与截图(由于多处出现这种问题,只截了一处,所以所截的图并不是这段代码)(以!开头;结尾的语句是lingo中的注释):

\begin{lstlisting}[language=lingo,
    basicstyle=\ttfamily,
    breaklines=true,
    keywordstyle=\bfseries\color{blue},
    commentstyle=\itshape\color{black!50!white},
    stringstyle=\bfseries\color{PineGreen!90!black},
    columns=flexible,
    numbers=left,
    numbersep=2em,
    numberstyle=\footnotesize,
    frame=single,
    framesep=0.8em
    ]
model:

sets:
su/1..402/:c,ts,tto,mdr;                   !k;
endsets

data:
correct=0.1;       !修正值,目的是排除掉一个异常数据;
ts,mdr=@ole('1_h_mdr_ts_srabc.xlsx','ts','mdr');
tto=@ole('1_tto.xlsx','tto');
@ole('D:\LINGO64_18\Samples\1_h_mdr_ts_srabc.xlsx','r_1')=c;
enddata

min=@sum(su:c*(ts+correct)/(tto*@sqr(mdr)));
@for(su:@bin(c));
@sum(su:c)=50;

end
 \end{lstlisting}

网页捕获_5-8-2023_20164_www.overleaf.com.jpeg

自动换行和代码高亮我试了,只要不用这个模板,然后在导言区加一段代码就可以解决(如果仍然使用这个模板就会报关于xcolor的错)。代码是我在网上找的,如下:

\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\usepackage{geometry}
%\geometry{a4paper,scale=0.8}

\lstset{
language=lingo,       % 设置语言
basicstyle=\ttfamily, % 设置字体族
breaklines=true,      % 自动换行
keywordstyle=\bfseries\color{NavyBlue}, % 设置关键字为粗体,颜色为 NavyBlue
morekeywords={sets,endsets,data,enddata,@size,@sum,@for,@bin,@gin},% 设置更多的关键字,用逗号分隔
emph={wszs},                                 % 指定强调词,如果有多个,用逗号隔开
    emphstyle=\bfseries\color{Rhodamine},    % 强调词样式设置
commentstyle=\itshape\color{black!50!white}, % 设置注释样式,斜体,浅灰色
stringstyle=\bfseries\color{PineGreen!90!black}, % 设置字符串样式,没什么用
columns=flexible,     % 缩紧字距
numbers=left,         % 显示行号在左边
numbersep=2em,        % 设置行号的具体位置
numberstyle=\footnotesize, % 缩小行号
frame=single,         % 单个边框
framesep=0.8em}        % 设置代码与边框的距离

我先是在overleaf上在线编译,发现了这样的问题,然后又保存下来在本地编译,问题还是没有消失,不知道应该怎么解决。求大佬帮助。
11111.jpg

查看更多

关注者
0
被浏览
467
2 个回答
LaTeXStudio
LaTeXStudio 2023-08-05
这家伙很懒,什么也没写!

你是加了重复的宏包,但是参数不一致,你最好这样来加

\PassOptionsToPackage{dvipsnames}{xcolor} 
龙友
龙友 2023-08-09
这家伙很懒,什么也没写!

在导言区加上这段代码,可以解决提到的所有问题,顺便把我发现的关键字数量不够的问题也解决了
`definecolor{matlab}{RGB}{0,100,0}
definecolor{lingo}{RGB}{128,0,128}
definecolor{darkgreen}{RGB}{0,100,0}
definecolor{mylightgreen}{RGB}{160,180,160}

lstnewenvironment{matlabcode}[1][]
{
lstset{

language=Matlab,
basicstyle=\small\ttfamily,
columns=fullflexible,
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{darkgreen},
stringstyle=\color{matlab},
breaklines=true,
postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
showstringspaces=false,
numbers=left,
numberstyle=\tiny\color{gray},
#1

}
}{}

lstdefinelanguage{LINGO}{%
morekeywords={model, end, sets, endsets, data, enddata, calc, endcalc, init, endinit,

@bin, @gin, @bnd, @free, @sum, @for, @sqr, @sqrt, @exp, @ole, @text, 
@abs, @sin, @cos, @tan, @log, @sign, @smax, @smin, @floor, @lgm,
@mod, @pow, @psn, @pps, @pbn, @phg, @pfd, @pcx, @ptd, @rand, @qrand, @peb, @pel, @ppl, @pfs, @psl,
@max, @min, @prod, @in, @size, @index, @wrap},

sensitive=false,
morecomment=[l]{!},
morecomment=[s]{/}{/},
morestring=[b]",
}

lstnewenvironment{lingocode}[1][]
{
lstset{

language=Lingo,
basicstyle=\small\ttfamily,
columns=fullflexible,
keywordstyle=\color{blue}\bfseries,
commentstyle=\color{mylightgreen},
stringstyle=\color{lingo},
breaklines=true,
breakatwhitespace=false,
postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
showstringspaces=false,
numbers=left,
numberstyle=\tiny\color{gray},
#1

}
}{}`

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览