7

At the moment I'm working on my Diploma thesis and after a long time of searching and trying, I still can't find a solution for my problem.

Long story short: I want to refer to a lstlisting-enviroment via \cref{} and I want to change the displayed name. I already did this for some other enviroments:

\renewcommand\figureautorefname{Abbildung}
\renewcommand\tableautorefname{Tabelle}
\renewcommand\sectionautorefname{Kapitel}
\renewcommand\subsectionautorefname{Kapitel}
\renewcommand\equationautorefname{Gleichung}

I need to use \cref{} because I implemented a special format for the first use of \cref{}.

The result should be something like:

This is a test-Code: Program-Code 1

instead of

This is a test-Code: Listing 1

Could anybody help me?

Thanks a lot!

Chris

Edit: I found the part, that causes the failure of the solution under this post: There is a part in the template of the university. In this part the german/english terms for supervisor/Examiner are defined for the titlepage. If I select the german version within the document-enviroment, Program-Code changes to Listing again. :-( I updated the Code.


Here is my example:

\documentclass{scrreprt}

\usepackage[T1]{fontenc}                %% ermöglicht Sonderzeichen
\usepackage{lmodern}                    %% verbessert die Darstellung der Sonderzeichen
\usepackage[ngerman, english]{babel}    %% deutsche oder englische Schreibweisen
\usepackage[utf8]{inputenc}             %% deutsche Tastatur wg. Umlauten
\usepackage[ngerman, english]{babel} 
\usepackage[fixlanguage]{babelbib}

\usepackage{subfig}                     %% Bilder => Unterbilder (a),(b),.. innerhalb einer Figure

\usepackage{hyperref}
\usepackage{xcolor}                     %% farbige Schrift und Schrifthinterlegung

\usepackage{listings}               %% SOURCE CODE
\lstset{%                                                       settings for the listing package:
    numbers=left, numberstyle=\tiny,%                           line numbers
    showstringspaces=false,%
    frame=b, framerule=1pt,%
    aboveskip=0.75\baselineskip, belowskip=0.75\baselineskip,%  space above and below listing
    breaklines, prebreak=\mbox{$\hookleftarrow$},%              break overlong lines and puts prebreak at the end
    stringstyle=\color{cyan}\ttfamily, % Farbe der String
    showspaces=false, % Leerzeichen anzeigen ?
    showtabs=false, % Tabs anzeigen ?
    xleftmargin=17pt,
    framexleftmargin=17pt,
    framexrightmargin=5pt,
    framexbottommargin=4pt,
    commentstyle=\color{8},
    showstringspaces=false, % Leerzeichen in Strings anzeigen ?
    morekeywords={__global__} % CUDA specific keywords
    }
\lstset{
    language=python,
    basicstyle=\ttfamily,
    keywordstyle=\color{orange},
}
\lstloadlanguages{python}
\renewcommand*\lstlistingname{Programm-Code}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

\renewcommand\figureautorefname{Abbildung}
\renewcommand\tableautorefname{Tabelle}
\renewcommand\sectionautorefname{Kapitel}
\renewcommand\subsectionautorefname{Kapitel}
\renewcommand\equationautorefname{Gleichung}

\usepackage[ngerman]{cleveref}
\crefname{listing}{Program-code}{Program-codes}  
\Crefname{listing}{Program-code}{Program-codes}

\usepackage{letltxmacro}
\AtBeginDocument{% 
  \LetLtxMacro\oldref\cref% 
  \renewcommand{\cref}[1]{% 
  \ifcsname[#1]\endcsname\relax%
  {\oldref{#1}}%
  \else%
  {\textbf{\oldref{#1}}%
  }%
  \expandafter\gdef\csname[#1]\endcsname{1}%
  \fi%
  }% 
}

%-----

\declarebtxcommands{english}{%
\def\btxprue#1{\protect\foreignlanguage{english}{Examiner}}%
\def\btxsuper#1{\protect\foreignlanguage{english}{Supervisor}}%
}
\declarebtxcommands{ngerman}{%
\def\btxprue#1{\protect\foreignlanguage{ngerman}{Prüfer}}%
\def\btxsuper#1{\protect\foreignlanguage{ngerman}{Hochschulbetreuer}}%
}

\declarebtxcommands{ngerman}{%
\renewcommand{\figurename}{Abb.}
\renewcommand{\tablename}{Tab.}
}
\declarebtxcommands{english}{%
\renewcommand{\figurename}{Fig.}
\renewcommand{\tablename}{Tab.}
}
%-----

\begin{document}
%% SPRACHAUSWAHL VOR TEXTBEGINN
\selectlanguage{ngerman}   % <-------
%\selectlanguage{english}
\selectbiblanguage{ngerman}
%%\selectbiblanguage{english}


This is a test-Code: \cref{lst:example1}\\
second reference: \cref{lst:example1}
\begin{lstlisting}[caption={Test},label={lst:example1}]
test
\end{lstlisting}

Beispieltabelle: \cref{tab:test}\\
second reference: \cref{tab:test}
\begin{table}[h]
\centering
 \captionabove{test-table}
 \begin{tabular}{|c|c|}
 \hline
 hello & world \\
 \hline
 \end{tabular}
\label{tab:test}
 \end{table}
\end{document}
  • By the way, those redefinitions you show apply to the \autoref command (from hyperref), not to \cref. – Torbjørn T. Feb 25 '14 at 20:38
  • You forgot to include the babelbib package. – Torbjørn T. Feb 25 '14 at 21:41
  • Oh, of course, my thesis includes babelbib and babel, I'm sorry. But it doesn't change the problem. Included are: \usepackage[ngerman, english]{babel} \usepackage[fixlanguage]{babelbib} – Mr_Nofreeze Feb 25 '14 at 21:44
  • No, but it makes your example compilable, which I consider useful. (\declarebtxcommands is defined by babelbib it seems.) – Torbjørn T. Feb 25 '14 at 21:52
  • Of course! It was my fault ;-) – Mr_Nofreeze Feb 25 '14 at 21:55
6
\crefname{listing}{Program-code}{Program-codes}  
\Crefname{listing}{Program-code}{Program-codes}

Add this after \usepackage{cleveref}. The first redefines the words (singular and plural) used for \cref, the second those for \Cref.

For your case there is some conflict with babel and the language selection commands. (There is some discussion around babel in section 10 of cleverefs manual.) Adding the \crefname after the \selectlanguage seems to fix the issue. Minimal example:

\documentclass{scrreprt}
\usepackage[ngerman,english]{babel}    
\usepackage{babelbib}

\usepackage{listings}               
\usepackage[ngerman]{cleveref}


\begin{document}
\selectlanguage{ngerman}   % <-------
%\selectlanguage{english}
\crefname{listing}{Program-code}{Program-codes}  
\Crefname{listing}{Program-code}{Program-codes}

This is a test-Code: \cref{lst:example1}
\begin{lstlisting}[caption={Test},label={lst:example1}]
test
\end{lstlisting}

\end{document}
  • Crazy sh**, it works in my minimal example, but it doesn't work in my Diploma thesis... There is still "Listing"... But thanks anyway! – Mr_Nofreeze Feb 25 '14 at 20:38
  • @Mr_Nofreeze Then you should try to create a new minimal example based on your thesis demonstrating the problem. – Torbjørn T. Feb 25 '14 at 20:41
  • I updated my code with a new description of my problem – Mr_Nofreeze Feb 25 '14 at 21:32
  • Perfect! That solves my problem! Many thanks!!! – Mr_Nofreeze Feb 25 '14 at 22:56

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.