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

  • I think this answer can be readily adapted to produce different listings for different languages: https://tex.stackexchange.com/a/4068/106804 – oliversm Feb 14 '20 at 14:28

1 Answers1

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

oliversm
  • 2,717
  • Yes, this is exactly what I'm after, Thank you!!! – JPabloFuenzalida Feb 14 '20 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 – JPabloFuenzalida Feb 14 '20 at 17:09
  • @JPabloFuenzalida At a glance I'm not to sure. Perhaps you can ask it in its own question. – oliversm Feb 15 '20 at 18:41