我最近问了这个问题:不同列表语言脚本的独立标题和编号
而我需要的答案是使用\lstnewenvironment
。问题是我仍在处理代码,所以我需要导入代码。我以前使用过\lstinputlisting
,但它似乎与不兼容\lstnewenvironment
。
有没有办法将输入文档提供给\lstnewenvironment
就像那样\lstinputlisting
?
我尝试了这个,但没有成功:
\documentclass{extarticle}
\usepackage{listings}
\newcounter{pythoncode}
\lstnewenvironment{pythoncode}[3]{
\renewcommand\lstlistingname{Python Code}
\setcounter{lstlisting}{\value{pythoncode}}
\lstset{
frame=single,
language=python,
rulecolor=\color{black},
commentstyle=\color{mGreen},
keywordstyle=\color{mOrange},
caption={[#1]{#1}},
label={#2},
inputpath={#3}
}
} {\addtocounter{pythoncode}{1}}
\begin{document}
\begin{pythoncode}{someCaption}{someLabel}{patronesOptimos.py}
\end{pythoncode}
\end{document}
输出是这样的(当然它不是一个空的 Python 文件):
我也尝试过这个,但它会产生超时:
\documentclass{extarticle}
\usepackage{listings}
\lstdefinestyle{mypy}{
frame=single,
language=python,
rulecolor=\color{black},
commentstyle=\color{mGreen},
keywordstyle=\color{mOrange},
}
\newcounter{pythoncode}
\lstnewenvironment{pythoncode}[3]{
\renewcommand\lstlistingname{Python Code}
\setcounter{lstlisting}{\value{pythoncode}}
\lstset{
escapeinside={(*@}{@*)},
caption={[#1]{#1}},
label={#2}
}
} {\addtocounter{pythoncode}{1}}
\begin{document}
\begin{pythoncode}{someCaption}{someLabel}
(*@
\lstinputlisting[style = mypy]{Codigos/patronesOptimos.py}
@*)
\end{pythoncode}
\end{document}
答案1
你不想要环境。
\begin{filecontents*}{\jobname.py}
print("Hello, World!")
\end{filecontents*}
\begin{filecontents*}{\jobname.run}
model cut.mod;
data cut.dat;
option solver cplex, solution_round 6;
option display_1col 0, display_transpose -10;
problem Cutting_Opt: Cut, Number, Fill;
option relax_integrality 1;
problem Pattern_Gen: Use, Reduced_Cost, Width_Limit;
option relax_integrality 0;
let nPAT := 0;
for {i in WIDTHS} {
let nPAT := nPAT + 1;
let nbr[i,nPAT] := floor (roll_width/i);
let {i2 in WIDTHS: i2 <> i} nbr[i2,nPAT] := 0;
}
repeat {
solve Cutting_Opt;
let {i in WIDTHS} price[i] := Fill[i].dual;
solve Pattern_Gen;
if Reduced_Cost < -0.00001 then {
let nPAT := nPAT + 1;
let {i in WIDTHS} nbr[i,nPAT] := Use[i];
}
else break;
}
display nbr, Cut;
option Cutting_Opt.relax_integrality 0;
solve Cutting_Opt;
display Cut;
\end{filecontents*}
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{newfloat}
\colorlet{mGreen}{green!65!blue}
\colorlet{mOrange}{red!50!green}
\colorlet{mGray}{black!60}
\colorlet{mPurple}{red!70!blue}
\newcommand{\genericcode}[3]{%
% #1 = language (long), #2 = language (short), #3 = style to load
\DeclareFloatingEnvironment[placement={!htp},name=#1 code]{#1Float}%
\expandafter\newcommand\csname #2code\endcsname[3]{%
\begin{#1Float}
\lstset{style=#3,columns=fullflexible}% <-- I can't stand the default
\lstinputlisting{##3}%
\caption{##1}\label{##2}
\end{#1Float}
}%
}
\genericcode{Python}{python}{mypy}
\genericcode{AMPL}{ampl}{StyleAMPL}
% You can now use e.g. \counterwithin in the following fashion
\counterwithin{PythonFloat}{section}
\lstdefinestyle{mypy}{
frame=single,
language=python,
rulecolor=\color{black},
commentstyle=\color{mGreen},
keywordstyle=\color{mOrange},
}
\lstdefinestyle{StyleAMPL}{
frame=single,
rulecolor=\color{black},
commentstyle=\color{mGreen},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{mGray},
stringstyle=\color{mPurple},
basicstyle=\footnotesize,
showstringspaces=false,
showtabs=false,
tabsize=3,
% language=AMPL,
}
\begin{document}
\section{Gives us a section number}
\pythoncode{Caption with section number}{PyLabel}{\jobname.py}
\amplcode{Caption without section number}{AMPLLabel}{\jobname.run}
\end{document}
笔记。我注释掉了,language=AMPL
因为我不知道应该如何定义它。我还添加了columns=fullflexible
,因为我总是对按列对齐的可变大小字符列表感到恐惧。
答案2
找到了!我还在处理细节问题,但主要问题已经解决了。
\lstinputlisting
解决方案是使用在内部创建一个新的浮动环境usepackage{newfloat}
。
我只需要找出如何[h!]
在新的浮动环境中添加放置参数。
\documentclass{article
\usepackage{listings}
\usepackage{newfloat}
\input{colors.tex} % mOrange, mGreen and so
\input{AMPL.tex} % where is \lstdefinelanguage{AMPL}{...}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% defining the styles
\lstdefinestyle{mypy}{
frame=single,
language=python,
rulecolor=\color{black},
commentstyle=\color{mGreen},
keywordstyle=\color{mOrange},
}
\lstdefinestyle{StyleAMPL}{
frame=single,
rulecolor=\color{black},
commentstyle=\color{mGreen},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{mGray},
stringstyle=\color{mPurple},
basicstyle=\footnotesize,
showstringspaces=false,
showtabs=false,
tabsize=3,
language=AMPL
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% defining the floats
\DeclareFloatingEnvironment[placement={!ht},name=Python code]{PythonFloat}
\newcounter{pythoncodecount}
\newenvironment{pythoncode}[1]{
\begin{PythonFloat}[tb]
\lstinputlisting[style = mypy]{#1}
}{\addtocounter{pythoncodecount}{1}\end{PythonFloat}}
\DeclareFloatingEnvironment[placement={!ht},name=AMPL code]{AmplFloat}
\newcounter{amplcodecount}
\newenvironment{amplcode}[1]{
\begin{AmplFloat}[tb]
\lstinputlisting[style = StyleAMPL]{#1}
}{\addtocounter{amplcodecount}{1}\end{AmplFloat}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% the document
\begin{document}
\begin{pythoncode}{Codigos/patronesOptimos.py}
\caption{a}
\label{a}
\end{pythoncode}
\begin{amplcode}{Codigos/ResultadoPatronesOptimos.tex}[h!]
\caption{b}
\label{b}
\end{amplcode}
\end{document}