3

I need to place source code example in ConTeXt with compiled result.

code:
  ConTeXt code example 
result:
  compiled result

I can do it like that:

\mainlanguage[ru]

\usemodule  [simplefonts][size=12pt]
\setmainfont[dejavusans][expansion=quality,protrusion=quality]
\setmonofont[monaco][expansion=quality,protrusion=quality]

\usemodule [vim]
\defineframedtext [CODEBLOCK][background=color,backgroundcolor=lightgray,
                              width=\dimexpr(\textwidth-1cm),frame=off,offset=2mm]
\definevimtyping[TEX][syntax=context,before=\startCODEBLOCK,after=\stopCODEBLOCK]
\defineframedtext [CODERESULT][width=\textwidth,frame=off,offset=2mm]
\definedescription[descr][indenting=1cm,indentnext=yes,location=right,
  width=\textwidth,distance=\dimexpr(1cm-\textwidth),align={width,stretch}]

\starttext


\startdescr{code:} \hfill
\startTEX
\ConTeXt
\stopTEX
\stopdescr

\startdescr{result:} \hfill
\startCODERESULT
\ConTeXt
\stopCODERESULT
\stopdescr

\stoptext

but code duplication is not very convenient.

I tried this:

\mainlanguage[ru]

\usemodule  [simplefonts][size=12pt]
\setmainfont[dejavusans][expansion=quality,protrusion=quality]
\setmonofont[monaco][expansion=quality,protrusion=quality]

\usemodule [vim]
\defineframedtext [CODEBLOCK][background=color,backgroundcolor=lightgray,
                              width=\dimexpr(\textwidth-1cm),frame=off,offset=2mm]
\definevimtyping[TEX][syntax=context,before=\startCODEBLOCK,after=\stopCODEBLOCK]
\defineframedtext [CODERESULT][width=\textwidth,frame=off,offset=2mm]
\definedescription[descr][indenting=1cm,indentnext=yes,location=right,
  width=\textwidth,distance=\dimexpr(1cm-\textwidth),align={width,stretch}]

\def\texsample#1{
\startdescr{code:} \hfill
\startTEX
#1
\stopTEX
\stopdescr

\startdescr{result:} \hfill
\startCODERESULT
#1
\stopCODERESULT
\stopdescr
}


\starttext

\texsample{
\ConTeXt
}

\stoptext

and this is what I see when launch ConTeXt

(/usr/share/texmf-dist/tex/context/third/filter/t-filter.mkiv
loading         > Filter (ver: 2013.04.15)
resolvers       > modules > 'module-catcodes' is loaded
(/usr/share/texmf-dist/tex/context/third/filter/t-module-catcodes.tex
loading         > Module Catcodes (ver: 2011.12.17)
))
resolvers       > modules > 'syntax-highlight' is loaded
(/usr/share/texmf-dist/tex/context/third/vim/t-syntax-highlight.mkiv
loading         > Code syntax highlighting (ver: 2012.08.01)
resolvers       > modules > 'syntax-groups' is loaded
(/usr/share/texmf-dist/tex/context/third/vim/t-syntax-groups.tex
loading         > Syntax highlighting groups (ver: 2012.05.06)
resolvers       > modules > 'module-catcodes' is already loaded
)
resolvers       > modules > 'filter' is already loaded
))))
*

and nothing happens.

  • 2
    The standard way to do this is to use buffers. \startbuffer ... \stopbuffer to store content, \typebuffer to type it, and \getbuffer to process it. The vim module also allows you to typeset buffers (see documentation). – Aditya Jan 17 '14 at 6:42
  • Did not understand how \startbuffer can help. – s9gf4ult Jan 17 '14 at 6:54
3

Here is how to use buffers to typeset ConTeXt source. To customize the layout of the typed buffer use \setuptyping[buffer][...=...].

\starttext

\startbuffer[logo]
\ConTeXt
\stopbuffer

To typeset the \ConTeXt\ logo use
\typebuffer[logo][option=context]
\getbuffer[logo]

\stoptext

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.