Program listings

There are some things to watch out for when doing program listings. Let's say that we want to put the following fragment of code into DocBook:

#include <stdio.h>
int main ()
{
        int count = 3;
        while (count--)
                printf ("Count is now %d\n", count);
}

    

The problem is that <stdio.h> will be interpreted as an SGML tag. For this small fragment, it is easy to replace the <-signs with &lt;, but for larger code fragments there could easily be missed SGML entities.

A slightly easier way is to enclose it in a CDATA tag (i.e. literal text), like this:

<programlisting><![CDATA[
…
]]></programlisting>
   

The next thing to bear in mind is that Tab characters will not be expanded to spaces, so you will need to do all the indenting with spaces.

[Warning]Warning

There is currently a bug in xmltex that causes incorrect output for “--” (i.e. two dashes). Grab the latest PassiveTeX for an xmltex.tex and PassiveTeX distribution with this fixed.

Of course, Appendix A, DocBook XML source of this book needs to include quite a lot of source code (XML rather than C), and the approach I have taken in order to be sure that I haven't missed anything is to have the Makefile run sed on the original source, and just include the result as an entity. Take a look at Appendix A, DocBook XML source to see how that's done.