Changes

Jump to navigation Jump to search
4,880 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\typeTEXdefinetype[NAME][option=TEX]</code> and use it with the name you chose. Other values are: <code>mp lua xml parsed-xml nested tex context</code>. {\bgroup \def\processinlineverbatim##1%An example that shows both features: {\processingverbatimtrue \localcatcodestrue<context source="yes"> \def\endofverbatimcommand{\TEXendofcommand##1}%definetype[inlineTEX][option=TEX] \bgroup This is the way to type code: \aftergroup\endofverbatimcommand \futurelet\next\doprocessinlineverbatim}% \initializetypingtype{TEX}% \startverbatimcolor the \processinlineverbatimConTeXt\egroup command}</texcode>.
Now you can sayAnd this is the way to have colored code:\inlineTEX{the \ConTeXt\ command},</context>
= Making a tight vertical fit =By default, typing environments include some blank space before the first line and after the last line of the verbatim text. This is because the <texcodett>\typeTEX+\typeTEX{\typeTEX is a wonderful macro}+, isn't it?before</tt> and <tt>after</texcodett>commands are set to <contexttt>\setupcolors[state=start]\def\typeTEX {\bgroup \def\processinlineverbatim##1% {\processingverbatimtrue \localcatcodestrue \def\endofverbatimcommand{\TEXendofcommand##1}% \bgroup \aftergroup\endofverbatimcommand \futurelet\next\doprocessinlineverbatim}% \initializetyping{TEX}% \startverbatimcolor \processinlineverbatim\egroup }blank</tt> by default. For example:
<context source="yes">\typeTEX+framed[offset=0mm,strut=no,align=right]{\typeTEX{starttypingFoo\typeTEX is a wonderful macrostoptyping}+, isn't it?
</context>
Source: [http:Here, we've added a <tt>\framed</tt> so you can see the extra spacing, but with <tt>offset=0mm</tt> and <tt>strut=no</tt> to prevent framed from introducing spacing, and <tt>align=right</archive.contextgardentt> to prevent framed from eating all newlines (not sure why that works like this).net To prevent this extra vertical space, we must unset the <tt>before</threadtt> and <tt>after</20050907.093522.baf795f5.en.htmltt> options:<context source="yes">\setuptyping[before=,after=]\framed[offset=0mm,strut=no,align=right]{\starttypingFoo\stoptyping}</context>
= Embedded formatting commands =
\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.texmkii|C]] || [[modules:pret-c| C]]
|-
| Eiffel || [[source:verb-eif.texmkii|EIF]] || Eiffel
|-
| JavaScript || [[source:verb-js.texmkii|JS]] || JavaScript
|-
| Java || [[source:verb-jv.texmkii|JV]] || Java
|-
| Lua || || [[source:verbpret-mplua.texlua|MPLUA]] || MetaPost & MetaFont
|-
| MetaPost & MetaFont || [[source:verb-pasmp.texmkii|PASMP]] || Pascal & Modula[[source:pret-mp.lua|MP]]
|-
| Pascal & Modula || [[source:verb-plpas.texmkii|PLPAS]] || Perl 5
|-
| Perl 5 || [[source:verb-sqlpl.texmkii|SQLPL]] || SQL
|-
| SQL || [[source:verb-texsql.texmkii|TEXSQL]] || TeX
|-
| TeX || [[source:verb-tex.mkii|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>. === 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.
=== Wait for LuaTeX ===For MkIV, you should simply write a lua script that processes each lineof input and produces tex output. The details about this are discussedat [[Custom_pretty_printer]].
Unless you need that highlighting urgently, I would wait for LuaTeX == Improved pretty printing == Default ConTeXt comment handling can be shown in 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 write it in lua 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 =
\stoptext
</context>
 
= Colored background =
 
<texcode>
\setupcolors[state=start]
 
\definetextbackground[verbatim]
[background=color,
backgroundcolor=yellow,
backgroundoffset=0cm,
offset=0.5cm,
frame=on,
framecolor=black,
location=paragraph,
color=black]
 
% Setup verbatim
\setuptyping[typing][margin=1cm,bodyfont=8.0pt,
before={\starttextbackground[verbatim]},
after={\stoptextbackground}]
 
\starttext
\starttyping
This is the first line.
This is the second line.
\stoptyping
\stoptext
</texcode>
= 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>
a) you are much more fluent in another programming language than TeX
b) you have a large project involving many code snippet snippets to be inserted and colorized
you can go for another, less elegant, way to format your verbatim text, which can include all the discussed ConTeXt benefits.
== Writing a parser/formatter ==
It is not difficult at all 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:
Fed with this source code contained in example.sc file:
<context>
<texcode>
\startSC
/BTEX{\color[SCred] {//// an example starting with a comment}/ETEX/BTEX{\color[SCgrey] {"this is a string"}/ETEX .postln ;
/BTEX{\color[SCgreen] \type{\}letterbackslash aSymbol}/ETEX ;
/BTEX{\color[SCblue] ThisClassDoesNotExist{ThisClassDoesNotExists}/ETEX ;
\stopSC
</texcode>
Basically, the idea is to envelope each item to be colorized inside a /BTEX /ETEX block.
Note that inside the block the text is no more verbatim, so you have to define some string replacement replacements for special characters (e.g. _, %, $ , \, etc). The As an example, the problem of the slash backslash (\) is solved here by wrapping the character within a type commandreplacing it with \letterbackslash.  You can then insert example. tex in your ConTeXt file with <texcode>\input example</texcode>  The example .tex file uses some definitions (explained beforein the previous sections) which have to be put in the global ConTeXt file(use <tt>escape=yes</tt> instead of <tt>option=commands</tt> for mkiv).
<texcode>
</texcode>
alsoAlso, example.tex requires color definitions, which are created following SuperCollider colorization interpreter scheme and have to be inserted in the global ConTeXt filetoo:
<texcode>
</texcode>
You can then insert example.tex in The previous definitions will be used for all the global ConTeXt file code files inserted with <texcode>\input .After compiling, this is the result of the discussed example</texcode>:
After compiling, this is the result.
<context>
/BTEX{\color[SCgrey] "this is a string"}/ETEX .postln ;
/BTEX{\color[SCgreen] \type{\}letterbackslash aSymbol}/ETEX ;
/BTEX{\color[SCblue] ThisClassDoesNotExistThisClassDoesNotExists}/ETEX ;
\stopSC
== Embedding the source code ==
If your document is inteded to provide code examples to be tested by the readers, the presence of format signs (e.g. line numbers) can create some troubles while copying and pasting the code text from your document to the language environment. More generally, the copy and paste approach, being slow and possbily possibly related to encodings, is not satisfying at all.
You can include source files in the pdf with a couple of ConTeXt commands:
</texcode>
 So that you can have this structure for each code snippet: <texcode>\input example \useattachment[ex][example.sc]\attachment[ex]</texcode>  The commands embed example.sc in the pdf, so that it is accessible by double-clicking on a generated icon. In this case the icon is placed after the colorized code frame.If you use Acrobat Reader (Preview.app in MacOSX does not provide support for the behaviour) example.sc will be open in the related application (ie.eg. SuperCollider.appin the discussed case).
139

edits

Navigation menu