Changes

Jump to navigation Jump to search
3,083 bytes added ,  10:27, 3 September 2017
scite pretty printing
=Displayed text=
<{{cmd>|starttyping</cmd>}},<{{cmd>|typebuffer</cmd>}}
Unlike LaTeX's <code>verbatim</code> environment, ConTeXt's <code>typing</code> environments have quite a range of built-in options for formatting the verbatim text, see below.
=In-line Inline text=
To typeset code inside the paragraph, you have the command <code>\startTEX ... \stopTEXtype</code> only works when typesetting the whole paragraph. If you need to type only a command or two as in-line text, you It can use the following be configured with <code>\setuptype[type][User:Taco|Taco's...]] macro (hopefully this will land in the core once in the future):</code>.
To have colored code, you need to use <texcodecode>\def\typeTEX {\bgroup \def\processinlineverbatim##1% {\processingverbatimtrue \localcatcodestrue \def\endofverbatimcommand{\TEXendofcommand##1}% \bgroup \aftergroup\endofverbatimcommand \futurelet\next\doprocessinlineverbatim}% \initializetyping{definetype[NAME][option=TEX}% \startverbatimcolor \processinlineverbatim\egroup }]</code> and use it with the name you chose. Other values are: <code>mp lua xml parsed-xml nested tex context</texcodecode>. An example that shows both features:
Now you can say:<context source="yes">\definetype[inlineTEX][option=TEX]
<texcode>\typeTEX+\typeTEX{\typeTEX This is a wonderful macro}+, isn't it?</texcode><context>\setupcolors[state=start]\defthe way to type code: \typeTEX type{the \bgroup \def\processinlineverbatim##1% {\processingverbatimtrue \localcatcodestrue \def\endofverbatimcommand{\TEXendofcommand##1}% \bgroup \aftergroup\endofverbatimcommand \futurelet\next\doprocessinlineverbatim}% \initializetyping{TEX}% \startverbatimcolor \processinlineverbatimConTeXt\egroup command}.
And this is the way to have colored code: \typeTEX+inlineTEX{the \typeTEX{ConTeXt\typeTEX is a wonderful macrocommand}+, isn't it?
</context>
 
Source: [http://archive.contextgarden.net/thread/20050907.093522.baf795f5.en.html]
= Making a tight vertical fit =
\stopC
</context>
 
In mkiv option-commands is gone and escape no longer is an escape character but a condition (range or start), as in this example:
 
<texcode>
\setuptyping[TEX][escape=yes]
 
\startTEX
/BTEX\em sometex/ETEX
/BTEX\em sometex/ETEX \after
\before /BTEX\em sometex/ETEX
\before /BTEX\em sometex/ETEX \after
\before /BTEX\em sometex/ETEX \inbetween /BTEX\em sometex/ETEX \after
\before \after
\stopTEX
 
\setuptyping[TEX][escape={[[,]]}]
 
\startTEX
[[\em sometex]]
[[\em sometex]] \after
\before [[\em sometex]]
\before [[\em sometex]] \after
\before [[\em sometex]] \inbetween [[\em sometex]] \after
\before \after
\stopTEX
 
\setuptyping[TEX][escape=//]
 
\startTEX
//\em sometex
\before //\em sometex
\stopTEX
</texcode>
 
[[Image:Verbatim-mkiv.png‎]]
 
= Pretty printing =
The context distribution includes a number of formatters (or pretty
printers), that can apply some nice formatting to your verbatim text.
Each formatter knows how to handle a specific file format or programming
language and applies syntax highlighting or other fancy displaying.
 
ConTeXt includes a number of predefined formatters, but it is also
possible to define your own. Pretty printing has been completely
redesigned in MkIV, so there are a few differences between MkII and
MkIV. In particular, MkII commonly refers to "formatters", while MkIV
often uses "pretty printers".
== Included Formatters ==
 There are already some formatters for several programming languages in the ConTeXt distribution:distributionr. The following list is for MkII and MkIV.
{|
! Language !! Code for MkII !! LanguageCode for MkIV |- | C || [[source:verb-c.mkii|C]] || [[modules:pret-c|C]]
|-
| Eiffel || [[source:verb-ceif.texmkii|CEIF]] || C
|-
| JavaScript || [[source:verb-eifjs.texmkii|EIFJS]] || Eiffel
|-
| Java || [[source:verb-jsjv.texmkii|JSJV]] || JavaScript
|-
| Lua || || [[source:verbpret-jvlua.texlua|JVLUA]] || Java
|-
| MetaPost & MetaFont || [[source:verb-mp.texmkii|MP]] || MetaPost & MetaFont[[source:pret-mp.lua|MP]]
|-
| Pascal & Modula || [[source:verb-pas.texmkii|PAS]] || Pascal & Modula
|-
| Perl 5 || [[source:verb-pl.texmkii|PL]] || Perl 5
|-
| SQL || [[source:verb-sql.texmkii|SQL]] || SQL
|-
| TeX || [[source:verb-tex.texmkii|TEX]] || TeX[[source:pret-tex.lua|TEX]]
|-
| XML || [[source:verb-xml.texmkii|XML]] || [[source:pret-xml.lua| XML]]
|-
|}
== Your own formatter ==
 
(from a ML answer by Taco, 2006-12-25, "Making a verbatim file for another language")
You should start from the definition that is closest to the language you want to define (especially wrt comment and string syntax) and can probably have a go at making a version of this. Some
questions occur:
 
=== How should I name my file? ===
; MkII: <tt>verb-xxxx.tex</tt>, where <tt>xxxx</tt> is the lowercase name of the formatter.
; MkIV: <tt>pret-xxxx.lua</tt>, where <tt>xxxx</tt> is the lowercase name of the pretty printer.
=== Where should i put my file? ===
=== Do I have to do anything else to get ConTeXt to recognise and use it? ===
Your file has You refer to have a pretty printer using its name like (''e.g.'', the<tt>xxxx</tt> in <tt>verb-xxxx.tex</tt>, with or <tt>pret-xxxx.lua</tt> a ),which should be in lowercase short identifier (example: verb-php.tex)Before you can use a pretty printer, and then you have to use one or two commands to make should let ConTeXt use know about it (this has already been done for the fileincluded formatters).
First:
<texcode>
\installprettytype [PHP] [PHP]
</texcode>
This maps the command <code>\starttyping[option=PHP]</code> to <tt>verb-php.tex</tt>.
This maps the command <code>\starttyping[option=PHP]</code> to the<tt>php</tt> pretty printer. The first argument is the option as passed to \starttyping, the second argument is an uppercase version of the pretty printer name(''e.g.'' <tt>xxxx</tt> ) (thus allowing ConTeXt to find the file),.
And you probably also want:
\definetyping[PHP] [option=PHP]
</texcode>
Because that allows <code>\startPHP ... \stopPHP</code> as a shortcut to <tt>\setuptypingstarttyping[option=PHP]... \stoptyping</tt>.
=== Wait for LuaTeX How to write the pretty printer? ===For MkII, there seems to be no real documentation here. You should takea good look at the existing formatters and base your work off those.However, if you really need custom pretty printing, you might be betterof to look at MkIV instead, since that allows you to write a prettyprinter in lua, which is a lot less painful than writing them in plainTeX.
Unless For MkIV, you need should simply write a lua script that highlighting urgently, I would wait for LuaTeX processes each lineof input and write it produces tex output. The details about this are discussedat [[Custom_pretty_printer]]. == Improved pretty printing == Default ConTeXt comment handling can be shown in lua the following sample: <context source="yes">\starttext\startbuffer[texcode]This is text. % and this is a comment\stopbuffer \startbuffer[xmlcode]<p>This is text.<!--and this is a comment--></p>\stopbuffer \startbuffer[luacode]if code=="code" then (which --this is a comment --[[this is certainly less painful than writing it in plain TeX). But it's up to a multiline comment--]] ---[[but this isn’t a multiline comment--]]\stopbuffer \typebuffer[texcode][option=TEX]\typebuffer[xmlcode][option=XML]\typebuffer[luacode][option=LUA]\stoptext</context> If you. I found those TeX definitions too complicated want to even try improve Lua multiline comments and to understandcolor the whole comments (not only the comment marker), write <code>\usemodule[scite]</code> before <code>\starttext</code>. (I would never dare cannot show it here, since ConTeXt at the wiki seems to try writing one by myselfhave a problem with that. -- Mojca)
= Tabbing =
= Line numbering =
In technical documents it can be required to display some code listings with the line numbers. You can do this with ConTeXt, and even more. All the features shown here are also available when displaying external files with <{{cmd>|typefile</cmd>}}.
== Preliminary setting ==
== Line numbers in the text ==
To have the line numbers in the text, change the linenumbering setup like this(<tt>location=text</tt> for mkiv resp.):
<texcode>
\stopcode
</context>
 
== Referencing line numbers ==
The following example from the mailing list [http://archive.contextgarden.net/thread/20101026.191201.55344b94.en.html#20101026.191201.55344b94] shows how to reference line numbers from a typing in the surrounding text (MkIV).
 
<texcode>
\setuptyping[numbering=line,escape=yes]
 
\starttext
 
See \inline[line:single] and also \inline[line:range].
 
See \inline{typeline}[line:single] and also \inline{typelines}[line:range].
 
See line~\inlinerange[line:single] and also lines~\inlinerange[line:range].
 
\starttyping
line 1
line 2/BTEX\startline[line:range]/ETEX
line 3/BTEX\someline[line:single]/ETEX
line 4
line 5/BTEX\stopline[line:range]/ETEX
line 6
\stoptyping
 
\stoptext
</texcode>
It is fairly simple to write a little parser which takes as its input your source file and gives in output a .tex file containing the code plus the opportune commands for colorization. You can then include the resulting .tex file in the global ConTeXt document file.
As an example, [http://www.cirmafonurgia.unito.it/andrea/sw/sc/scToConTeXt.py this] is a Python script for the SuperCollider language, which has a strict Smalltalk-like syntax.
Fed with the following source code contained in e.g. example.sc file:
<texcode>
\startSC
/BTEX{\color[SCred] {//// an example starting with a comment}/ETEX/BTEX{\color[SCgrey] {"this is a string"}/ETEX .postln ;
/BTEX{\color[SCgreen] {\letterbackslash aSymbol}/ETEX ;
/BTEX{\color[SCblue] {ThisClassDoesNotExists}/ETEX ;
\stopSC
</texcode>
The example.tex file uses some definitions (explained in the previous sections) which have to be put in the ConTeXt file(use <tt>escape=yes</tt> instead of <tt>option=commands</tt> for mkiv).
<texcode>
139

edits

Navigation menu