1

I recently asked this question: Independent caption and numbers on different lstlisting language script

And the answer for my need was to use \lstnewenvironment. Problem is that I'm still working on the codes, so I need the codes to be imported. I was previously using \lstinputlisting, and it seems to be incompatible with \lstnewenvironment.

Is there a way to give the input document to the \lstnewenvironment as it is done with \lstinputlisting?

I tried this with no luck:

\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}

Output is this (and of course it's not an empty Python file):

Output is this (and of course it's not an empty python file)


I also tried this, but it produces a timeout:

\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}

2 Answers 2

1

You don't want an environment.

\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}

enter image description here

Note. I commented out language=AMPL because I don't have any idea of how it should be defined. I also added columns=fullflexible, because I'm always horrified by listings in variable size characters aligned in columns.

0

Found it! I'm still working on details, but the main problem is solved.

The solution is to create a new float enviroment with \lstinputlisting inside, using usepackage{newfloat}.

I just have to find out how to add the placement [h!] parameter on the new float enviroment.

\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}

my solution

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .