0

I'm trying to write a document where I use AMPL, Python and I would also like to be able to show some of the outputs.

Problem is that the first python code is Script 1, the first AMPL is script 2, the second python is script 3, the output of the first python code is script 4, and so.

I want the different languages to be independent, a different counter and caption for the AMPL scripts, for the Python scripts and for the outputs.

Is this possible? Thank you in advance

1

1 Answer 1

0

Based on this answer to Listings: different counters for different listing environments I think this might be what you're after:

\documentclass{extarticle}

\usepackage{listings}

\newcounter{pythoncode}
\lstnewenvironment{pythoncode}[2]{
\renewcommand\lstlistingname{Python Code}
\setcounter{lstlisting}{\value{pythoncode}}
\lstset
{
language={},
basicstyle=\ttfamily,
frame = tb,
caption={[#1]{#1}},
label={#2},        
}
} {\addtocounter{pythoncode}{1}}

\newcounter{amplcode}
\lstnewenvironment{amplcode}[2]{
    \renewcommand\lstlistingname{AMPL Code}
    \setcounter{lstlisting}{\value{amplcode}}
    \lstset
    {
    language={},
    basicstyle=\ttfamily,
    frame = tb,
    caption={[#1]{#1}},
    label={#2},        
    }
} {\addtocounter{amplcode}{1}}

\begin{document}
\section{Examples}
\begin{pythoncode}{Hello world in Python 2}{alg-label}
print "Hello world"
\end{pythoncode}

\begin{pythoncode}{Hello world in Python 3}{another-alg-label}
print("Hello world")
\end{pythoncode}

\begin{amplcode}{\texttt{blend.mod}}{prg-label}
set INPUT;    # inputs
set OUTPUT;   # outputs
...
\end{amplcode}
\end{document}

enter image description here

3
  • Yes, this is exactly what I'm after, Thank you!!! Feb 14, 2020 at 14:49
  • Sorry for bothering you again, I'm having another trouble now... do you know if there is a way to merge this solution with lstinputlisting? My scripts are on files that I'm still working on and will change several times to. I tried giving the file in an argument and doing inputpath{#3}, but it didn't work Feb 14, 2020 at 17:09
  • @JPabloFuenzalida At a glance I'm not to sure. Perhaps you can ask it in its own question.
    – oliversm
    Feb 15, 2020 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.