3

I'm trying to typeset a lot of C code in the latest ConTeXt (MKIV). It tried:

 \startC
    printf("Hello, world\n");
 \stopC

only to find that \startC macro is undefined. It is defined in verb-c.mkii. I even tried to put

\input verb-c.mkii

before using \startC but it fails on another undefined macros. What is the proper way to typeset C? The manual recommends \startC but it doesn't state how to switch it on except of \definetyping (which doesn't hightlight the syntax).

It actually works in MKII version of ConTeXt but it fails in MKIV.

3
  • Welcome to TeX.SX!. When asking questions it is better to provide a full minimal working example (MWE) both in order to demonstrate what you are trying to do and to help others help you. The MWE should look like \documentclass...\begin{document}...\end{document}, it should compile and contain close to the minimal amount of code needed to explain/demonstrate what you are asking. This saves a lot of time for everyon
    – user30471
    Nov 4 '14 at 22:20
  • 1
    @Andrew ConTeXt is very different from LaTeX.
    – egreg
    Nov 4 '14 at 22:21
  • @egreg Oops, didn't notice that this was ConTeXt...
    – user30471
    Nov 4 '14 at 22:24
3

This ConTeXt Garden page lists the pret-c module. So the following should work.

\installprettytype[C][C]
\definetyping[C][option=C, tab=4] % defines \startC as shortcut for \starttyping[option=C]
\definetype[typeC][option=C, tab=4, style=tt] % defines \typeC
\definecolor[Ccomment][darkblue]

\starttext
bla bla \typeC{int main(){}} bla bla
\startC
int foo = 42;
\stopC
\stoptext

Maybe you need to install the module first:

tlmgr install context-C-Pretty-Printing
2

I am not sure if this works in ConTeXt but in LaTeX I have been using this to include my C++ code. You should be able to change this to work with C as well.

Preamble:

\usepackage{listings}
\usepackage{textcomp}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstset{
%backgroundcolor=\color{lbcolor},
tabsize=4,    
%   rulecolor=,
language=[GNU]C++,
    basicstyle=\scriptsize,
    upquote=true, % if true need to use \usepackage{textcomp}
    aboveskip={1.5\baselineskip},
    columns=fixed,
    showstringspaces=false,
    extendedchars=false,
    breaklines=true,
    prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
    frame=single,
    numbers=left,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    identifierstyle=\ttfamily,
    keywordstyle=\color[rgb]{0,0,1},
    commentstyle=\color[rgb]{0.026,0.112,0.095},
    stringstyle=\color[rgb]{0.627,0.126,0.941},
    numberstyle=\color[rgb]{0.205, 0.142, 0.73},
%        \lstdefinestyle{C++}{language=C++,style=numbers}’.
}
\lstset{
backgroundcolor=\color{lbcolor},
tabsize=4,
language=C++,
captionpos=b,
tabsize=3,
frame=lines,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
breaklines=true,
showstringspaces=false,
basicstyle=\footnotesize,
%  identifierstyle=\color{magenta},
keywordstyle=\color[rgb]{0,0,1},
commentstyle=\color{Green},
stringstyle=\color{red}
}

an in the document:

\lstinputlisting{\CppCode/SSTe.cpp}
2
  • 3
    Thanks for the LaTeX answer but the ConTeXt is completely different and it's not LaTeX-compatible. Nov 4 '14 at 22:21
  • 1
    Hm, to bad. Here i hoped I could help for once rather than only ask questions.
    – MatoBehr
    Nov 4 '14 at 22:26
0

DISCLAIMER: This doesn't answer the question, I misunderstood it and thought the interest was on MkII. As I say at the bottom of the answer I don't know what's the system in MkIV (nor in MkII :D).

Just copying from the link in the contextwiki you shared

\input verb-c.mkii

\setupcolors[state=start]
\setuptyping[option=color]

% define the colors to fit your document style
\definecolor[MYcolorone]  [r=.8,g=.5,b=.5]
\definecolor[MYcolortwo]  [r=.8,g=.5,b=.2]
\definecolor[MYcolorthree][r=.8,g=.5,b=.8]
\definecolor[MYcolorfour] [r=.8,g=.2,b=.5]

% define a palete using these four colors
\definepalet[MYcolors]
  [prettyone=MYcolorone,
   prettytwo=MYcolortwo,
   prettythree=MYcolorthree,
   prettyfour=MYcolorfour]

% XML code will be typeset with the palette you just defined
\definepalet[Ccolorpretty] [MYcolors] % the name is magic !

\starttext

\startC
  printf("Hello, world\n");
\stopC

\stoptext

enter image description here

It would be nice to have an official answer from someone that actually knows what's the system. Including the way of doing this in MkIV which seems to be the standard now (rather than MkII).

3
  • Thanks for another answer attempt. Yes, it is possible in mkii but I want to do it in mkiv (that's why i put the flag context-mkiv) and this very example doesn't work with mkiv. It reports undefined \useprettyidentifiers It looks we still need a solution. Nov 5 '14 at 11:26
  • I thought that flag was a mistake, from the question it seems that you want to use it in MkII. I think it would be better that you state the MkIV part in the question.
    – Manuel
    Nov 5 '14 at 11:32
  • Good point. I added a disclaimer about MKIV in the question. Nov 5 '14 at 11:46

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.