3

I have defined two listings environments, one for Python and other for Bash, both have different settings, this is a minimal example of what i'm using:

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}

% Some definitions to use color
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9}  % for normal

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\footnotesize,
otherkeywords={self},
tabsize=1,
keywordstyle=\ttb\color{deepblue},
emph={__init__, update_port},
emphstyle=\ttb\color{deepred},
stringstyle=\ttb\color{deepgreen},
frame=single,
showstringspaces=false,
float=htpb,
numbers=left,
captionpos=b,
numbersep=5pt,
linewidth=0.95\linewidth,
xleftmargin=0.05\linewidth,
breaklines=true
breakwhitespace=false,
}}


% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}


% Bash style for highlighting
\newcommand\bashstyle{
   \lstset{
  language=bash,
  keywordstyle=\sffamily\ttm,
  basicstyle=\sffamily\ttm,
  numbersep=5pt,
  frame=tb,
  columns=fullflexible,
  backgroundcolor=\color{yellow!20},
  linewidth=0.95\linewidth,
  xleftmargin=0.05\linewidth,
  breaklines=true,
  captionpos=b
}}

% Bash environment
\lstnewenvironment{bash}[1][]
{
\bashstyle
\lstset{#1}
}
{}


\begin{document}

\lstlistoflistings

\section{test}

\begin{python}[caption=python example]
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla
\end{python}

\begin{bash}[caption=bash example]
$ sudo yum update -y
$ sudo yum install -y https://rdo.fedorapeople.org/rdo-release.rpm
$ sudo yum install packstack
\end{bash}

\end{document}

The result is like this:

result of the above code

As we can see both fall under the same Listings, however it is desirable to have two different listings for both of this styles, they should have separate lists, with separate index names and separate numbering. For example i should have a "Python code 1: python example" and one "bash code 1: bash example", i should also have a list of python codes and a list of bash codes.

Is it possible to do it? If so how can it be done?

Edited to be more clear.

  • To change your style depending on the language, you should use \lstdefinestyle{custom}. Have you tried to create two new environments based on the listing environment ? – Jérôme Dequeker Feb 29 '16 at 14:10
  • Yes i tried creating two environments, but they still show under the same listing environment. I want to have (if possible) two totally different things for both environments. I will try using the "\lstdefinestyle{custom}" to see the results. – Ettore Zugliani Feb 29 '16 at 14:52
  • What do you mean by they still show under the same listing environment ? Can you post the given MWE ? – Jérôme Dequeker Feb 29 '16 at 15:00
  • The question was pretty vague, i changed it to be more clear, can you please have a look again? – Ettore Zugliani Feb 29 '16 at 16:54
  • In essence you want two different list of listings, hmm not sure if listings provides that. – daleif Feb 29 '16 at 18:30
3

You can try this code and let me know if there is a problem. I combined information from here for the counters and here for the two different lists.

\documentclass{article}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings

\usepackage{xcolor}

% Some definitions to use color
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9}  % for normal

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\newcounter{python}
\newcounter{bash}

\makeatletter
% --------------------------------------- Python
\newcommand{\lstlistpythonname}{List of Python Codes}
\lst@UserCommand\lstlistofpython{\bgroup
    \let\contentsname\lstlistpythonname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lor}}%
    \tableofcontents \egroup}
\lstnewenvironment{python}[1][]{%
  \renewcommand{\lstlistingname}{Python Code}%
  \let\c@lstlisting=\c@python
  \let\thelstlisting=\thepython
  \xpatchcmd*{\lst@MakeCaption}{lol}{lor}{}{}%
  \lstset{language=Python,
  basicstyle=\footnotesize,
  otherkeywords={self},
  tabsize=1,
  keywordstyle=\ttb\color{deepblue},
  emph={__init__, update_port},
  emphstyle=\ttb\color{deepred},
  stringstyle=\ttb\color{deepgreen},
  frame=single,
  showstringspaces=false,
  float=htpb,
  numbers=left,
  captionpos=b,
  numbersep=5pt,
  linewidth=0.95\linewidth,
  xleftmargin=0.05\linewidth,
  breaklines=true
  breakwhitespace=false,
  #1}}
  {}
% --------------------------------------- Bash
\newcommand{\lstlistbashname}{List of Bash Codes}
\lst@UserCommand\lstlistofbash{\bgroup
    \let\contentsname\lstlistbashname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lop}}%
    \tableofcontents \egroup}
\lstnewenvironment{bash}[1][]{%
  \renewcommand{\lstlistingname}{Bash Code}%
  \let\c@lstlisting=\c@bash
    \let\thelstlisting=\thebash
  \xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
  \lstset{language=bash,
    keywordstyle=\sffamily\ttm,
    basicstyle=\sffamily\ttm,
    numbersep=5pt,
    frame=tb,
    columns=fullflexible,
    backgroundcolor=\color{yellow!20},
    linewidth=0.95\linewidth,
    xleftmargin=0.05\linewidth,
    breaklines=true,
    captionpos=b,
    #1}}
  {}
\makeatother
\begin{document}

\lstlistofpython
\lstlistofbash
%\lstlistofpseudocode
\begin{bash}[caption=bash example]
$ sudo yum update -y
$ sudo yum install -y https://rdo.fedorapeople.org/rdo-release.rpm
$ sudo yum install packstack
\end{bash}
\begin{python}[caption=python example]
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla
\end{python}
\begin{python}[caption=python example]
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla
\end{python}
\begin{bash}[caption=bash example]
$ sudo yum update -y
$ sudo yum install -y https://rdo.fedorapeople.org/rdo-release.rpm
$ sudo yum install packstack
\end{bash}

\begin{python}[caption=python example]
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla
\end{python}
\begin{python}[caption=python example25]
class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla
\end{python}

\end{document}
| improve this answer | |

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.