# Using LaTeX listings throughout the document

I've got a LaTeX document with a main file and many sub-files. Because we're writing a documentation we use code snippets everywhere. How can I define a listing and use it throughout the documents?

Defining sample:

\lstset{language=[Objective]C, breakindent=40pt, breaklines}
\lstset{numbers=left, numberstyle=\tiny, numbersep=5pt}


Edit:
I changed it to:

\lstnewenvironment{ObjC}
{ \lstset{
language=[Objective]C,
...
}
}


And want to use it like that:

\begin{ObjC}
id foo = nil;
\end{ObjC}


What is wrong?

-
Welcome to TeX.sx! Please add a complete but minimal working example (MWE) that illustrates your problem. The code you added looks good to me and should work. What error(s) do you get? –  Martin Scharrer Dec 15 '11 at 12:25
Please share your ideas, expectations, problems in more detailed sentences. –  In PSTricks we trust Dec 15 '11 at 12:26
I would recommend you to define an own listings style using \lstdefinestyle{<name>}{<listings settings>} and then use it using \lstset{style=<name>}. This is IMHO cleaner and much better to set the settings globally, especially if you need to add some other listings later which should use a different style. –  Martin Scharrer Dec 15 '11 at 12:27
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 7 at 10:58

Personally, I prefer to define custom environments, so that instead of saying:

 \begin{listing}
...
\end{listings}


you can use your own environments. For example for LaTeX/TeX code I have defined three environments, with slight variations for each (teX, teXX and teXXX). I also define a macro called \emphasis to add words that I need to be emphasized:

\newcommand\emphasis[2][red]{\lstset{emph={write,void,writeln,#2},
emphstyle={\ttfamily\textcolor{#1}}}}%


Here is the code:

%% Emphasis
\renewcommand{\ttdefault}{cmtt}         % prefer old tt font
\newcommand\emphasis[2][red]{\lstset{emph={write,void,writeln,#2},
emphstyle={\ttfamily\textcolor{#1}}}}%

\lstnewenvironment{teXXX}[1][]
{\lstset{language=[LaTeX]TeX}\lstset{%
escapeinside={{(*@}{@*)}},
breaklines=true,
framesep=5pt,
basicstyle=\ttfamily,
showstringspaces=false,
keywordstyle=\ttfamily\textcolor{blue},
stringstyle=\color{orange},
rulecolor=\color{gray!10},
breakatwhitespace=true,
showspaces=false,  % shows spacing symbol
xleftmargin=0pt,
xrightmargin=5pt,
aboveskip=0pt, % compact the code looks ugly in type
belowskip=0pt,  % user responsible to insert any skips
backgroundcolor=\color{gray!15}, #1
}}
{}


The code is too long to turn into a MWE, but here is an image. Pages with code tend to look empty. To compensate I use tow columns for explanatory text in between. I tend to use very few colours, as I find them distracting. To emphasize, I normally use a bold bold font in the same colour as the other keywords. In the image below, I had it changed to red in order to make it visible in a reduced scan.

-