TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}

I changed it to:

{ \lstset{

And want to use it like that:

 id foo = nil;

What is wrong?

share|improve this question
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. – kiss my armpit 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 '14 at 10:58

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


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:


Here is the code:

%% Emphasis
\renewcommand{\ttdefault}{cmtt}         % prefer old tt font

     showspaces=false,  % shows spacing symbol
     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.

enter image description here

share|improve this answer

Your Answer


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.