Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In my document I have used:

\renewcommand\lstlistingname{Algorithm}
\renewcommand\lstlistlistingname{Algorithms} 

to rename my lstlistings titles.

The problem is that now I want to add a different kind of code,with lstinputlisting but I want it to have another caption below. Is there any way somehow to define a new listings environment and apply again the commands above to it?

For example ideally using two new commands \c_lstinputlisting{testc.c}, and \cpp_lstinputlisting{testc.cpp} would give two different caption names like

C Code 1.x

and

C++ Code 1.x

In case I am not clear, I am not talking about the caption itself, but what goes before the caption that is the general name of the caption name. Without editing it is the default

Listing 1.x

share|improve this question
    
Can you please expand a little on the way this is supposed to look like? It sounds as if you are mistaking the listing name for a caption but maybe I get you wrong. –  Max Jan 5 '13 at 11:20
    
Hi Max, yes sorry this was a mistake I meant the caption. Changed it, and also added some more stuff to make the question clearer. –  entropiece Jan 5 '13 at 11:48
    
@Vassilis Do you also need to produce distinct "List of C code", "List of C++ code" and so on? This would be more difficult. –  egreg Jan 5 '13 at 11:56
    
@egreg I don't care much for that now, I mainly want to have two different listing environments with two different caption names. Of course it would be great if they could be two different lists in the toc but this is not my main priority. –  entropiece Jan 5 '13 at 11:58
add comment

1 Answer

up vote 3 down vote accepted

You can define the commands so that they change locally the meanings of the involved control sequences:

\documentclass{book}
\usepackage{listings}
\lstset{numberbychapter}

\makeatletter
\newcommand{\cinputlisting}[2][]{%
  \begingroup\let\c@lstlisting\c@clisting
  \renewcommand\lstlistingname{C file}%
  \lstinputlisting[
    language=C,
    % <other specific options>
    #1]{#2}\endgroup}

\newcommand{\cppinputlisting}[2][]{%
  \begingroup\let\c@lstlisting\c@cpplisting
  \renewcommand\lstlistingname{C++ file}%
  \lstinputlisting[
    language=C++,
    % <other specific options>
    #1]{#2}\endgroup}
\makeatother

\newcounter{clisting}[chapter]
\newcounter{cpplisting}[chapter]

\begin{document}
\mainmatter
\chapter{One}
\cinputlisting[caption=something]{test.c}

\cppinputlisting[caption=whatever]{test.cpp}
\end{document}
share|improve this answer
    
That works fine. Thanks! –  entropiece Jan 5 '13 at 13:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.