3
\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
\definecolor{chatbackcolor}{rgb}{0.95,0.95,0.92}
\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{chatbackcolor},
  showstringspaces=false,
  %framexleftmargin = 2em,
  xleftmargin = 1cm,
  xrightmargin = 1cm,
  extendedchars=true,
  captionpos=b,
  basicstyle=\small\rmfamily,
  %basicstyle=\footnotesize\ttfamily,
  tabsize=2,
  keywordstyle=\textbf,
  stringstyle=\textit,
  numbers=none,
  numberstyle=\small,
  % für schönen Zeilenumbruch
  breakautoindent  = true,
  breakindent      = 2em,
  breaklines       = true,
  postbreak        = ,
  prebreak         = \raisebox{-.8ex}[0ex][0ex]{\Righttorque},
  frameround=ftff,
  frame=single,
  literate={ö}{{\"o}}1
           {ä}{{\"a}}1
           {ü}{{\"u}}1, #1
}}{}

% 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}

\chapter{My first chapter}
\section{first}
\section{second}

\begin{chat}[caption={Greeting with hello}\label{chat-greeting}]
    Sadik: Hello
\end{chat}

As shown in \ref{chat-greeting} on page \pageref{chat-greeting} ...

\end{document}

I have defined an environment (with \lstnewenvironment) called chat. I want to reference this environment with \ref{label name}. But \ref returns the number of the chapter instead of the number of the listing. In this case it shows 2, but 1.1 is expected.

Some threads mentioned a refstepcounter, but I failed to use it correctly (and I don't know if it would be helpful)

enter image description here

2
  • What is \RightTorque? ;-)
    – user31729
    Apr 23, 2016 at 19:15
  • I actually have no idea. must be a copy&paste thing ... thanks for pointing out.
    – Sadık
    Apr 23, 2016 at 21:00

1 Answer 1

2

You need the listings key label={....}, not \label. The \label command would refer to the section 2 (which was typeset before), since this was the last counter being using in \refstepcounter before the caption come into action (which applies the listings counter)

\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
\definecolor{chatbackcolor}{rgb}{0.95,0.95,0.92}
\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{chatbackcolor},
  showstringspaces=false,
  %framexleftmargin = 2em,
  xleftmargin = 1cm,
  xrightmargin = 1cm,
  extendedchars=true,
  captionpos=b,
  basicstyle=\small\rmfamily,
  %basicstyle=\footnotesize\ttfamily,
  tabsize=2,
  keywordstyle=\textbf,
  stringstyle=\textit,
  numbers=none,
  numberstyle=\small,
  % für schönen Zeilenumbruch
  breakautoindent  = true,
  breakindent      = 2em,
  breaklines       = true,
  postbreak        = ,
%  prebreak         = \raisebox{-.8ex}[0ex][0ex]{\Righttorque},
  frameround=ftff,
  frame=single,
  literate={ö}{{\"o}}1
           {ä}{{\"a}}1
           {ü}{{\"u}}1, #1
}}{}

% 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}

\chapter{My first chapter}
\section{first}
\section{second}

\begin{chat}[caption={Greeting with hello},label={chat-greeting}]
    Sadik: Hello
\end{chat}

As shown in \ref{chat-greeting} on page \pageref{chat-greeting} ...

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .