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.

I want to create a table for a few blocks of source code. In LaTeX, I would do

\hline 
\begin{lstlisting}
abc;
def;
\end{lstlisting}
\tabularnewline

Is there a way to accomplish something like this in ConTeXt? I have tried \starttyping and \stoptyping within table cells, but the file fails to compile if I do that.

The reason I would like to use tables is that I could then use \placetable for floats, and also that I would have a clean way of referring to it in other parts of the document.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Use buffers. Store the content in a buffer using

\startbuffer[...] 
  ...
\stopbuffer

and retrieve it using \typebuffer. Here is a simple example with tables.

\startsetups table:setup
  \setupTABLE[each][each][frame=off]
  \setupTABLE[row][first][style=bold,color=white,background=color,backgroundcolor=black]
  \setupTABLE[row][last][bottomframe=on, framethickness=3bp]
\stopsetups
\starttext

\startbuffer[CPP]
#include <iostream.h>

void main()
{
  cout << "Hello World\n";
}
\stopbuffer

\startbuffer[ruby]
puts "Hello World"
\stopbuffer

\startTABLE[setups=table:setup]
  \NC C++                \NC Ruby              \NC \NR
  \NC \typebuffer[CPP]   \NC \typebuffer[ruby]  \NC \NR
\stopTABLE
\stoptext

which gives

enter image description here

share|improve this answer
    
This suits my document well. Thanks! –  prash Feb 15 '12 at 17:51
add comment

The first thing: Verbatin text in tables work:

\starttext

\starttabulate
    \NC Foo        \NC Bar \NC\AR
    \NC \type{Foo} \NC Bar \NC\AR
\stoptabulate

\bTABLE
    \bTR
        \bTD Foo \eTD
        \bTD Bar \eTD
    \eTR
    \bTR
        \bTD Foo        \eTD
        \bTD \type{Bar} \eTD
    \eTR
\eTABLE

\stoptext

Result: firstresult

The second thing is that you don't need a table to refer to your code. You can create your own floating type. However even that doesn't have to float. The following code creates a custom floating evironment, not perfect, but hopefully enough to get you started.

\definefloat [code] [codes]

\starttext

\input knuth

As illustrated in \in{code}[code:example].

\startplacecode [title=Some example code., reference=code:example]
    \starttyping
        int main(){
            return 0;
        }
    \stoptyping
\stopplacecode

\stoptext

It looks like this:

secondresult

share|improve this answer
    
Your first answer works only with single line text, so I can't use it. I like your second answer, and will use it later. For the moment, though, I need tables. –  prash Feb 15 '12 at 17:55
    
If you need more text, then buffers is the way to go, see Adityas anwser. –  Marco Feb 15 '12 at 18:08
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.