2

I would like to include listings with source code in ConTeXt MKIV and have a mechanism for labeling and referencing the listing and even being able to attach the code with useattachment.

In LaTeX, I would write this with the help of the listings package:

\begin{figure}
  \centering
  \lstinputlisting[style=code,caption={XML document},label=lst:xml_book]{xml/book.xml}
\end{figure}

LaTeX chooses the right placement for the figure at execution time. I don't know how ConTeXt handles this. We can see the label, the caption and the path to the included file. I have difficulties to tell ConTeXt where the source file is.

Moreover, I saw an exemple allowing the author to include an attachment with the source code in the PDF file. That would be interesting to do as well even if I could use an URL to the source code instead.

\define[1]\Ex{\externalfigure[code/#1]
\useattachment[#1][code/#1.scd]
\attachment[#1]
}

But I couldn't make it work because my source code file couldn't be found. I tried every combination of directories/files I could think of without any success.

What I managed to do is put the source code inside the ConTeXt file (of course I would like to include the code from an external file).

\definetyping[XML]

\setuptyping
    [XML]
    [numbering=line,
  option=none,
    bodyfont=small,
    location=intext,
    before={\startframedcode},
    after={\stopframedcode}]

\startXML
<?xml version="1.0" encoding="UTF-8"?>
<book added_at="2011/07/15 15:02">
    ...
</book>
\stopXML

I checked the documentation, various sources but it didn't help. Could someone post a complete working example ?

4

Let's proceed in steps:

1. Showing file listing

  • The command \typefile displays the file verbatim. So

    \typefile[xml/book.xml]
    

    will display the XML code, but without syntax highlighting.

  • To enable syntax highlighting with \typefile use \typefile[option], where option is either TEX, LUA, or XML. So, to get syntax highlighted XML code, use:

    \typefile[XML][xml/book.xml]
    

    It is possible to write parsers for other languages, but an easier option is to use t-vim module.

2. Floating environment for code listing

  • If you want to replicate, the LaTeX example that you provided, you can use

    \startplacefigure
        [
          location={here,top}, 
          title={XML Document},
          reference={lst:xml_book},
        ]
        \typefile[XML]{code/book.xml}
    \stopplacefigure
    
  • If you don't want a separate float category for code listings, you can define a new one using:

    \definefloat[listing][listings]
    

    The second argument is used in the macros to display a list of listings (\placelistoflistings and \completelistoflistings, similar to \placelistoffigures and \completelistoffigures).

    \starttext
    \startplacelisting
        [
          location={here,top}, 
          title={XML Document},
          reference={lst:xml_book},
        ]
        \typefile[XML]{code/book.xml}
    \stopplacelisting
    \stoptext
    

3. Adding attachments

  • To ensure that attachments work, you need to enable interaction using:

    \setupinteraction[state=start]
    

    Although, the old method of using attachment still works, the new (MkIV) syntax is cleaner:

    \attachment[file=code/book.xml, title={View Source Code}, name=book.xml, author=Ludovic Kuty]
    

    file is the name of the file that you want to include (relative to the directory from which context is being run); name is the name that the file should be given when open from Acrobat. Note that you should test the PDF using Adobe Acrobat (Reader or Pro). Other PDF readers may not support all PDF features.

Complete working example

\definefloat[listing][listings]
\setupinteraction[state=start]

\starttext
\startplacelisting
    [
      location={here,top}, 
      title={XML Document\attachment[file=code/book.xml, title={View Source Code}, name=book.xml, author=Ludovic Kuty]},
      reference={lst:xml_book},
    ]
    \typefile[XML]{code/book.xml}
\stopplacelisting
\stoptext

You can wrap this behind a macro, if you wish.

Here is a screenshot of the output in Adobe Acrobat. I have opened the attachments tab in Acrobat to show that the attachment is included. Also note the attachment symbol near the title of the listing.

enter image description here

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.