1

I want to have different listing environments, each with an own caption.

My try was this (vorlage.tex):

\documentclass{scrreprt}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{color}

\definecolor{codecolor}{rgb}{0.8,0.9,0.9}
\definecolor{chatcolor}{rgb}{0.9,0.9,0.8}
\usepackage{listings}% http://ctan.org/pkg/listings


\makeatletter
% --------------------------------------- Chat-Auszug
\newcommand{\lstlistchatname}{Chat-Auszug}
\lst@UserCommand\lstlistofchat{\bgroup
    \let\contentsname\lstlistchatname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{loc}}%
    \tableofcontents \egroup}
\lstnewenvironment{chat}[1][]{%
  \renewcommand{\lstlistingname}{Chat}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
  \lstset{
  backgroundcolor=\color{chatcolor}
}}{}

% --------------------------------------- Code
\newcommand{\lstlistcodename}{Code}
\lst@UserCommand\lstlistofpycode{\bgroup
    \let\contentsname\lstlistcodename
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{loc}}%
    \tableofcontents \egroup}
\lstnewenvironment{pycode}[1][]{%
  \renewcommand{\lstlistingname}{Code}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
  \lstset{
  backgroundcolor=\color{codecolor},
}}{}

% linksbündige Fußboten
\deffootnote{1.5em}{1em}{\makebox[1.5em][l]{\thefootnotemark}}

\typearea{14} % typearea berechnet einen sinnvollen Satzspiegel (das heißt die Seitenränder) siehe auch http://www.ctan.org/pkg/typearea. Diese Berechnung befindet sich am Schluss, damit die Einstellungen oben berücksichtigt werden

\usepackage{scrhack} % Vermeidung einer Warnung

Then using like this:

\input{vorlage.tex}
\begin{document}

\lstlistofchat
\lstlistofpycode

\begin{pycode}[caption={Import nltk}]
import nltk
\end{pycode}

\begin{chat}[caption={Talking...}]
Sadik: Hello
\end{chat}

\end{document}

This is based on this answer

The different background colors are applied, so the style lstset works. But the caption is not displayed at all.

enter image description here

For the first example the caption should be something like Chat-Auszug 1.1 Import NLTK and for the second example something like Code 1.1 Talking.... Both are not displayed. What am I missing?

Sadık
  • 327
  • 1
    Welcome to TeX.SX! Fragments of code are not really useful. Please provide a compilable document. And you need the float option, as far as I know –  Apr 04 '16 at 10:58
  • I updated the answer and added almost the whole document, although this is not going to help in my opinion. Why does it need to be compilable? I can't post thousands of lines of code here. What is the float option? – Sadık Apr 04 '16 at 11:43
  • The example should be compilable so that we can test the issue. It should be small so that we don't have to much work when trying to find out what's going on. So make a real sensible minimal example. Remove the parts that are not necessary to demonstrate your problem. – Ulrike Fischer Apr 04 '16 at 11:53
  • ok, now it's compilable. – Sadık Apr 04 '16 at 12:18

1 Answers1

2

The code holds specified parameters, but they haven't been told the environments to be used. See comments in code:

\documentclass{scrreprt}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{color}

\definecolor{codecolor}{rgb}{0.8,0.9,0.9}
\definecolor{chatcolor}{rgb}{0.9,0.9,0.8}
\usepackage{listings}% http://ctan.org/pkg/listings


\makeatletter
% --------------------------------------- Chat-Auszug
\newcommand{\lstlistchatname}{Chat-Auszug}
\lst@UserCommand\lstlistofchat{\bgroup
    \let\contentsname\lstlistchatname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{loc}}%
    \tableofcontents \egroup}
\lstnewenvironment{chat}[1][]{% <<--- is told to expect a parameter
  \renewcommand{\lstlistingname}{Chat}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
  \lstset{
  backgroundcolor=\color{chatcolor}, #1 %<--- forgot to use #1. 
}}{}

% --------------------------------------- Code
\newcommand{\lstlistcodename}{Code}
\lst@UserCommand\lstlistofpycode{\bgroup
    \let\contentsname\lstlistcodename
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{loc}}%
    \tableofcontents \egroup}
\lstnewenvironment{pycode}[1][]{%   <<--- is told to expect a parameter
  \renewcommand{\lstlistingname}{Code}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
  \lstset{
  backgroundcolor=\color{codecolor}, #1 %<---forgot to use #1, same as above
}}{}

% linksbündige Fußboten
\deffootnote{1.5em}{1em}{\makebox[1.5em][l]{\thefootnotemark}}

\typearea{14} % typearea berechnet einen sinnvollen Satzspiegel (das heißt die Seitenränder) siehe auch http://www.ctan.org/pkg/typearea. Diese Berechnung befindet sich am Schluss, damit die Einstellungen oben berücksichtigt werden

\usepackage{scrhack} % Vermeidung einer Warnung

\begin{document}

    \lstlistofchat
    \lstlistofpycode

    \begin{pycode}[caption={Import nltk}]
        import nltk
    \end{pycode}

    \begin{chat}[caption={Talking...}]
        Sadik: Hello
    \end{chat}

\end{document}
naphaneal
  • 2,614