2

So I want to show some python, bash and terminal output using the listing package.

I searched for some language definitions for bash and python for the listing package and in the beginning it worked really good. Now I wanted to add some terminal output but I found that only the last defined language is used for all listings, no mather what language I manually define for the given listing.

I made up an basic example on overleaf:

https://www.overleaf.com/18688751pxczqpkbbkyg#/70340370/

Problem is, this example doesn't really gives the same result on overleaf as I am experiencing here on my local document.

What am I doing wrong? I just want to add some color highlighting for python and bash scripts and some blank code for the terminal output.

Edit: here's the code:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{color}
\usepackage[procnames]{listings}

\begin{filecontents}{00_python.py}
# Image processing
from PIL import Image
import numpy as np

image_dimensions = 224
# Image paths
train_dir = "/path/to/train"
validation_dir = "/path/to/train"
\end{filecontents}

\begin{filecontents}{00_terminal.txt}
test@ubuntu-vm:~/Desktop$ time ./00_preprocess_and_sort.sh
 Getting variables...Done

    input dir: /home/test/new/00_data/00_all
    sorted dir: /home/test/new/00_data/01_sorted
    backup dir (attr.): /home/test/new/00_data/00_all_backup_attributes
    backup dir: /home/test/new/00_data/00_all_backup
    id dir: /home/test/new/00_data/03_ids
    stats dir: /home/test/new/00_data/04_statistics
\end{filecontents}

\begin{filecontents}{00_bash.sh}
#!/ bin/ bash

printf " Getting variables ... "
INPUT_DIR =~/ new /00 _data /00 _all
ID_DIR =~/ new /00 _data /03 _ids
SORTED_DIR =~/ new /00 _data /01 _sorted
BACKUP_DIR_ATTRIBUTES =~/ new /00 _data /00 _all_backup_attributes
BACKUP_DIR =~/ new /00 _data /00 _all_backup
STATISTIC_DIR =~/ new /00 _data /04 _statistics
\end{filecontents}

\definecolor{keywords}{RGB}{255,0,90}
\definecolor{comments}{RGB}{0,0,113}
\definecolor{red}{RGB}{160,0,0}
\definecolor{green}{RGB}{0,150,0}

\lstset{language=python, 
        basicstyle=\itshape\small, 
        keywordstyle=\color{keywords},
        commentstyle=\color{comments},
        stringstyle=\color{red},
        showstringspaces=false,
        identifierstyle=\color{green},
        procnamekeys={def,class}
}

\lstset{language=bash,
        basicstyle=\bfseries,
        showstringspaces=false,
        commentstyle=\color{red},
        keywordstyle=\color{blue}
}

\begin{document}
\lstinputlisting[language=bash, breaklines=true, title={Bash script}, frame=tb]{00_bash.sh}
\lstinputlisting[language=python, breaklines=true, title={Python code}, frame=tb]{00_python.py}
\lstinputlisting[breaklines=true, title={Terminal output}, frame=tb]{00_terminal.txt}
\end{document}
Robert
  • 125
  • 1
    \lstset just sets the global listings options. That means that several \lstsets overwrite each other with the last one winning, furthermore their settings are overruled by local settings. I guess you want something like \lstdefinestyle (or maybe \lstdefinelanguage). (Also, please consider adding the code (in reduced form) directly to your question. Not all people like to follow links to third-party sites and you never know when that link becomes unreachable.) – moewe Aug 30 '18 at 09:18
  • Spooky ... just as I'm looking at the file somebody is editing it and changing between \lstset and \lstdefinestyle. – moewe Aug 30 '18 at 09:21
  • I am searching for a solution at the moment and it was me changing to lstdefinestyle but without giving me a proper solution as I wanted – Robert Aug 30 '18 at 09:23
  • 1
    Mhhh, then I don't quite understand what you want. To me it seems \lstdefinestyle works. Can you explain in more detail what you want, why and how \lstdefinestyle does not work and add a static version of the code that does not change to your question, please? – moewe Aug 30 '18 at 09:28
  • Ok I got it now with \lstdefinestyle. Somehow on overleaf the font highlighting is different from my local version. Thanks for your fast respose moewe :-) – Robert Aug 30 '18 at 09:28
  • 1
    Overleaf are running an older version of TeX live, I guess their listings is a bit outdated. Maybe some language definitions changed in the meantime, that could explain slight differences in highlighting. – moewe Aug 30 '18 at 09:29

1 Answers1

3

As moewe already mentioned in the comments, I needed to change from \lstset (which changes the global listing settings) to \lstdefinestyle to create different styles.

So for example, from

\lstset{language=bash,
        basicstyle=\bfseries,
        showstringspaces=false,
        commentstyle=\color{red},
        keywordstyle=\color{blue}
}
...
\lstinputlisting[language=bash, breaklines=true, title={Bash script}, frame=tb]{00_bash.sh}

to

\lstdefinestyle{bashstyle}{language=bash,
        basicstyle=\bfseries,
        showstringspaces=false,
        commentstyle=\color{red},
        keywordstyle=\color{blue}
}
...
\lstinputlisting[style=bashstyle, breaklines=true, title={Bash script}, frame=tb]{00_bash.sh}
Robert
  • 125