https://wiki.contextgarden.net/api.php?action=feedcontributions&user=Otared&feedformat=atomWiki - User contributions [en]2024-03-29T12:14:18ZUser contributionsMediaWiki 1.34.1https://wiki.contextgarden.net/index.php?title=Command/definefontfamily&diff=34359Command/definefontfamily2024-02-29T11:49:00Z<p>Otared: /* Example */</p>
<hr />
<div>{{Reference<br />
|name=definefontfamily<br />
|attributes=<br />
}}<br />
<br />
== [[Help:Reference|Syntax]] (autogenerated) ==<br />
<syntax>definefontfamily</syntax><br />
<br />
<br />
== [[Help:Reference|Syntax]] ==<br />
<br />
== Description == <br />
<br />
# typescript identifier<br />
# font class<br />
# existing font name<br />
<br />
== Example ==<br />
<br />
In the first example we create a new typeface with the name `mainface` which includes fonts from the TeX Gyre family. Even though there is no math used in this example it is better to set one because math symbols are sometimes used in text mode.<br />
<br />
<context mode="mkiv" source="yes" text="produces"><br />
\definefontfamily [mainface] [rm] [TeX Gyre Pagella]<br />
\definefontfamily [mainface] [ss] [TeX Gyre Heros]<br />
\definefontfamily [mainface] [mm] [TeX Gyre Pagella Math]<br />
<br />
\setupbodyfont[mainface]<br />
<br />
\starttext<br />
\startTEXpage[offset=2.5em]<br />
\rm Serif: TeX Gyre Pagella<br />
<br />
\ss Sans: TeX Gyre Heros<br />
\stopTEXpage<br />
\stoptext<br />
</context><br />
<br />
In this example we are switching the fonts which are used for the upright, italic etc. alternatives. With the help of the `style` keyword you access one of the predefined styles, by default `\definefontfamily` uses the following styles for each alternative:<br />
<br />
{|<br />
! Alternative !! Style<br />
|-<br />
| tf || regular<br />
|-<br />
| it || italic<br />
|-<br />
| sl || slanted<br />
|-<br />
| bf || bold<br />
|-<br />
|bi || bolditalic<br />
|-<br />
| sc || smallcaps<br />
|}<br />
<br />
<context mode="mkiv" source="yes" text="produces"><br />
\definefontfamily[changedstyles][rm][TeX Gyre Pagella][tf=style:italic,it=style:bold,bi=style:bolditalic,bf=style:regular]<br />
<br />
\setupbodyfont[pagella]<br />
<br />
\starttext<br />
\startTEXpage[offset=2.5em]<br />
\rm\tf Regular, \it Italic, \bf Bold and \bi BoldItalic.<br />
<br />
\switchtobodyfont[changedstyles]<br />
<br />
\rm\tf Regular, \it Italic, \bf Bold and \bi BoldItalic.<br />
\stopTEXpage<br />
\stoptext<br />
</context><br />
<br />
<context mode="mkiv" source="yes" text="produces"><br />
\definefontfamily[changedfiles][rm][TeX Gyre Pagella][it=file:texgyreherositalic,bi=file:texgyrecursorbolditalic]<br />
<br />
\setupbodyfont[changedfiles]<br />
<br />
\starttext<br />
\startTEXpage[offset=2.5em]<br />
\tf Regular, \it Italic, \bf Bold and \bi BoldItalic.<br />
</context><br />
<br />
<context mode="mkiv" source="yes" text="produces"><br />
\definefontfamily[changedfeatures][rm][TeX Gyre Pagella][tf=features:smallcaps,bf=features:none]<br />
<br />
\setupbodyfont[pagella]<br />
<br />
\starttext<br />
\startTEXpage[offset=2.5em]<br />
\tf ffi, \it ffi, \bf ffi, \bi ffi.<br />
<br />
\switchtobodyfont[changedfeatures]<br />
<br />
\tf ffi, \it ffi, \bf ffi, \bi ffi.<br />
\stopTEXpage<br />
\stoptext<br />
</context><br />
<br />
If it is not possible to install fonts system-wide, one can explicitly specify the filename of a font. Custom fonts often don't come with math symbols or a fitting monospaced font. One can replace these using Latin Modern which comes with ConTeXt.<br />
<br />
<context mode="mkiv" source="yes" text="produces"><br />
\definefontfamily [mainface][serif] [Latin Modern Roman]<br />
[tf=file:filename_serif_regular.ttf,<br />
it=file:filename_serif_italic.ttf,<br />
bf=file:filename_serif_bold.ttf,<br />
bi=file:filename_serif_bold_italic.ttf,<br />
sc=file:filename_serif_smallcaps.ttf]<br />
\definefontfamily [mainface][sans] [Latin Modern Sans]<br />
[tf=file:filename_sans_regular.ttf,<br />
it=file:filename_sans_italic.ttf,<br />
bf=file:filename_sans_bold.ttf,<br />
bi=file:filename_sans_bold_italic.ttf,<br />
sc=file:filename_sans_smallcaps.ttf]<br />
\definefontfamily [mainface] [mono] [Latin Modern Mono] [features=none]<br />
\definefontfamily [mainface] [math] [Latin Modern Math]<br />
\setupbodyfont [mainface,11pt] <br />
\starttext<br />
\startTEXpage[offset=2.5em]<br />
\tf ffi, \it ffi, \bf ffi, \bi ffi.<br />
<br />
\tt Monospaced<br />
<br />
$\sqrt{3x^2}$<br />
\stopTEXpage<br />
\stoptext<br />
<br />
</context><br />
<br />
== See also ==<br />
<br />
* {{cmd|setupfonts}}<br />
* {{cmd|definefont}}<br />
* {{cmd|definetypeface}}<br />
* {{cmd|definefontsynonym}}<br />
* {{cmd|definefontfile}}<br />
* {{cmd|definefallbackfamily}}<br />
* https://meeting.contextgarden.net/2021/talks/2021-09-23/definefontfamily.pdf<br />
<br />
[[Category:Command/Fonts|definefontfamily]]</div>Otaredhttps://wiki.contextgarden.net/index.php?title=find&diff=34354find2024-02-24T10:41:42Z<p>Otared: </p>
<hr />
<div>Begining with ConTeXt version 2024.02.22, the following new option {{code|find}} can be used to check a command in the sources:<br />
<br />
<texcode><br />
context --find="%\starttabulate"<br />
</texcode><br />
<br />
In this example the result will be a list of the files and line numbers where the command {{cmd|starttabulate}} appears.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=find&diff=34353find2024-02-24T09:39:40Z<p>Otared: An explanation of the option find which Hans H. sent on the maling list on 2022.02.22 18:41</p>
<hr />
<div>Begining with ConTeXt version 2022.02.22, the following new option {{code|find}} can be used to check a command in the sources:<br />
<br />
<texcode><br />
context --find="%\starttabulate"<br />
</texcode><br />
<br />
In this example the result will be a list of the files and line numbers where the command {{cmd|starttabulate}} appears.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=composing_characters&diff=32452composing characters2021-02-17T18:19:53Z<p>Otared: Created page with "Beginning with LuaMetaTeX (LMTX) versions after January 2021, there is a new mechanism for typing some composed characters, that is characters with some sort of diacritic sign..."</p>
<hr />
<div>Beginning with LuaMetaTeX (LMTX) versions after January 2021, there is a new mechanism for typing some composed characters, that is characters with some sort of diacritic signs, such as {{code|\'e}} in order to obtain « é », or {{code|\c c}} to obtain « ç ». Actually most of these characters have a UNICODE definition and can be typed directly from some keyboard configurations. But in order to have backward compatibility, and at the same time allow the user to type them through a command, the following commands are made available. <br />
<br />
First of all the command {{cmd|chr}} creates the specified composed character: for instance {{code|\chr{a acute}}} results in « á ». The same can be obtained with {{code|{\withacute a}}}<br />
<br />
Here are a few of them, the complete list can be seen by typesetting the file {{code|s-characters-combinations.mkxl}}, or by copying the code below and typesetting it with LMTX.<br />
<br />
<context mode="mkiv" source="yes" ><br />
\usemodule[characters-combinations]<br />
<br />
\setupbodyfont[dejavu]<br />
<br />
\starttext<br />
<br />
\showcharactercombinations<br />
<br />
\page<br />
<br />
\startlines<br />
\chr {a acute } á \withacute a<br />
\chr {o double acute } ő \withdoubleacute o<br />
\chr {a grave } à \withgrave a<br />
\chr {o double grave } ȍ \withdoublegrave o<br />
\chr {a circumflex } â \withcircumflex a<br />
\chr {e circumflex below} ḙ \withcircumflexbelow e<br />
\chr {a diaeresis } ä \withdiaeresis a<br />
\chr {a tilde } ã \withtilde a<br />
\chr {i tilde below } ḭ \withtildebelow i<br />
\chr {a macron } ā \withmacron a<br />
\chr {k line below } ḵ \withlinebelow k<br />
\chr {c cedilla } ç \withcedilla c<br />
\chr {s comma below } ş \withcommabelow s<br />
\chr {z hook below } ȥ \withhookbelow z<br />
\chr {a hook } ả \withhook a<br />
\chr {a ring } å \withring a<br />
\chr {a ring below } ḁ \withringbelow a<br />
\chr {m dot } ṁ \withdot m<br />
\chr {l middle dot } ŀ \withmiddledot l<br />
\chr {m dot below } ṃ \withdotbelow m<br />
\chr {a breve } ă \withbreve a<br />
\chr {a inverted breve } ȃ \withinvertedbreve a<br />
\chr {k caron } ǩ \withcaron k<br />
\chr {o stroke } ø \withstroke o<br />
\stoplines<br />
<br />
\chr{a double grave} \withdoublegrave{i} <br />
<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=Matrix_in_maths&diff=32092Matrix in maths2020-11-30T10:57:26Z<p>Otared: </p>
<hr />
<div>In order to type matrices in math formulas, there is the command {{cmd|definemathmatrix}}. Once a certain type of matrix is defined, one can also add a simple command in order to type matrices in a Matlab or Scilab format.<br />
<br />
Here are a few examples<br />
<br />
<context source = yes><br />
%% defining matrix with parentheses<br />
\definemathmatrix[pmatrix]<br />
[matrix:parentheses]<br />
[simplecommand=pmatrix]<br />
<br />
%% defining matrix with brackets<br />
\definemathmatrix[bmatrix]<br />
[matrix:brackets]<br />
[simplecommand=bmatrix]<br />
<br />
%% defining determinant with bars<br />
\definemathmatrix[determinant]<br />
[matrix:bars]<br />
[simplecommand=thedeterminant]<br />
\starttext<br />
Let $A$ be the matrix<br />
\startformula<br />
A := <br />
\startpmatrix<br />
\NC 2 \NC 3 \NR<br />
\NC 1 \NC 2 \NR<br />
\stoppmatrix.<br />
\stopformula<br />
Show that<br />
\startformula<br />
{\rm det}(A) = \startdeterminant<br />
\NC 2 \NC 3 \NR<br />
\NC 1 \NC 2 \NR<br />
\stopdeterminant = 1.<br />
\stopformula<br />
Compute the matrix $AB$ where $B$ is the matrix<br />
\startformula<br />
B := \pmatrix{2, -3 ; -1, 2}.<br />
\stopformula<br />
\stoptext<br />
</context><br />
It is possible to have different delimiters on the left and on the right. For instance:<br />
<context source = yes ><br />
\definemathmatrix[pvmatrix]<br />
[left={\left(\thinspace},<br />
right={\thinspace\right|}]<br />
<br />
\definemathmatrix[rpmatrix]<br />
[left={\left.\thinspace},<br />
right={\thinspace\right)}]<br />
<br />
\starttext<br />
\startformula<br />
\startpvmatrix<br />
\NC 11 \NC 12 \NC 13 \NR<br />
\NC 21 \NC 22 \NC 23 \NR<br />
\NC 31 \NC 32 \NC 33 \NR<br />
\stoppvmatrix<br />
\startrpmatrix<br />
\NC 0 \NR<br />
\NC 1 \NR<br />
\NC 2 \NR<br />
\stoprpmatrix<br />
\stopformula<br />
<br />
\stoptext<br />
</context><br />
<br />
In ConTeXt LMTX beginning version 2020-11-27, it is possible to create « block » matrices where the blocks are separated by vertical or horizontal lines (respectively {{cmd|VL}} and {{cmd|HL}}). For instance the above example can be typeset more easily by the following (the output may be incorrect on this wiki: if this is the case copy the code below and typeset it on your machine with ConTeXt LMTX):<br />
<br />
<context source = yes><br />
\definemathmatrix[mymatrix]<br />
[left={\left\lparent\thinspace}, % instead of the left parenthesis, one can use any other fence<br />
right={\thinspace\right\rparent}] % instead of the right parenthesis, one can use any other fence<br />
<br />
\starttext<br />
Here is $3 \times 3$ matrix with a column added to it<br />
\startformula<br />
\startmymatrix<br />
\NC 11 \NC 12 \NC 13 \VL 0 \NR<br />
\NC 21 \NC 22 \NC 23 \VL 1 \NR<br />
\NC 31 \NC 32 \NC 33 \VL 2 \NR<br />
\stopmymatrix<br />
\stopformula<br />
Here is a block matrix:<br />
\startformula<br />
\startmymatrix<br />
\NC A \VL B \NR<br />
\HL<br />
\NC C \VL D \NR<br />
\stopmymatrix<br />
\stopformula<br />
</context><br />
<br />
{{todo|regenerate examples after the wiki context has been updated}}<br />
<br />
The two commands {{cmd|VL}} and {{cmd|HL}} take an optional parameter which defines the thickness of the rule and its color. So {{cmd|HL|[3,red]}} will produce a red horizontal rule three times thicker than the default matrix rule. The rule thickness for a specific matrix is defined with the {{code|1=[rulethickness=]}} parameter of {{cmd|definemathmatrix}}.<br />
<br />
<context source=yes><br />
\definemathmatrix[mymatrix][matrix:parentheses][rulethickness=2pt]<br />
<br />
\starttext<br />
\startformula<br />
\startmymatrix<br />
\NC A \VL[3,green] B \NR<br />
\HL[10,red]<br />
\NC C \VL D \NR<br />
\stopmymatrix<br />
\stopformula<br />
</context><br />
<br />
A {{cmd|VL}} counts as a hidden cell. To interrupt a vertical line, use an empty cell.<br />
<br />
<context source=yes><br />
\definemathmatrix[mymatrix][matrix:parentheses]<br />
<br />
\starttext<br />
Weird matrix<br />
\startformula<br />
\startmymatrix<br />
\NC A \VL B \NR<br />
\HL[red]<br />
\NC C \NC \NC D \NR<br />
\stopmymatrix<br />
\stopformula<br />
<br />
This allows things like<br />
<br />
\startformula<br />
\startmymatrix<br />
\NC A \VL B \NR<br />
\HL<br />
\NC \NC C \NC \NR<br />
\stopmymatrix<br />
\qquad<br />
\startmymatrix<br />
\NC A_1 \VL B_1 \NR<br />
\HL<br />
\NC \vdots \NC \vdots \NC \vdots \NR<br />
\HL<br />
\NC A_n \VL B_n \NR<br />
\stopmymatrix<br />
\stopformula<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=Matrix_in_maths&diff=32091Matrix in maths2020-11-30T10:55:48Z<p>Otared: </p>
<hr />
<div>In order to type matrices in math formulas, there is the command {{cmd|definemathmatrix}}. Once a certain type of matrix is defined, one can also add a simple command in order to type matrices in a Matlab or Scilab format.<br />
<br />
Here are a few examples<br />
<br />
<context source = yes><br />
%% defining matrix with parentheses<br />
\definemathmatrix[pmatrix]<br />
[matrix:parentheses]<br />
[simplecommand=pmatrix]<br />
<br />
%% defining matrix with brackets<br />
\definemathmatrix[bmatrix]<br />
[matrix:brackets]<br />
[simplecommand=bmatrix]<br />
<br />
%% defining determinant with bars<br />
\definemathmatrix[determinant]<br />
[matrix:bars]<br />
[simplecommand=thedeterminant]<br />
\starttext<br />
Let $A$ be the matrix<br />
\startformula<br />
A := <br />
\startpmatrix<br />
\NC 2 \NC 3 \NR<br />
\NC 1 \NC 2 \NR<br />
\stoppmatrix.<br />
\stopformula<br />
Show that<br />
\startformula<br />
{\rm det}(A) = \startdeterminant<br />
\NC 2 \NC 3 \NR<br />
\NC 1 \NC 2 \NR<br />
\stopdeterminant = 1.<br />
\stopformula<br />
Compute the matrix $AB$ where $B$ is the matrix<br />
\startformula<br />
B := \pmatrix{2, -3 ; -1, 2}.<br />
\stopformula<br />
\stoptext<br />
</context><br />
It is possible to have different delimiters on the left and on the right. For instance:<br />
<context source = yes ><br />
\definemathmatrix[pvmatrix]<br />
[left={\left(\thinspace},<br />
right={\thinspace\right|}]<br />
<br />
\definemathmatrix[rpmatrix]<br />
[left={\left.\thinspace},<br />
right={\thinspace\right)}]<br />
<br />
\starttext<br />
\startformula<br />
\startpvmatrix<br />
\NC 11 \NC 12 \NC 13 \NR<br />
\NC 21 \NC 22 \NC 23 \NR<br />
\NC 31 \NC 32 \NC 33 \NR<br />
\stoppvmatrix<br />
\startrpmatrix<br />
\NC 0 \NR<br />
\NC 1 \NR<br />
\NC 2 \NR<br />
\stoprpmatrix<br />
\stopformula<br />
<br />
\stoptext<br />
</context><br />
<br />
In ConTeXt LMTX beginning version 2020-11-27, it is possible to create « block » matrices where the blocks are separated by vertical or horizontal lines (respectively {{cmd|VL}} and {{cmd|HL}}). For instance the above example can be typeset more easily by the following (the output may be incorrect on this wiki: if this is the case copy the code below and typeset it on your machine with ConTeXt LMTX):<br />
<br />
<context source = yes><br />
\definemathmatrix[mymatrix]<br />
[left={\left\lparent\thinspace}, % instead of the left parenthesis, one can use any other fence<br />
right={\thinspace\right\rparent}] % instead of the right parenthesis, one can use any other fence<br />
<br />
\starttext<br />
Here is $3 \times 3$ matrix with a column added to it<br />
\startformula<br />
\startmymatrix<br />
\NC 11 \NC 12 \NC 13 \VL 0 \NR<br />
\NC 21 \NC 22 \NC 23 \VL 1 \NR<br />
\NC 31 \NC 32 \NC 33 \VL 2 \NR<br />
\stopmymatrix<br />
\stopformula<br />
Here is block matrix:<br />
\startformula<br />
\startmymatrix<br />
\NC A \VL B \NR<br />
\HL<br />
\NC C \VL D \NR<br />
\stopmymatrix<br />
\stopformula<br />
</context><br />
<br />
{{todo|regenerate examples after the wiki context has been updated}}<br />
<br />
The two commands {{cmd|VL}} and {{cmd|HL}} take an optional parameter which defines the thickness of the rule and its color. So {{cmd|HL|[3,red]}} will produce a red horizontal rule three times thicker than the default matrix rule. The rule thickness for a specific matrix is defined with the {{code|1=[rulethickness=]}} parameter of {{cmd|definemathmatrix}}.<br />
<br />
<context source=yes><br />
\definemathmatrix[mymatrix][matrix:parentheses][rulethickness=2pt]<br />
<br />
\starttext<br />
\startformula<br />
\startmymatrix<br />
\NC A \VL[3,green] B \NR<br />
\HL[10,red]<br />
\NC C \VL D \NR<br />
\stopmymatrix<br />
\stopformula<br />
</context><br />
<br />
A {{cmd|VL}} counts as a hidden cell. To interrupt a vertical line, use an empty cell.<br />
<br />
<context source=yes><br />
\definemathmatrix[mymatrix][matrix:parentheses]<br />
<br />
\starttext<br />
Weird matrix<br />
\startformula<br />
\startmymatrix<br />
\NC A \VL B \NR<br />
\HL[red]<br />
\NC C \NC \NC D \NR<br />
\stopmymatrix<br />
\stopformula<br />
<br />
This allows things like<br />
<br />
\startformula<br />
\startmymatrix<br />
\NC A \VL B \NR<br />
\HL<br />
\NC \NC C \NC \NR<br />
\stopmymatrix<br />
\qquad<br />
\startmymatrix<br />
\NC A_1 \VL B_1 \NR<br />
\HL<br />
\NC \vdots \NC \vdots \NC \vdots \NR<br />
\HL<br />
\NC A_n \VL B_n \NR<br />
\stopmymatrix<br />
\stopformula<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=Matrix_in_maths&diff=32051Matrix in maths2020-11-29T07:45:12Z<p>Otared: Created page with "In order to type matrices in math formulas, there is the command {{cmd|definemathmatrix}}. Once a certain type of matrix is defined, one can also add a simple command in order..."</p>
<hr />
<div>In order to type matrices in math formulas, there is the command {{cmd|definemathmatrix}}. Once a certain type of matrix is defined, one can also add a simple command in order to type matrices in a Matlab or Scilab format.<br />
<br />
Here are a few examples<br />
<br />
<context source = yes><br />
%% defining matrix with parentheses<br />
\definemathmatrix[pmatrix]<br />
[matrix:parentheses]<br />
[simplecommand=pmatrix]<br />
<br />
%% defining matrix with brackets<br />
\definemathmatrix[bmatrix]<br />
[matrix:brackets]<br />
[simplecommand=bmatrix]<br />
<br />
%% defining determinant with bars<br />
\definemathmatrix[determinant]<br />
[matrix:bars]<br />
[simplecommand=thedeterminant]<br />
\starttext<br />
Let $A$ be the matrix<br />
\startformula<br />
A := <br />
\startpmatrix<br />
\NC 2 \NC 3 \NR<br />
\NC 1 \NC 2 \NR<br />
\stoppmatrix.<br />
\stopformula<br />
Show that<br />
\startformula<br />
{\rm det}(A) = \startdeterminant<br />
\NC 2 \NC 3 \NR<br />
\NC 1 \NC 2 \NR<br />
\stopdeterminant = 1.<br />
\stopformula<br />
Compute the matrix $AB$ where $B$ is the matrix<br />
\startformula<br />
B := \pmatrix{2, -3 ; -1, 2}.<br />
\stopformula<br />
\stoptext<br />
</context><br />
It is possible to have different delimiters on the left and on the right. For instance:<br />
<context source = yes ><br />
\definemathmatrix[pvmatrix]<br />
[left={\left(\thinspace},<br />
right={\thinspace\right|}]<br />
<br />
\definemathmatrix[rpmatrix]<br />
[left={\left.\thinspace},<br />
right={\thinspace\right)}]<br />
<br />
\starttext<br />
\startformula<br />
\startpvmatrix<br />
\NC 11 \NC 12 \NC 13 \NR<br />
\NC 21 \NC 22 \NC 23 \NR<br />
\NC 31 \NC 32 \NC 33 \NR<br />
\stoppvmatrix<br />
\startrpmatrix<br />
\NC 0 \NR<br />
\NC 1 \NR<br />
\NC 2 \NR<br />
\stoprpmatrix<br />
\stopformula<br />
<br />
\stoptext<br />
</context><br />
<br />
In ConTeXt LMTX beginning version 2020-11-27, it is possible to create « block » matrices where the blocks are separated by vertical or horizontal lines (respectively {{cmd|VL}} and {{cmd|HL}}). For instance the above example can be typeset more easily by the following (the output may be incorrect on this wiki: if this is the case copy the code below and typeset it on your machine with ConTeXt LMTX):<br />
<br />
<context source = yes><br />
\definemathmatrix[mymatrix]<br />
[left={\left\lparent\thinspace}, % instead of the left parenthesis, one can use any other fence<br />
right={\thinspace\right\rparent}] % instead of the right parenthesis, one can use any other fence<br />
<br />
\starttext<br />
Here is $3 \times 3$ matrix with a column added to it<br />
\startformula<br />
\startmymatrix<br />
\NC 11 \NC 12 \NC 13 \VL 0 \NR<br />
\NC 21 \NC 22 \NC 23 \VL 1 \NR<br />
\NC 31 \NC 32 \NC 33 \VL 2 \NR<br />
\stopmymatrix<br />
\stopformula<br />
Here is block matrix:<br />
\startformula<br />
\startmymatrix<br />
\NC A \VL B \NR<br />
\HL<br />
\NC C \VL D \NR<br />
\stopmymatrix<br />
\stopformula<br />
and here is another one<br />
\startformula<br />
\startmymatrix<br />
\NC A \VL[3,green] B \NR % <br />
\HL[10,red]<br />
\NC C \VL D \NR<br />
\stopmymatrix<br />
\stopformula<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=Programming_in_LuaTeX&diff=29422Programming in LuaTeX2020-06-24T15:12:58Z<p>Otared: /* Parsing input without exploding your head */</p>
<hr />
<div>= Calling Lua from TeX =<br />
<br />
The interweaving of ConTeXt and Lua consists of two elements: first you tell TeX that you're starting some Lua code; then, once inside Lua, you need to use the appropriate functions to put things into the TeX stream.<br />
<br />
There are two main ways to execute Lua code in a ConTeXt document: The command <code>\ctxlua</code>, and the environment <code>\startluacode...\stopluacode</code>. Both are wrappers around the LuaTeX primitive <code>\directlua</code>, which you should never need to use. In general, you will define a function inside a <code>\startluacode</code> block, and then define a TeX command that calls the function using <code>\ctxlua</code>, especially because <code>\ctxlua</code> has a few idiosyncracies.<br />
<br />
The main thing about Lua code in a TeX document is this: the code is expanded by TeX ''before'' Lua gets to it. '''This means that all the Lua code, even the comments, must be valid TeX!''' A string like "\undefined" will cause an immediate failure.<br />
<br />
== Calling a bit of Lua inline: \ctxlua ==<br />
<br />
The command <code>\ctxlua</code> is for short inline snippets of Lua, such<br />
as<br />
<br />
<texcode><br />
$2 + 5 \neq \ctxlua{context(3+5)}$, but is equal to \ctxlua{context(2+5)}.<br />
This is \ctxlua{context(string.upper("absolutely"))} true.<br />
</texcode><br />
<br />
<code>\ctxlua</code> operates under the normal TeX catcodes (category codes). This means the following two things for the Lua code inside:<br />
* all newlines get treated as spaces<br />
* special TeX characters like &, #, $, {, }, etc., need to be escaped. <br />
<br />
In addition, the warning above still holds. All the Lua code, even the comments, must be valid TeX.<br />
<br />
Some code to illustrate the newline problem:<br />
<texcode><br />
\ctxlua<br />
{-- A Lua comment<br />
tex.print("This is not printed")}<br />
\ctxlua<br />
{% A Tex comment<br />
tex.print("This is printed")}<br />
</texcode><br />
<br />
The problem with special TeX characters. (<code>#t</code> is Lua for 'the length of array <code>t</code>.)<br />
<texcode><br />
% This doesn't work:<br />
%\ctxlua<br />
% {local t = {1,2,3,4}<br />
% tex.print("length " .. #t)}<br />
\ctxlua<br />
{local t = {1,2,3,4}<br />
tex.print("length " .. \string#t)}<br />
</texcode><br />
<br />
== A larger Lua block: \startluacode...\stopluacode ==<br />
<br />
Inside the <code>\startluacode...\stopluacode</code> environment, newlines and special characters behave normally. This solves the catcode problem that <code>\ctxlua</code> suffers from. Apart from these special characters, the main warning remains in force: all the Lua code, even the comments, must be valid TeX.<br />
<br />
<texcode><br />
\startluacode<br />
-- The unknown command \undefined will cause this entire block to fail.<br />
<br />
-- Print a countdown '10, 8, ..., 0!'<br />
-- `..` is Lua for string concatenation<br />
for i = 10, 2, -2 do<br />
context(i .. ", ")<br />
end<br />
context("0!")<br />
<br />
-- \\par is equivalent to a blank line in the input<br />
-- (Notice the escaped backslash: TeX won't mind the above comment.)<br />
context.par()<br />
<br />
-- Look! we can use # and $ with impunity!<br />
context("Unless we print them, then we must \\#\\$\\& print the escape characters, too.")<br />
\stopluacode<br />
</texcode><br />
<br />
== Putting Lua code in an external file ==<br />
<br />
You can put your lua code in an external file (with the <code>.lua</code> extension) and include it with the <code>require</code> command:<br />
<br />
<texcode><br />
\startluacode<br />
-- include the file my-lua-lib.lua<br />
require("my-lua-lib")<br />
\stopluacode<br />
</texcode><br />
<br />
== Namespaces ==<br />
<br />
It is a good habit to put your custom-defined functions in their own namespace. The traditional namespace for this is <code>userdata</code>:<br />
<texcode><br />
\startluacode<br />
-- if userdata doesn't exist yet, create it<br />
userdata = userdata or {}<br />
-- define a shorter synonym<br />
u = userdata<br />
<br />
-- create my custom function inside the userdata namespace<br />
function u.myfunction()<br />
-- do stuff<br />
end <br />
\stopluacode<br />
</texcode><br />
<br />
The full list of canonical namespaces, taken from [http://minimals.contextgarden.net/current/context/alpha/tex/context/base/luat-ini.lua luat-ini.lua]:<br />
<br />
<code><pre><br />
userdata = userdata or { } -- for users (e.g. functions etc)<br />
thirddata = thirddata or { } -- only for third party modules<br />
moduledata = moduledata or { } -- only for development team<br />
documentdata = documentdata or { } -- for users (e.g. raw data)<br />
parametersets = parametersets or { } -- experimental for team<br />
</pre></code><br />
<br />
If your module, environment, or document is going to be used by other people, you should create your own subnamespaces within these tables.<br />
<br />
<code><pre><br />
moduledata['mymodule'] = { }<br />
mm = moduledata.mymodule<br />
function mm.mainfunction()<br />
-- do stuff<br />
end<br />
</pre></code><br />
<br />
= Calling Lua from TeX =<br />
<br />
One can execute a Lua code from within TeX and get back the result in TeX by using {{code|\cldcontext}}. Thus, if {{code|myfunction}} is a function of a variable {{code|x}} defined in Lua, {{code|\cldcontext{myfunction(5)}}} returns the value {{code|myfunction(5)}} in TeX. This is equivalent to {{code|\ctxlua{context(myfunction(5))}}}.<br />
<br />
<br />
Being a topic on itself, pages are dedicated:<br />
* '''[[CLD|ConTeXt Lua Documents]]''', or CLD, are way to access TeX from inside Lua scripts. A page give clues about [[CLD_passing_variables|passing variables]] within CLD (2018).<br />
* [[Lua|Wiki page dedicated to Lua]]<br />
** [[Extensions to the Lua I/O library]]<br />
** [[String manipulation]]<br />
** [[Table manipulation]]<br />
<br />
= Putting stuff in your TeX document from Lua =<br />
<br />
== Simple printing: context(), tex.print(), and tex.sprint() ==<br />
Use <code>context(...)</code> for most things. It is equivalent to <code>tex.print(string.format(...))</code>, so<br />
<br />
<texcode><br />
\startluacode<br />
name = "Jane"<br />
date = "today"<br />
context("Hello %s, how are you %s?", name, date)<br />
-- Becomes 'Hello Jane, how are you today?'<br />
\stopluacode<br />
</texcode><br />
<br />
More primitively, you have <code>tex.print()</code> and <code>tex.sprint()</code>. Either one can take as an argument either a number of strings, or an array of strings, and will then insert the strings into the TeX stream. The only difference is that <code>tex.print()</code> treats each string as a separate input line, while <code>tex.sprint()</code> doesn't. So the following lines<br />
<br />
<texcode><br />
\ctxlua{tex.print("a", "b")}<br />
\ctxlua{tex.print({"a", "b"})}<br />
</texcode><br />
<br />
are both interpreted by TeX as<br />
<br />
<texcode><br />
a<br />
b<br />
</texcode><br />
<br />
but when we use <code>tex.sprint</code> instead, either of the following<br />
<br />
<texcode><br />
\ctxlua{tex.sprint("a", "b")}<br />
\ctxlua{tex.sprint({"a", "b"})}<br />
</texcode><br />
<br />
will be read by TeX as<br />
<br />
<texcode><br />
ab<br />
</texcode> <br />
<br />
without any space in between.<br />
<br />
== Context commands ==<br />
<br />
Most commands that you would type with a backslash in plain ConTeXt, you can access from Lua with <code>context.<em>command</em></code>. Unadorned strings end up in TeX as arguments in curly braces; Lua tables end up in TeX as paramater blocks in square brackets. The following two pieces of code are equivalent:<br />
<br />
<texcode><br />
\startluacode<br />
context.chapter({first}, "Some title")<br />
context.startcolumns({n = 3, rule = "on"})<br />
context("Hello one")<br />
context.column()<br />
context("Hello two")<br />
context.column()<br />
context("Hello three")<br />
context.stopcolumns()<br />
\stopluacode<br />
</texcode><br />
<br />
<texcode><br />
\chapter[first]{Some title}<br />
\startcolumns[n=3, rule=on]<br />
Hello one<br />
\column<br />
Hello two<br />
\column<br />
Hello three<br />
\stopcolumns<br />
</texcode><br />
<br />
For a fuller account of the context.commands, see the [http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf ConTeXt Lua document] manual. It is old, but most of it still applies.<br />
<br />
One final note: arguments can also be specified in the form of nested functions. Because LuaTeX evaluates the deepest-nested argument first, this may cause the <code>context()</code> calls to be evaluated in the wrong order. For more on this, see the article on [[CLD|ConTeXt Lua documents]], and also, again, the [http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf CLD manual].<br />
<br />
= Passing arguments and buffers: ConTeXt commands that hook into Lua =<br />
<br />
== Making \command{arg1}{arg2} hook into Lua ==<br />
First, define a Lua function:<br />
<br />
<texcode><br />
\startluacode<br />
-- remember, using the userdata namespace prevents conflicts<br />
userdata = userdata or {}<br />
<br />
function userdata.surroundwithdashes(str)<br />
context("--" .. str .. "--")<br />
end<br />
\stopluacode<br />
</texcode><br />
<br />
Then define the TeX command that expands to a <code>\ctxlua</code> call:<br />
<br />
<texcode><br />
\def\surroundwd#1%<br />
{\ctxlua{userdata.surroundwithdashes([==[#1]==])}}<br />
</texcode><br />
<br />
''NB'': quoting with <code>[==[#1]==]</code><br />
([http://www.lua.org/manual/5.2/manual.html#3.1 long strings])<br />
works just like <code>"#1"</code> in most cases, but in addition <br />
it is robust against <code>#1</code> containing the quotation mark<br />
<code>"</code> which would terminate the Lua string prematurely.<br />
Inside <code>\protect .. \unprotect</code> the macros <code>\!!bs</code><br />
and <code>\!!es</code> are at your disposition.<br />
They are equivalent to <code>[===[</code> and <code>]===]</code> and --<br />
being single tokens to TeX -- parsed faster.<br />
(See [http://repo.or.cz/w/context.git/blob/refs/heads/origin:/tex/context/base/luat-ini.mkiv#l174 <code>luat-ini.mkiv</code>].)<br />
<br />
== Making \startenv...\stopenv hook into Lua ==<br />
The first job is, as ever, to have the Lua function at the ready<br />
<texcode><br />
\startluacode<br />
userdata = userdata or {}<br />
<br />
function userdata.verynarrow(buffer)<br />
-- equivalent to \startnarrower[10em]<br />
context.startnarrower({"10em"})<br />
context(buffer)<br />
context.stopnarrower()<br />
end<br />
\stopluacode<br />
</texcode><br />
<br />
Next, we define the start command of our custom buffer:<br />
<br />
<texcode><br />
\def\startverynarrow%<br />
{\dostartbuffer<br />
[verynarrow] % buffer name<br />
[startverynarrow] % command where buffer starts<br />
[stopverynarrow]} % command where buffer ends<br />
% also: command invoked when buffer stops<br />
<br />
</texcode><br />
<br />
Lastly, we define the <code>\stopverynarrow</code> command such that it passes the recently-complated buffer to our <code>verynarrow</code> Lua function:<br />
<br />
<texcode><br />
\def\stopverynarrow<br />
{\ctxlua<br />
{userdata.verynarrow(buffers.getcontent('verynarrow'))}}<br />
</texcode><br />
<br />
And that's it! The rest of this article will consist of examples.<br />
<br />
= Examples =<br />
<br />
== Arithmetic without using an abacus ==<br />
<br />
''This example demonstrates writing simple commands that invoke \ctxlua.''<br />
<br />
Doing simple arithmetic in TeX can be extremely difficult. With Lua, simple arithmetic becomes trivial. For example, if you want a macro to find the cosine of an angle (in degrees), you can write<br />
<texcode><br />
\def\COSINE#1%<br />
{\ctxlua{context(math.cos(#1*2*math.pi/360))}}<br />
</texcode><br />
<br />
The built-in <code>math.cos</code> function assumes that the argument is specified in radians, so we convert from degrees to radians on the fly. If you want to type the value of $\pi$ in an article, you can simply say<br />
<texcode><br />
$\pi = \ctxlua{context(math.pi)}$ <br />
</texcode><br />
or, if you want less precision:<br />
<texcode> <br />
$\pi = \ctxlua{context("\%.6f", math.pi)}$<br />
</texcode><br />
Notice that the percent sign is escaped.<br />
<br />
<br />
== Loops without worrying about expansion ==<br />
<br />
''This example demonstrates using Lua to write a quasi-repetitive piece of ConTeXt code.''<br />
<br />
Loops in TeX are tricky, because macro assignments and macro expansion interact in strange ways. For example, suppose we want to typeset a table showing the sum of the roll of two dice and want the output to look like this:<br />
<context source="yes"><br />
\setupcolors[state=start]<br />
\setupTABLE[each][each][width=2em,height=2em,align={middle,middle}] <br />
\setupTABLE[r][1][background=color,backgroundcolor=gray] <br />
\setupTABLE[c][1][background=color,backgroundcolor=gray]<br />
<br />
\bTABLE <br />
\bTR \bTD $(+)$ \eTD \bTD 1 \eTD \bTD 2 \eTD <br />
\bTD 3 \eTD \bTD 4 \eTD \bTD 5 \eTD \bTD 6 \eTD \eTR <br />
\bTR \bTD 1 \eTD \bTD 2 \eTD \bTD 3 \eTD <br />
\bTD 4 \eTD \bTD 5 \eTD \bTD 6 \eTD \bTD 7 \eTD \eTR <br />
\bTR \bTD 2 \eTD \bTD 3 \eTD \bTD 4 \eTD <br />
\bTD 5 \eTD \bTD 6 \eTD \bTD 7 \eTD \bTD 8 \eTD \eTR <br />
\bTR \bTD 3 \eTD \bTD 4 \eTD \bTD 5 \eTD<br />
\bTD 6 \eTD \bTD 7 \eTD \bTD 8 \eTD \bTD 9 \eTD \eTR <br />
\bTR \bTD 4 \eTD \bTD 5 \eTD \bTD 6 \eTD<br />
\bTD 7 \eTD \bTD 8 \eTD \bTD 9 \eTD \bTD 10 \eTD \eTR <br />
\bTR \bTD 5 \eTD \bTD 6 \eTD \bTD 7 \eTD <br />
\bTD 8 \eTD \bTD 9 \eTD \bTD 10 \eTD \bTD 11 \eTD \eTR <br />
\bTR \bTD 6 \eTD \bTD 7 \eTD \bTD 8 \eTD <br />
\bTD 9 \eTD \bTD 10 \eTD \bTD 11 \eTD \bTD 12 \eTD \eTR <br />
\eTABLE<br />
</context><br />
<br />
This is easy in LuaTeX. Once a Lua instance starts, TeX does not see anything until the Lua instance exits. So, we can write the loop in Lua, and simply print the values that we would have typed to the TeX stream. When the control is passed to TeX, TeX sees the input as if we had typed it by hand. This is the Lua code for the above table:<br />
<br />
<texcode><br />
\setupcolors[state=start]<br />
\setupTABLE[each][each][width=2em,height=2em,align={middle,middle}] <br />
\setupTABLE[r][1][background=color,backgroundcolor=gray] <br />
\setupTABLE[c][1][background=color,backgroundcolor=gray]<br />
<br />
\startluacode<br />
context.bTABLE()<br />
context.bTR()<br />
context.bTD() context("$(+)$") context.eTD()<br />
for j=1,6 do<br />
context.bTD() context(j) context.eTD()<br />
end<br />
context.eTR()<br />
for i=1,6 do<br />
context.bTR()<br />
context.bTD() context(i) context.eTD()<br />
for j=1,6 do<br />
context.bTD() context(i+j) context.eTD()<br />
end<br />
context.eTR()<br />
end<br />
context.eTABLE()<br />
\stopluacode<br />
</texcode><br />
<br />
== Parsing input without exploding your head ==<br />
<br />
''This example demonstrates parsing simple ASCII notation with Lua's lpeg parser.''<br />
<br />
As an example, let's consider typesetting chemical molecules in TeX. Normally, molecules should be typeset in text mode rather than math mode. <br />
If we want <br />
:H<sub>3</sub>SO<sub>4</sub><sup>+</sup>,<br />
we must type <br />
:<code>H\low{3}SO\lohi{4}{\textplus}</code>,<br />
but we'd much rather type<br />
:<code>\molecule{H_3SO_4^+}</code>.<br />
<br />
So, we need a function that can take a string like that, parse it, and turn it into the appropriate TeX code. LuaTeX includes a general parser based on PEG (parsing expression grammar) called [http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html lpeg], and it makes writing little parsers positively joyful. (Once you've got the knack of it, at least.) For example, the above <code>\molecule</code> macro can be written as follows.<br />
<br />
<texcode><br />
\startluacode<br />
<br />
-- we will put our molecule function in the userdata namespace.<br />
userdata = userdata or { }<br />
<br />
-- The formatting functions into which the captured<br />
-- superscript/subscript blocks will be fed<br />
local formatters = { }<br />
<br />
function formatters.low(one)<br />
return string.format("\\low{%s}", one)<br />
end<br />
<br />
function formatters.high(one)<br />
return string.format("\\high{%s}", one)<br />
end<br />
<br />
function formatters.lowhigh(one, two)<br />
return string.format("\\lohi{%s}{%s}", one, two)<br />
end<br />
<br />
function formatters.highlow(one, two,three)<br />
return string.format("\\lohi{%s}{%s}", one,two)<br />
end<br />
<br />
-- These are the characters we may encounter<br />
-- The `/` means we want to expand + and - to \textplus c.q. \textminus;<br />
-- this substition is not instant, but will take place inside the first <br />
-- surrounding lpeg.Cs() call.<br />
local plus = lpeg.P("+") / "\\textplus "<br />
local minus = lpeg.P("-") / "\\textminus "<br />
local character = lpeg.R("az", "AZ", "09") -- R is for 'range'<br />
local subscript = lpeg.P("_") -- P is simply for 'pattern'<br />
local superscript = lpeg.P("^")<br />
local leftbrace = lpeg.P("{")<br />
local rightbrace = lpeg.P("}")<br />
<br />
-- a ^ or _ affects either a single character, or a brace-delimited<br />
-- block. Whichever it is, call it `content`.<br />
local single = character + plus + minus<br />
local multiple = leftbrace * single^1 * rightbrace<br />
local content = single + multiple<br />
<br />
-- These are our top-level elements: non-special text, of course, and<br />
-- blocks of superscript/subscript/both.<br />
-- lpeg.Cs(content) does two things:<br />
-- (1) not all matches go into the `/ function` construction; only<br />
-- *captures* go in. The C in Cs stands for Capture. This way, <br />
-- the superscript/subscript mark gets discarded.<br />
-- (2) it expands plus/minus before they go into the formatter. The<br />
-- s in Cs stands for 'substitute in the replacement values, if any'<br />
local text = single^1<br />
local low = subscript * lpeg.Cs(content) <br />
/ formatters.low<br />
local high = superscript * lpeg.Cs(content) <br />
/ formatters.high<br />
local lowhigh = subscript * lpeg.Cs(content) * <br />
superscript * lpeg.Cs(content) <br />
/ formatters.lowhigh<br />
local highlow = superscript * lpeg.Cs(content) * <br />
subscript * lpeg.Cs(content) <br />
/ formatters.highlow<br />
<br />
-- Finally, the root element: 'moleculepattern'<br />
local moleculepattern = lpeg.Cs((lowhigh + highlow + low + high + text)^0)<br />
<br />
function thirddata.molecule(string)<br />
-- * `:match` returns the matched string. Our pattern<br />
-- `moleculepattern` should match the entire input string. Any<br />
-- *performed* substitutions are retained. (`.Cs()` performs a<br />
-- previously defined substitution.)<br />
-- * `context()` inserts the resulting string into the stream, ready for<br />
-- TeX to evaluate.<br />
context(moleculepattern:match(string))<br />
end<br />
<br />
\stopluacode<br />
<br />
\def\molecule#1{\ctxlua{thirddata.molecule("#1")}}<br />
<br />
\starttext<br />
\molecule{Hg^+}, \molecule{SO_4^{2-}}<br />
\stoptext<br />
</texcode><br />
<br />
Quite terse and readable by parser standards, isn't it?<br />
<br />
== Manipulating verbatim text ==<br />
<br />
''This example demonstrates defining a custom \start...\stop buffer that gets processed through Lua in its entirety.''<br />
<br />
Suppose we want to write an environment <code>\startdedentedtyping</code> ... <code>\stopdedentedtyping</code> that removes the indentation of the first line from every line. Thus, the output of ...<br />
<br />
<texcode><br />
\startdedentedtyping<br />
#include &lt;stdio.h&gt;<br />
void main()<br />
{<br />
print("Hello world \n") ;<br />
}<br />
\stopdedentedtyping<br />
</texcode><br />
... should be the same as the output of ...<br />
<br />
<texcode><br />
\starttyping<br />
#include &lt;stdio.h&gt;<br />
void main()<br />
{<br />
print("Hello world \n") ;<br />
}<br />
\stoptyping<br />
</texcode><br />
... even though the leading whitespace is different.<br />
<br />
Defining an environment in TeX that removes the leading spaces but leaves<br />
other spaces untouched is complicated. On the other hand, once we capture the<br />
contents of the environment, removing the leading indent or ''dedenting'' the<br />
content in Lua is easy. Here is a Lua function that uses simple string<br />
substitutions.<br />
<br />
<texcode><br />
\startluacode<br />
-- Initialize a userdata name space to keep our own functions in.<br />
-- That way, we won't interfere with anything ConTeXt keeps in <br />
-- the global name space.<br />
userdata = userdata or {}<br />
<br />
function userdata.dedentedtyping(content)<br />
local lines = string.splitlines(content)<br />
local indent = string.match(lines[1], '^ +') or ''<br />
local pattern = '^' .. indent<br />
for i=1,#lines do<br />
lines[i] = string.gsub(lines[i],pattern,"")<br />
end<br />
<br />
content = table.concat(lines,'\n')<br />
<br />
tex.sprint("\\starttyping\n" .. content .. "\\stoptyping\n")<br />
<br />
-- The typing environment looks for an explicit \type{\stoptyping}. So,<br />
-- context.starttyping() context(content) context.stoptyping()<br />
-- does not work. But<br />
-- context.starttyping() context(content) tex.sprint("\\stoptyping")<br />
-- does.<br />
end<br />
\stopluacode<br />
</texcode><br />
<br />
Here is the code for defining the <code>\startdedentedtyping...\stopdedentedtyping</code> pair:<br />
<br />
<texcode><br />
% Create an environment that stores everything <br />
% between \startdedentedtyping and \stopdedentedtyping <br />
% in a buffer named 'dedentedtyping'.<br />
\def\startdedentedtyping<br />
{\dostartbuffer<br />
[dedentedtyping]<br />
[startdedentedtyping]<br />
[stopdedentedtyping]}<br />
<br />
% On closing the dedentedtyping environment, call the LuaTeX<br />
% function dedentedtyping(), and pass it the contents of <br />
% the buffer called 'dedentedtyping'<br />
\def\stopdedentedtyping<br />
{\ctxlua<br />
{userdata.dedentedtyping(buffers.getcontent('dedentedtyping'))}}<br />
</texcode><br />
<br />
That's all. Finally, we will go into a little more detail on how TeX and Lua communicate with each other.<br />
<br />
== Other examples ==<br />
<br />
* [[Calculations_in_Lua|Calculations in Lua]] (warning date 2012)<br />
* [[LPeg|Writing a parser with LPeg]] (Lua Parsing Expression Grammars)<br />
* [[Random|Random numbers]] in ConTeXt and MetaPost<br />
* [[SQL|An example with SQL database]] <br />
<br />
= In detail: the interaction between TeX and Lua =<br />
<br />
To a first approximation, the interaction between TeX and Lua is straightforward. When TeX (i.e., the LuaTeX engine) starts, it loads the input file in memory and processes it token by token. When TeX encounters <code>\directlua</code>, it stops reading the file in memory, <em>fully expands the argument of <code>\directlua</code></em>, and passes the control to a Lua instance. The Lua instance, which runs with a few preloaded libraries, processes the expanded arguments of <code>\directlua</code>. This Lua instance has a special output stream which can be accessed using <code>tex.print(...)</code>. The function <code>tex.print(...)</code> is just like the Lua function <code>print(...)</code> except that <code>tex.print(...)</code> prints to a <em>TeX stream</em> rather than to the standard output. When the Lua instance finishes processing its input, it passes the contents of the <em>TeX stream</em> back to TeX.<ref>The output of <code>tex.print(...)</code> is buffered and not passed to TeX until the Lua instance has stopped.</ref> TeX then inserts the contents of the <em>TeX stream</em> at the current location of the file that it was reading; expands the contents of the <em>TeX stream</em>; and continues. If TeX encounters another <code>\directlua</code>, the above process is repeated. <br />
<br />
As an exercise, imagine what happens when the following input is processed by LuaTeX. The answer is in the footnotes. <ref>In this example, two different kinds of quotations are used to avoid escaping quotes. Escaping quotes inside <code>\directlua</code> is tricky. The above was a contrived example; if you ever need to escape quotes, you can use the <code>\startluacode ... \stopluacode</code> syntax.</ref><br />
<br />
<texcode><br />
\directlua%<br />
{tex.print("Depth 1 <br />
\\directlua{tex.print('Depth 2')}")}<br />
</texcode><br />
<br />
For more on this, see the [http://wiki.luatex.org/index.php/Writing_Lua_in_TeX] article on the [http://wiki.luatex.org/index.php/Main_Page LuaTeX wiki].<br />
<br />
= Notes =<br />
<references /><br />
<br />
{{note | This article is originally based on [https://www.tug.org/members/TUGboat/tb30-2/tb95mahajan-luatex.pdf this TugBoat article ]. Feel free to modify it.}}<br />
<br />
[[Category:Programming and Databases]]</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Programming_in_LuaTeX&diff=29421Programming in LuaTeX2020-06-24T15:00:02Z<p>Otared: /* Calling TeX from Lua */</p>
<hr />
<div>= Calling Lua from TeX =<br />
<br />
The interweaving of ConTeXt and Lua consists of two elements: first you tell TeX that you're starting some Lua code; then, once inside Lua, you need to use the appropriate functions to put things into the TeX stream.<br />
<br />
There are two main ways to execute Lua code in a ConTeXt document: The command <code>\ctxlua</code>, and the environment <code>\startluacode...\stopluacode</code>. Both are wrappers around the LuaTeX primitive <code>\directlua</code>, which you should never need to use. In general, you will define a function inside a <code>\startluacode</code> block, and then define a TeX command that calls the function using <code>\ctxlua</code>, especially because <code>\ctxlua</code> has a few idiosyncracies.<br />
<br />
The main thing about Lua code in a TeX document is this: the code is expanded by TeX ''before'' Lua gets to it. '''This means that all the Lua code, even the comments, must be valid TeX!''' A string like "\undefined" will cause an immediate failure.<br />
<br />
== Calling a bit of Lua inline: \ctxlua ==<br />
<br />
The command <code>\ctxlua</code> is for short inline snippets of Lua, such<br />
as<br />
<br />
<texcode><br />
$2 + 5 \neq \ctxlua{context(3+5)}$, but is equal to \ctxlua{context(2+5)}.<br />
This is \ctxlua{context(string.upper("absolutely"))} true.<br />
</texcode><br />
<br />
<code>\ctxlua</code> operates under the normal TeX catcodes (category codes). This means the following two things for the Lua code inside:<br />
* all newlines get treated as spaces<br />
* special TeX characters like &, #, $, {, }, etc., need to be escaped. <br />
<br />
In addition, the warning above still holds. All the Lua code, even the comments, must be valid TeX.<br />
<br />
Some code to illustrate the newline problem:<br />
<texcode><br />
\ctxlua<br />
{-- A Lua comment<br />
tex.print("This is not printed")}<br />
\ctxlua<br />
{% A Tex comment<br />
tex.print("This is printed")}<br />
</texcode><br />
<br />
The problem with special TeX characters. (<code>#t</code> is Lua for 'the length of array <code>t</code>.)<br />
<texcode><br />
% This doesn't work:<br />
%\ctxlua<br />
% {local t = {1,2,3,4}<br />
% tex.print("length " .. #t)}<br />
\ctxlua<br />
{local t = {1,2,3,4}<br />
tex.print("length " .. \string#t)}<br />
</texcode><br />
<br />
== A larger Lua block: \startluacode...\stopluacode ==<br />
<br />
Inside the <code>\startluacode...\stopluacode</code> environment, newlines and special characters behave normally. This solves the catcode problem that <code>\ctxlua</code> suffers from. Apart from these special characters, the main warning remains in force: all the Lua code, even the comments, must be valid TeX.<br />
<br />
<texcode><br />
\startluacode<br />
-- The unknown command \undefined will cause this entire block to fail.<br />
<br />
-- Print a countdown '10, 8, ..., 0!'<br />
-- `..` is Lua for string concatenation<br />
for i = 10, 2, -2 do<br />
context(i .. ", ")<br />
end<br />
context("0!")<br />
<br />
-- \\par is equivalent to a blank line in the input<br />
-- (Notice the escaped backslash: TeX won't mind the above comment.)<br />
context.par()<br />
<br />
-- Look! we can use # and $ with impunity!<br />
context("Unless we print them, then we must \\#\\$\\& print the escape characters, too.")<br />
\stopluacode<br />
</texcode><br />
<br />
== Putting Lua code in an external file ==<br />
<br />
You can put your lua code in an external file (with the <code>.lua</code> extension) and include it with the <code>require</code> command:<br />
<br />
<texcode><br />
\startluacode<br />
-- include the file my-lua-lib.lua<br />
require("my-lua-lib")<br />
\stopluacode<br />
</texcode><br />
<br />
== Namespaces ==<br />
<br />
It is a good habit to put your custom-defined functions in their own namespace. The traditional namespace for this is <code>userdata</code>:<br />
<texcode><br />
\startluacode<br />
-- if userdata doesn't exist yet, create it<br />
userdata = userdata or {}<br />
-- define a shorter synonym<br />
u = userdata<br />
<br />
-- create my custom function inside the userdata namespace<br />
function u.myfunction()<br />
-- do stuff<br />
end <br />
\stopluacode<br />
</texcode><br />
<br />
The full list of canonical namespaces, taken from [http://minimals.contextgarden.net/current/context/alpha/tex/context/base/luat-ini.lua luat-ini.lua]:<br />
<br />
<code><pre><br />
userdata = userdata or { } -- for users (e.g. functions etc)<br />
thirddata = thirddata or { } -- only for third party modules<br />
moduledata = moduledata or { } -- only for development team<br />
documentdata = documentdata or { } -- for users (e.g. raw data)<br />
parametersets = parametersets or { } -- experimental for team<br />
</pre></code><br />
<br />
If your module, environment, or document is going to be used by other people, you should create your own subnamespaces within these tables.<br />
<br />
<code><pre><br />
moduledata['mymodule'] = { }<br />
mm = moduledata.mymodule<br />
function mm.mainfunction()<br />
-- do stuff<br />
end<br />
</pre></code><br />
<br />
= Calling Lua from TeX =<br />
<br />
One can execute a Lua code from within TeX and get back the result in TeX by using {{code|\cldcontext}}. Thus, if {{code|myfunction}} is a function of a variable {{code|x}} defined in Lua, {{code|\cldcontext{myfunction(5)}}} returns the value {{code|myfunction(5)}} in TeX. This is equivalent to {{code|\ctxlua{context(myfunction(5))}}}.<br />
<br />
<br />
Being a topic on itself, pages are dedicated:<br />
* '''[[CLD|ConTeXt Lua Documents]]''', or CLD, are way to access TeX from inside Lua scripts. A page give clues about [[CLD_passing_variables|passing variables]] within CLD (2018).<br />
* [[Lua|Wiki page dedicated to Lua]]<br />
** [[Extensions to the Lua I/O library]]<br />
** [[String manipulation]]<br />
** [[Table manipulation]]<br />
<br />
= Putting stuff in your TeX document from Lua =<br />
<br />
== Simple printing: context(), tex.print(), and tex.sprint() ==<br />
Use <code>context(...)</code> for most things. It is equivalent to <code>tex.print(string.format(...))</code>, so<br />
<br />
<texcode><br />
\startluacode<br />
name = "Jane"<br />
date = "today"<br />
context("Hello %s, how are you %s?", name, date)<br />
-- Becomes 'Hello Jane, how are you today?'<br />
\stopluacode<br />
</texcode><br />
<br />
More primitively, you have <code>tex.print()</code> and <code>tex.sprint()</code>. Either one can take as an argument either a number of strings, or an array of strings, and will then insert the strings into the TeX stream. The only difference is that <code>tex.print()</code> treats each string as a separate input line, while <code>tex.sprint()</code> doesn't. So the following lines<br />
<br />
<texcode><br />
\ctxlua{tex.print("a", "b")}<br />
\ctxlua{tex.print({"a", "b"})}<br />
</texcode><br />
<br />
are both interpreted by TeX as<br />
<br />
<texcode><br />
a<br />
b<br />
</texcode><br />
<br />
but when we use <code>tex.sprint</code> instead, either of the following<br />
<br />
<texcode><br />
\ctxlua{tex.sprint("a", "b")}<br />
\ctxlua{tex.sprint({"a", "b"})}<br />
</texcode><br />
<br />
will be read by TeX as<br />
<br />
<texcode><br />
ab<br />
</texcode> <br />
<br />
without any space in between.<br />
<br />
== Context commands ==<br />
<br />
Most commands that you would type with a backslash in plain ConTeXt, you can access from Lua with <code>context.<em>command</em></code>. Unadorned strings end up in TeX as arguments in curly braces; Lua tables end up in TeX as paramater blocks in square brackets. The following two pieces of code are equivalent:<br />
<br />
<texcode><br />
\startluacode<br />
context.chapter({first}, "Some title")<br />
context.startcolumns({n = 3, rule = "on"})<br />
context("Hello one")<br />
context.column()<br />
context("Hello two")<br />
context.column()<br />
context("Hello three")<br />
context.stopcolumns()<br />
\stopluacode<br />
</texcode><br />
<br />
<texcode><br />
\chapter[first]{Some title}<br />
\startcolumns[n=3, rule=on]<br />
Hello one<br />
\column<br />
Hello two<br />
\column<br />
Hello three<br />
\stopcolumns<br />
</texcode><br />
<br />
For a fuller account of the context.commands, see the [http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf ConTeXt Lua document] manual. It is old, but most of it still applies.<br />
<br />
One final note: arguments can also be specified in the form of nested functions. Because LuaTeX evaluates the deepest-nested argument first, this may cause the <code>context()</code> calls to be evaluated in the wrong order. For more on this, see the article on [[CLD|ConTeXt Lua documents]], and also, again, the [http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf CLD manual].<br />
<br />
= Passing arguments and buffers: ConTeXt commands that hook into Lua =<br />
<br />
== Making \command{arg1}{arg2} hook into Lua ==<br />
First, define a Lua function:<br />
<br />
<texcode><br />
\startluacode<br />
-- remember, using the userdata namespace prevents conflicts<br />
userdata = userdata or {}<br />
<br />
function userdata.surroundwithdashes(str)<br />
context("--" .. str .. "--")<br />
end<br />
\stopluacode<br />
</texcode><br />
<br />
Then define the TeX command that expands to a <code>\ctxlua</code> call:<br />
<br />
<texcode><br />
\def\surroundwd#1%<br />
{\ctxlua{userdata.surroundwithdashes([==[#1]==])}}<br />
</texcode><br />
<br />
''NB'': quoting with <code>[==[#1]==]</code><br />
([http://www.lua.org/manual/5.2/manual.html#3.1 long strings])<br />
works just like <code>"#1"</code> in most cases, but in addition <br />
it is robust against <code>#1</code> containing the quotation mark<br />
<code>"</code> which would terminate the Lua string prematurely.<br />
Inside <code>\protect .. \unprotect</code> the macros <code>\!!bs</code><br />
and <code>\!!es</code> are at your disposition.<br />
They are equivalent to <code>[===[</code> and <code>]===]</code> and --<br />
being single tokens to TeX -- parsed faster.<br />
(See [http://repo.or.cz/w/context.git/blob/refs/heads/origin:/tex/context/base/luat-ini.mkiv#l174 <code>luat-ini.mkiv</code>].)<br />
<br />
== Making \startenv...\stopenv hook into Lua ==<br />
The first job is, as ever, to have the Lua function at the ready<br />
<texcode><br />
\startluacode<br />
userdata = userdata or {}<br />
<br />
function userdata.verynarrow(buffer)<br />
-- equivalent to \startnarrower[10em]<br />
context.startnarrower({"10em"})<br />
context(buffer)<br />
context.stopnarrower()<br />
end<br />
\stopluacode<br />
</texcode><br />
<br />
Next, we define the start command of our custom buffer:<br />
<br />
<texcode><br />
\def\startverynarrow%<br />
{\dostartbuffer<br />
[verynarrow] % buffer name<br />
[startverynarrow] % command where buffer starts<br />
[stopverynarrow]} % command where buffer ends<br />
% also: command invoked when buffer stops<br />
<br />
</texcode><br />
<br />
Lastly, we define the <code>\stopverynarrow</code> command such that it passes the recently-complated buffer to our <code>verynarrow</code> Lua function:<br />
<br />
<texcode><br />
\def\stopverynarrow<br />
{\ctxlua<br />
{userdata.verynarrow(buffers.getcontent('verynarrow'))}}<br />
</texcode><br />
<br />
And that's it! The rest of this article will consist of examples.<br />
<br />
= Examples =<br />
<br />
== Arithmetic without using an abacus ==<br />
<br />
''This example demonstrates writing simple commands that invoke \ctxlua.''<br />
<br />
Doing simple arithmetic in TeX can be extremely difficult. With Lua, simple arithmetic becomes trivial. For example, if you want a macro to find the cosine of an angle (in degrees), you can write<br />
<texcode><br />
\def\COSINE#1%<br />
{\ctxlua{context(math.cos(#1*2*math.pi/360))}}<br />
</texcode><br />
<br />
The built-in <code>math.cos</code> function assumes that the argument is specified in radians, so we convert from degrees to radians on the fly. If you want to type the value of $\pi$ in an article, you can simply say<br />
<texcode><br />
$\pi = \ctxlua{context(math.pi)}$ <br />
</texcode><br />
or, if you want less precision:<br />
<texcode> <br />
$\pi = \ctxlua{context("\%.6f", math.pi)}$<br />
</texcode><br />
Notice that the percent sign is escaped.<br />
<br />
<br />
== Loops without worrying about expansion ==<br />
<br />
''This example demonstrates using Lua to write a quasi-repetitive piece of ConTeXt code.''<br />
<br />
Loops in TeX are tricky, because macro assignments and macro expansion interact in strange ways. For example, suppose we want to typeset a table showing the sum of the roll of two dice and want the output to look like this:<br />
<context source="yes"><br />
\setupcolors[state=start]<br />
\setupTABLE[each][each][width=2em,height=2em,align={middle,middle}] <br />
\setupTABLE[r][1][background=color,backgroundcolor=gray] <br />
\setupTABLE[c][1][background=color,backgroundcolor=gray]<br />
<br />
\bTABLE <br />
\bTR \bTD $(+)$ \eTD \bTD 1 \eTD \bTD 2 \eTD <br />
\bTD 3 \eTD \bTD 4 \eTD \bTD 5 \eTD \bTD 6 \eTD \eTR <br />
\bTR \bTD 1 \eTD \bTD 2 \eTD \bTD 3 \eTD <br />
\bTD 4 \eTD \bTD 5 \eTD \bTD 6 \eTD \bTD 7 \eTD \eTR <br />
\bTR \bTD 2 \eTD \bTD 3 \eTD \bTD 4 \eTD <br />
\bTD 5 \eTD \bTD 6 \eTD \bTD 7 \eTD \bTD 8 \eTD \eTR <br />
\bTR \bTD 3 \eTD \bTD 4 \eTD \bTD 5 \eTD<br />
\bTD 6 \eTD \bTD 7 \eTD \bTD 8 \eTD \bTD 9 \eTD \eTR <br />
\bTR \bTD 4 \eTD \bTD 5 \eTD \bTD 6 \eTD<br />
\bTD 7 \eTD \bTD 8 \eTD \bTD 9 \eTD \bTD 10 \eTD \eTR <br />
\bTR \bTD 5 \eTD \bTD 6 \eTD \bTD 7 \eTD <br />
\bTD 8 \eTD \bTD 9 \eTD \bTD 10 \eTD \bTD 11 \eTD \eTR <br />
\bTR \bTD 6 \eTD \bTD 7 \eTD \bTD 8 \eTD <br />
\bTD 9 \eTD \bTD 10 \eTD \bTD 11 \eTD \bTD 12 \eTD \eTR <br />
\eTABLE<br />
</context><br />
<br />
This is easy in LuaTeX. Once a Lua instance starts, TeX does not see anything until the Lua instance exits. So, we can write the loop in Lua, and simply print the values that we would have typed to the TeX stream. When the control is passed to TeX, TeX sees the input as if we had typed it by hand. This is the Lua code for the above table:<br />
<br />
<texcode><br />
\setupcolors[state=start]<br />
\setupTABLE[each][each][width=2em,height=2em,align={middle,middle}] <br />
\setupTABLE[r][1][background=color,backgroundcolor=gray] <br />
\setupTABLE[c][1][background=color,backgroundcolor=gray]<br />
<br />
\startluacode<br />
context.bTABLE()<br />
context.bTR()<br />
context.bTD() context("$(+)$") context.eTD()<br />
for j=1,6 do<br />
context.bTD() context(j) context.eTD()<br />
end<br />
context.eTR()<br />
for i=1,6 do<br />
context.bTR()<br />
context.bTD() context(i) context.eTD()<br />
for j=1,6 do<br />
context.bTD() context(i+j) context.eTD()<br />
end<br />
context.eTR()<br />
end<br />
context.eTABLE()<br />
\stopluacode<br />
</texcode><br />
<br />
== Parsing input without exploding your head ==<br />
<br />
''This example demonstrates parsing simple ASCII notation with Lua's lpeg parser.''<br />
<br />
As an example, let's consider typesetting chemical molecules in TeX. Normally, molecules should be typeset in text mode rather than math mode. <br />
If we want <br />
:H<sub>4</sub>SO<sub>4</sub><sup>+</sup>,<br />
we must type <br />
:<code>H\low{3}SO\lohi{4}{\textplus}</code>,<br />
but we'd much rather type<br />
:<code>\molecule{H_3SO_4^+}</code>.<br />
<br />
So, we need a function that can take a string like that, parse it, and turn it into the appropriate TeX code. LuaTeX includes a general parser based on PEG (parsing expression grammar) called [http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html lpeg], and it makes writing little parsers positively joyful. (Once you've got the knack of it, at least.) For example, the above <code>\molecule</code> macro can be written as follows.<br />
<br />
<texcode><br />
\startluacode<br />
<br />
-- we will put our molecule function in the userdata namespace.<br />
userdata = userdata or { }<br />
<br />
-- The formatting functions into which the captured<br />
-- superscript/subscript blocks will be fed<br />
local formatters = { }<br />
<br />
function formatters.low(one)<br />
return string.format("\\low{%s}", one)<br />
end<br />
<br />
function formatters.high(one)<br />
return string.format("\\high{%s}", one)<br />
end<br />
<br />
function formatters.lowhigh(one, two)<br />
return string.format("\\lohi{%s}{%s}", one, two)<br />
end<br />
<br />
function formatters.highlow(one, two,three)<br />
return string.format("\\lohi{%s}{%s}", one,two)<br />
end<br />
<br />
-- These are the characters we may encounter<br />
-- The `/` means we want to expand + and - to \textplus c.q. \textminus;<br />
-- this substition is not instant, but will take place inside the first <br />
-- surrounding lpeg.Cs() call.<br />
local plus = lpeg.P("+") / "\\textplus "<br />
local minus = lpeg.P("-") / "\\textminus "<br />
local character = lpeg.R("az", "AZ", "09") -- R is for 'range'<br />
local subscript = lpeg.P("_") -- P is simply for 'pattern'<br />
local superscript = lpeg.P("^")<br />
local leftbrace = lpeg.P("{")<br />
local rightbrace = lpeg.P("}")<br />
<br />
-- a ^ or _ affects either a single character, or a brace-delimited<br />
-- block. Whichever it is, call it `content`.<br />
local single = character + plus + minus<br />
local multiple = leftbrace * single^1 * rightbrace<br />
local content = single + multiple<br />
<br />
-- These are our top-level elements: non-special text, of course, and<br />
-- blocks of superscript/subscript/both.<br />
-- lpeg.Cs(content) does two things:<br />
-- (1) not all matches go into the `/ function` construction; only<br />
-- *captures* go in. The C in Cs stands for Capture. This way, <br />
-- the superscript/subscript mark gets discarded.<br />
-- (2) it expands plus/minus before they go into the formatter. The<br />
-- s in Cs stands for 'substitute in the replacement values, if any'<br />
local text = single^1<br />
local low = subscript * lpeg.Cs(content) <br />
/ formatters.low<br />
local high = superscript * lpeg.Cs(content) <br />
/ formatters.high<br />
local lowhigh = subscript * lpeg.Cs(content) * <br />
superscript * lpeg.Cs(content) <br />
/ formatters.lowhigh<br />
local highlow = superscript * lpeg.Cs(content) * <br />
subscript * lpeg.Cs(content) <br />
/ formatters.highlow<br />
<br />
-- Finally, the root element: 'moleculepattern'<br />
local moleculepattern = lpeg.Cs((lowhigh + highlow + low + high + text)^0)<br />
<br />
function thirddata.molecule(string)<br />
-- * `:match` returns the matched string. Our pattern<br />
-- `moleculepattern` should match the entire input string. Any<br />
-- *performed* substitutions are retained. (`.Cs()` performs a<br />
-- previously defined substitution.)<br />
-- * `context()` inserts the resulting string into the stream, ready for<br />
-- TeX to evaluate.<br />
context(moleculepattern:match(string))<br />
end<br />
<br />
\stopluacode<br />
<br />
\def\molecule#1{\ctxlua{thirddata.molecule("#1")}}<br />
<br />
\starttext<br />
\molecule{Hg^+}, \molecule{SO_4^{2-}}<br />
\stoptext<br />
</texcode><br />
<br />
Quite terse and readable by parser standards, isn't it?<br />
<br />
== Manipulating verbatim text ==<br />
<br />
''This example demonstrates defining a custom \start...\stop buffer that gets processed through Lua in its entirety.''<br />
<br />
Suppose we want to write an environment <code>\startdedentedtyping</code> ... <code>\stopdedentedtyping</code> that removes the indentation of the first line from every line. Thus, the output of ...<br />
<br />
<texcode><br />
\startdedentedtyping<br />
#include &lt;stdio.h&gt;<br />
void main()<br />
{<br />
print("Hello world \n") ;<br />
}<br />
\stopdedentedtyping<br />
</texcode><br />
... should be the same as the output of ...<br />
<br />
<texcode><br />
\starttyping<br />
#include &lt;stdio.h&gt;<br />
void main()<br />
{<br />
print("Hello world \n") ;<br />
}<br />
\stoptyping<br />
</texcode><br />
... even though the leading whitespace is different.<br />
<br />
Defining an environment in TeX that removes the leading spaces but leaves<br />
other spaces untouched is complicated. On the other hand, once we capture the<br />
contents of the environment, removing the leading indent or ''dedenting'' the<br />
content in Lua is easy. Here is a Lua function that uses simple string<br />
substitutions.<br />
<br />
<texcode><br />
\startluacode<br />
-- Initialize a userdata name space to keep our own functions in.<br />
-- That way, we won't interfere with anything ConTeXt keeps in <br />
-- the global name space.<br />
userdata = userdata or {}<br />
<br />
function userdata.dedentedtyping(content)<br />
local lines = string.splitlines(content)<br />
local indent = string.match(lines[1], '^ +') or ''<br />
local pattern = '^' .. indent<br />
for i=1,#lines do<br />
lines[i] = string.gsub(lines[i],pattern,"")<br />
end<br />
<br />
content = table.concat(lines,'\n')<br />
<br />
tex.sprint("\\starttyping\n" .. content .. "\\stoptyping\n")<br />
<br />
-- The typing environment looks for an explicit \type{\stoptyping}. So,<br />
-- context.starttyping() context(content) context.stoptyping()<br />
-- does not work. But<br />
-- context.starttyping() context(content) tex.sprint("\\stoptyping")<br />
-- does.<br />
end<br />
\stopluacode<br />
</texcode><br />
<br />
Here is the code for defining the <code>\startdedentedtyping...\stopdedentedtyping</code> pair:<br />
<br />
<texcode><br />
% Create an environment that stores everything <br />
% between \startdedentedtyping and \stopdedentedtyping <br />
% in a buffer named 'dedentedtyping'.<br />
\def\startdedentedtyping<br />
{\dostartbuffer<br />
[dedentedtyping]<br />
[startdedentedtyping]<br />
[stopdedentedtyping]}<br />
<br />
% On closing the dedentedtyping environment, call the LuaTeX<br />
% function dedentedtyping(), and pass it the contents of <br />
% the buffer called 'dedentedtyping'<br />
\def\stopdedentedtyping<br />
{\ctxlua<br />
{userdata.dedentedtyping(buffers.getcontent('dedentedtyping'))}}<br />
</texcode><br />
<br />
That's all. Finally, we will go into a little more detail on how TeX and Lua communicate with each other.<br />
<br />
== Other examples ==<br />
<br />
* [[Calculations_in_Lua|Calculations in Lua]] (warning date 2012)<br />
* [[LPeg|Writing a parser with LPeg]] (Lua Parsing Expression Grammars)<br />
* [[Random|Random numbers]] in ConTeXt and MetaPost<br />
* [[SQL|An example with SQL database]] <br />
<br />
= In detail: the interaction between TeX and Lua =<br />
<br />
To a first approximation, the interaction between TeX and Lua is straightforward. When TeX (i.e., the LuaTeX engine) starts, it loads the input file in memory and processes it token by token. When TeX encounters <code>\directlua</code>, it stops reading the file in memory, <em>fully expands the argument of <code>\directlua</code></em>, and passes the control to a Lua instance. The Lua instance, which runs with a few preloaded libraries, processes the expanded arguments of <code>\directlua</code>. This Lua instance has a special output stream which can be accessed using <code>tex.print(...)</code>. The function <code>tex.print(...)</code> is just like the Lua function <code>print(...)</code> except that <code>tex.print(...)</code> prints to a <em>TeX stream</em> rather than to the standard output. When the Lua instance finishes processing its input, it passes the contents of the <em>TeX stream</em> back to TeX.<ref>The output of <code>tex.print(...)</code> is buffered and not passed to TeX until the Lua instance has stopped.</ref> TeX then inserts the contents of the <em>TeX stream</em> at the current location of the file that it was reading; expands the contents of the <em>TeX stream</em>; and continues. If TeX encounters another <code>\directlua</code>, the above process is repeated. <br />
<br />
As an exercise, imagine what happens when the following input is processed by LuaTeX. The answer is in the footnotes. <ref>In this example, two different kinds of quotations are used to avoid escaping quotes. Escaping quotes inside <code>\directlua</code> is tricky. The above was a contrived example; if you ever need to escape quotes, you can use the <code>\startluacode ... \stopluacode</code> syntax.</ref><br />
<br />
<texcode><br />
\directlua%<br />
{tex.print("Depth 1 <br />
\\directlua{tex.print('Depth 2')}")}<br />
</texcode><br />
<br />
For more on this, see the [http://wiki.luatex.org/index.php/Writing_Lua_in_TeX] article on the [http://wiki.luatex.org/index.php/Main_Page LuaTeX wiki].<br />
<br />
= Notes =<br />
<references /><br />
<br />
{{note | This article is originally based on [https://www.tug.org/members/TUGboat/tb30-2/tb95mahajan-luatex.pdf this TugBoat article ]. Feel free to modify it.}}<br />
<br />
[[Category:Programming and Databases]]</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Programming_in_LuaTeX&diff=29420Programming in LuaTeX2020-06-24T14:50:33Z<p>Otared: /* Putting Lua code in an external file */</p>
<hr />
<div>= Calling Lua from TeX =<br />
<br />
The interweaving of ConTeXt and Lua consists of two elements: first you tell TeX that you're starting some Lua code; then, once inside Lua, you need to use the appropriate functions to put things into the TeX stream.<br />
<br />
There are two main ways to execute Lua code in a ConTeXt document: The command <code>\ctxlua</code>, and the environment <code>\startluacode...\stopluacode</code>. Both are wrappers around the LuaTeX primitive <code>\directlua</code>, which you should never need to use. In general, you will define a function inside a <code>\startluacode</code> block, and then define a TeX command that calls the function using <code>\ctxlua</code>, especially because <code>\ctxlua</code> has a few idiosyncracies.<br />
<br />
The main thing about Lua code in a TeX document is this: the code is expanded by TeX ''before'' Lua gets to it. '''This means that all the Lua code, even the comments, must be valid TeX!''' A string like "\undefined" will cause an immediate failure.<br />
<br />
== Calling a bit of Lua inline: \ctxlua ==<br />
<br />
The command <code>\ctxlua</code> is for short inline snippets of Lua, such<br />
as<br />
<br />
<texcode><br />
$2 + 5 \neq \ctxlua{context(3+5)}$, but is equal to \ctxlua{context(2+5)}.<br />
This is \ctxlua{context(string.upper("absolutely"))} true.<br />
</texcode><br />
<br />
<code>\ctxlua</code> operates under the normal TeX catcodes (category codes). This means the following two things for the Lua code inside:<br />
* all newlines get treated as spaces<br />
* special TeX characters like &, #, $, {, }, etc., need to be escaped. <br />
<br />
In addition, the warning above still holds. All the Lua code, even the comments, must be valid TeX.<br />
<br />
Some code to illustrate the newline problem:<br />
<texcode><br />
\ctxlua<br />
{-- A Lua comment<br />
tex.print("This is not printed")}<br />
\ctxlua<br />
{% A Tex comment<br />
tex.print("This is printed")}<br />
</texcode><br />
<br />
The problem with special TeX characters. (<code>#t</code> is Lua for 'the length of array <code>t</code>.)<br />
<texcode><br />
% This doesn't work:<br />
%\ctxlua<br />
% {local t = {1,2,3,4}<br />
% tex.print("length " .. #t)}<br />
\ctxlua<br />
{local t = {1,2,3,4}<br />
tex.print("length " .. \string#t)}<br />
</texcode><br />
<br />
== A larger Lua block: \startluacode...\stopluacode ==<br />
<br />
Inside the <code>\startluacode...\stopluacode</code> environment, newlines and special characters behave normally. This solves the catcode problem that <code>\ctxlua</code> suffers from. Apart from these special characters, the main warning remains in force: all the Lua code, even the comments, must be valid TeX.<br />
<br />
<texcode><br />
\startluacode<br />
-- The unknown command \undefined will cause this entire block to fail.<br />
<br />
-- Print a countdown '10, 8, ..., 0!'<br />
-- `..` is Lua for string concatenation<br />
for i = 10, 2, -2 do<br />
context(i .. ", ")<br />
end<br />
context("0!")<br />
<br />
-- \\par is equivalent to a blank line in the input<br />
-- (Notice the escaped backslash: TeX won't mind the above comment.)<br />
context.par()<br />
<br />
-- Look! we can use # and $ with impunity!<br />
context("Unless we print them, then we must \\#\\$\\& print the escape characters, too.")<br />
\stopluacode<br />
</texcode><br />
<br />
== Putting Lua code in an external file ==<br />
<br />
You can put your lua code in an external file (with the <code>.lua</code> extension) and include it with the <code>require</code> command:<br />
<br />
<texcode><br />
\startluacode<br />
-- include the file my-lua-lib.lua<br />
require("my-lua-lib")<br />
\stopluacode<br />
</texcode><br />
<br />
== Namespaces ==<br />
<br />
It is a good habit to put your custom-defined functions in their own namespace. The traditional namespace for this is <code>userdata</code>:<br />
<texcode><br />
\startluacode<br />
-- if userdata doesn't exist yet, create it<br />
userdata = userdata or {}<br />
-- define a shorter synonym<br />
u = userdata<br />
<br />
-- create my custom function inside the userdata namespace<br />
function u.myfunction()<br />
-- do stuff<br />
end <br />
\stopluacode<br />
</texcode><br />
<br />
The full list of canonical namespaces, taken from [http://minimals.contextgarden.net/current/context/alpha/tex/context/base/luat-ini.lua luat-ini.lua]:<br />
<br />
<code><pre><br />
userdata = userdata or { } -- for users (e.g. functions etc)<br />
thirddata = thirddata or { } -- only for third party modules<br />
moduledata = moduledata or { } -- only for development team<br />
documentdata = documentdata or { } -- for users (e.g. raw data)<br />
parametersets = parametersets or { } -- experimental for team<br />
</pre></code><br />
<br />
If your module, environment, or document is going to be used by other people, you should create your own subnamespaces within these tables.<br />
<br />
<code><pre><br />
moduledata['mymodule'] = { }<br />
mm = moduledata.mymodule<br />
function mm.mainfunction()<br />
-- do stuff<br />
end<br />
</pre></code><br />
<br />
= Calling TeX from Lua =<br />
<br />
The reverse is possible. Being a topic on itself, pages are dedicated:<br />
* '''[[CLD|ConTeXt Lua Documents]]''', or CLD, are way to access TeX from inside Lua scripts. A page give clues about [[CLD_passing_variables|passing variables]] within CLD (2018).<br />
* [[Lua|Wiki page dedicated to Lua]]<br />
** [[Extensions to the Lua I/O library]]<br />
** [[String manipulation]]<br />
** [[Table manipulation]]<br />
<br />
= Putting stuff in your TeX document from Lua =<br />
<br />
== Simple printing: context(), tex.print(), and tex.sprint() ==<br />
Use <code>context(...)</code> for most things. It is equivalent to <code>tex.print(string.format(...))</code>, so<br />
<br />
<texcode><br />
\startluacode<br />
name = "Jane"<br />
date = "today"<br />
context("Hello %s, how are you %s?", name, date)<br />
-- Becomes 'Hello Jane, how are you today?'<br />
\stopluacode<br />
</texcode><br />
<br />
More primitively, you have <code>tex.print()</code> and <code>tex.sprint()</code>. Either one can take as an argument either a number of strings, or an array of strings, and will then insert the strings into the TeX stream. The only difference is that <code>tex.print()</code> treats each string as a separate input line, while <code>tex.sprint()</code> doesn't. So the following lines<br />
<br />
<texcode><br />
\ctxlua{tex.print("a", "b")}<br />
\ctxlua{tex.print({"a", "b"})}<br />
</texcode><br />
<br />
are both interpreted by TeX as<br />
<br />
<texcode><br />
a<br />
b<br />
</texcode><br />
<br />
but when we use <code>tex.sprint</code> instead, either of the following<br />
<br />
<texcode><br />
\ctxlua{tex.sprint("a", "b")}<br />
\ctxlua{tex.sprint({"a", "b"})}<br />
</texcode><br />
<br />
will be read by TeX as<br />
<br />
<texcode><br />
ab<br />
</texcode> <br />
<br />
without any space in between.<br />
<br />
== Context commands ==<br />
<br />
Most commands that you would type with a backslash in plain ConTeXt, you can access from Lua with <code>context.<em>command</em></code>. Unadorned strings end up in TeX as arguments in curly braces; Lua tables end up in TeX as paramater blocks in square brackets. The following two pieces of code are equivalent:<br />
<br />
<texcode><br />
\startluacode<br />
context.chapter({first}, "Some title")<br />
context.startcolumns({n = 3, rule = "on"})<br />
context("Hello one")<br />
context.column()<br />
context("Hello two")<br />
context.column()<br />
context("Hello three")<br />
context.stopcolumns()<br />
\stopluacode<br />
</texcode><br />
<br />
<texcode><br />
\chapter[first]{Some title}<br />
\startcolumns[n=3, rule=on]<br />
Hello one<br />
\column<br />
Hello two<br />
\column<br />
Hello three<br />
\stopcolumns<br />
</texcode><br />
<br />
For a fuller account of the context.commands, see the [http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf ConTeXt Lua document] manual. It is old, but most of it still applies.<br />
<br />
One final note: arguments can also be specified in the form of nested functions. Because LuaTeX evaluates the deepest-nested argument first, this may cause the <code>context()</code> calls to be evaluated in the wrong order. For more on this, see the article on [[CLD|ConTeXt Lua documents]], and also, again, the [http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf CLD manual].<br />
<br />
= Passing arguments and buffers: ConTeXt commands that hook into Lua =<br />
<br />
== Making \command{arg1}{arg2} hook into Lua ==<br />
First, define a Lua function:<br />
<br />
<texcode><br />
\startluacode<br />
-- remember, using the userdata namespace prevents conflicts<br />
userdata = userdata or {}<br />
<br />
function userdata.surroundwithdashes(str)<br />
context("--" .. str .. "--")<br />
end<br />
\stopluacode<br />
</texcode><br />
<br />
Then define the TeX command that expands to a <code>\ctxlua</code> call:<br />
<br />
<texcode><br />
\def\surroundwd#1%<br />
{\ctxlua{userdata.surroundwithdashes([==[#1]==])}}<br />
</texcode><br />
<br />
''NB'': quoting with <code>[==[#1]==]</code><br />
([http://www.lua.org/manual/5.2/manual.html#3.1 long strings])<br />
works just like <code>"#1"</code> in most cases, but in addition <br />
it is robust against <code>#1</code> containing the quotation mark<br />
<code>"</code> which would terminate the Lua string prematurely.<br />
Inside <code>\protect .. \unprotect</code> the macros <code>\!!bs</code><br />
and <code>\!!es</code> are at your disposition.<br />
They are equivalent to <code>[===[</code> and <code>]===]</code> and --<br />
being single tokens to TeX -- parsed faster.<br />
(See [http://repo.or.cz/w/context.git/blob/refs/heads/origin:/tex/context/base/luat-ini.mkiv#l174 <code>luat-ini.mkiv</code>].)<br />
<br />
== Making \startenv...\stopenv hook into Lua ==<br />
The first job is, as ever, to have the Lua function at the ready<br />
<texcode><br />
\startluacode<br />
userdata = userdata or {}<br />
<br />
function userdata.verynarrow(buffer)<br />
-- equivalent to \startnarrower[10em]<br />
context.startnarrower({"10em"})<br />
context(buffer)<br />
context.stopnarrower()<br />
end<br />
\stopluacode<br />
</texcode><br />
<br />
Next, we define the start command of our custom buffer:<br />
<br />
<texcode><br />
\def\startverynarrow%<br />
{\dostartbuffer<br />
[verynarrow] % buffer name<br />
[startverynarrow] % command where buffer starts<br />
[stopverynarrow]} % command where buffer ends<br />
% also: command invoked when buffer stops<br />
<br />
</texcode><br />
<br />
Lastly, we define the <code>\stopverynarrow</code> command such that it passes the recently-complated buffer to our <code>verynarrow</code> Lua function:<br />
<br />
<texcode><br />
\def\stopverynarrow<br />
{\ctxlua<br />
{userdata.verynarrow(buffers.getcontent('verynarrow'))}}<br />
</texcode><br />
<br />
And that's it! The rest of this article will consist of examples.<br />
<br />
= Examples =<br />
<br />
== Arithmetic without using an abacus ==<br />
<br />
''This example demonstrates writing simple commands that invoke \ctxlua.''<br />
<br />
Doing simple arithmetic in TeX can be extremely difficult. With Lua, simple arithmetic becomes trivial. For example, if you want a macro to find the cosine of an angle (in degrees), you can write<br />
<texcode><br />
\def\COSINE#1%<br />
{\ctxlua{context(math.cos(#1*2*math.pi/360))}}<br />
</texcode><br />
<br />
The built-in <code>math.cos</code> function assumes that the argument is specified in radians, so we convert from degrees to radians on the fly. If you want to type the value of $\pi$ in an article, you can simply say<br />
<texcode><br />
$\pi = \ctxlua{context(math.pi)}$ <br />
</texcode><br />
or, if you want less precision:<br />
<texcode> <br />
$\pi = \ctxlua{context("\%.6f", math.pi)}$<br />
</texcode><br />
Notice that the percent sign is escaped.<br />
<br />
<br />
== Loops without worrying about expansion ==<br />
<br />
''This example demonstrates using Lua to write a quasi-repetitive piece of ConTeXt code.''<br />
<br />
Loops in TeX are tricky, because macro assignments and macro expansion interact in strange ways. For example, suppose we want to typeset a table showing the sum of the roll of two dice and want the output to look like this:<br />
<context source="yes"><br />
\setupcolors[state=start]<br />
\setupTABLE[each][each][width=2em,height=2em,align={middle,middle}] <br />
\setupTABLE[r][1][background=color,backgroundcolor=gray] <br />
\setupTABLE[c][1][background=color,backgroundcolor=gray]<br />
<br />
\bTABLE <br />
\bTR \bTD $(+)$ \eTD \bTD 1 \eTD \bTD 2 \eTD <br />
\bTD 3 \eTD \bTD 4 \eTD \bTD 5 \eTD \bTD 6 \eTD \eTR <br />
\bTR \bTD 1 \eTD \bTD 2 \eTD \bTD 3 \eTD <br />
\bTD 4 \eTD \bTD 5 \eTD \bTD 6 \eTD \bTD 7 \eTD \eTR <br />
\bTR \bTD 2 \eTD \bTD 3 \eTD \bTD 4 \eTD <br />
\bTD 5 \eTD \bTD 6 \eTD \bTD 7 \eTD \bTD 8 \eTD \eTR <br />
\bTR \bTD 3 \eTD \bTD 4 \eTD \bTD 5 \eTD<br />
\bTD 6 \eTD \bTD 7 \eTD \bTD 8 \eTD \bTD 9 \eTD \eTR <br />
\bTR \bTD 4 \eTD \bTD 5 \eTD \bTD 6 \eTD<br />
\bTD 7 \eTD \bTD 8 \eTD \bTD 9 \eTD \bTD 10 \eTD \eTR <br />
\bTR \bTD 5 \eTD \bTD 6 \eTD \bTD 7 \eTD <br />
\bTD 8 \eTD \bTD 9 \eTD \bTD 10 \eTD \bTD 11 \eTD \eTR <br />
\bTR \bTD 6 \eTD \bTD 7 \eTD \bTD 8 \eTD <br />
\bTD 9 \eTD \bTD 10 \eTD \bTD 11 \eTD \bTD 12 \eTD \eTR <br />
\eTABLE<br />
</context><br />
<br />
This is easy in LuaTeX. Once a Lua instance starts, TeX does not see anything until the Lua instance exits. So, we can write the loop in Lua, and simply print the values that we would have typed to the TeX stream. When the control is passed to TeX, TeX sees the input as if we had typed it by hand. This is the Lua code for the above table:<br />
<br />
<texcode><br />
\setupcolors[state=start]<br />
\setupTABLE[each][each][width=2em,height=2em,align={middle,middle}] <br />
\setupTABLE[r][1][background=color,backgroundcolor=gray] <br />
\setupTABLE[c][1][background=color,backgroundcolor=gray]<br />
<br />
\startluacode<br />
context.bTABLE()<br />
context.bTR()<br />
context.bTD() context("$(+)$") context.eTD()<br />
for j=1,6 do<br />
context.bTD() context(j) context.eTD()<br />
end<br />
context.eTR()<br />
for i=1,6 do<br />
context.bTR()<br />
context.bTD() context(i) context.eTD()<br />
for j=1,6 do<br />
context.bTD() context(i+j) context.eTD()<br />
end<br />
context.eTR()<br />
end<br />
context.eTABLE()<br />
\stopluacode<br />
</texcode><br />
<br />
== Parsing input without exploding your head ==<br />
<br />
''This example demonstrates parsing simple ASCII notation with Lua's lpeg parser.''<br />
<br />
As an example, let's consider typesetting chemical molecules in TeX. Normally, molecules should be typeset in text mode rather than math mode. <br />
If we want <br />
:H<sub>4</sub>SO<sub>4</sub><sup>+</sup>,<br />
we must type <br />
:<code>H\low{3}SO\lohi{4}{\textplus}</code>,<br />
but we'd much rather type<br />
:<code>\molecule{H_3SO_4^+}</code>.<br />
<br />
So, we need a function that can take a string like that, parse it, and turn it into the appropriate TeX code. LuaTeX includes a general parser based on PEG (parsing expression grammar) called [http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html lpeg], and it makes writing little parsers positively joyful. (Once you've got the knack of it, at least.) For example, the above <code>\molecule</code> macro can be written as follows.<br />
<br />
<texcode><br />
\startluacode<br />
<br />
-- we will put our molecule function in the userdata namespace.<br />
userdata = userdata or { }<br />
<br />
-- The formatting functions into which the captured<br />
-- superscript/subscript blocks will be fed<br />
local formatters = { }<br />
<br />
function formatters.low(one)<br />
return string.format("\\low{%s}", one)<br />
end<br />
<br />
function formatters.high(one)<br />
return string.format("\\high{%s}", one)<br />
end<br />
<br />
function formatters.lowhigh(one, two)<br />
return string.format("\\lohi{%s}{%s}", one, two)<br />
end<br />
<br />
function formatters.highlow(one, two,three)<br />
return string.format("\\lohi{%s}{%s}", one,two)<br />
end<br />
<br />
-- These are the characters we may encounter<br />
-- The `/` means we want to expand + and - to \textplus c.q. \textminus;<br />
-- this substition is not instant, but will take place inside the first <br />
-- surrounding lpeg.Cs() call.<br />
local plus = lpeg.P("+") / "\\textplus "<br />
local minus = lpeg.P("-") / "\\textminus "<br />
local character = lpeg.R("az", "AZ", "09") -- R is for 'range'<br />
local subscript = lpeg.P("_") -- P is simply for 'pattern'<br />
local superscript = lpeg.P("^")<br />
local leftbrace = lpeg.P("{")<br />
local rightbrace = lpeg.P("}")<br />
<br />
-- a ^ or _ affects either a single character, or a brace-delimited<br />
-- block. Whichever it is, call it `content`.<br />
local single = character + plus + minus<br />
local multiple = leftbrace * single^1 * rightbrace<br />
local content = single + multiple<br />
<br />
-- These are our top-level elements: non-special text, of course, and<br />
-- blocks of superscript/subscript/both.<br />
-- lpeg.Cs(content) does two things:<br />
-- (1) not all matches go into the `/ function` construction; only<br />
-- *captures* go in. The C in Cs stands for Capture. This way, <br />
-- the superscript/subscript mark gets discarded.<br />
-- (2) it expands plus/minus before they go into the formatter. The<br />
-- s in Cs stands for 'substitute in the replacement values, if any'<br />
local text = single^1<br />
local low = subscript * lpeg.Cs(content) <br />
/ formatters.low<br />
local high = superscript * lpeg.Cs(content) <br />
/ formatters.high<br />
local lowhigh = subscript * lpeg.Cs(content) * <br />
superscript * lpeg.Cs(content) <br />
/ formatters.lowhigh<br />
local highlow = superscript * lpeg.Cs(content) * <br />
subscript * lpeg.Cs(content) <br />
/ formatters.highlow<br />
<br />
-- Finally, the root element: 'moleculepattern'<br />
local moleculepattern = lpeg.Cs((lowhigh + highlow + low + high + text)^0)<br />
<br />
function thirddata.molecule(string)<br />
-- * `:match` returns the matched string. Our pattern<br />
-- `moleculepattern` should match the entire input string. Any<br />
-- *performed* substitutions are retained. (`.Cs()` performs a<br />
-- previously defined substitution.)<br />
-- * `context()` inserts the resulting string into the stream, ready for<br />
-- TeX to evaluate.<br />
context(moleculepattern:match(string))<br />
end<br />
<br />
\stopluacode<br />
<br />
\def\molecule#1{\ctxlua{thirddata.molecule("#1")}}<br />
<br />
\starttext<br />
\molecule{Hg^+}, \molecule{SO_4^{2-}}<br />
\stoptext<br />
</texcode><br />
<br />
Quite terse and readable by parser standards, isn't it?<br />
<br />
== Manipulating verbatim text ==<br />
<br />
''This example demonstrates defining a custom \start...\stop buffer that gets processed through Lua in its entirety.''<br />
<br />
Suppose we want to write an environment <code>\startdedentedtyping</code> ... <code>\stopdedentedtyping</code> that removes the indentation of the first line from every line. Thus, the output of ...<br />
<br />
<texcode><br />
\startdedentedtyping<br />
#include &lt;stdio.h&gt;<br />
void main()<br />
{<br />
print("Hello world \n") ;<br />
}<br />
\stopdedentedtyping<br />
</texcode><br />
... should be the same as the output of ...<br />
<br />
<texcode><br />
\starttyping<br />
#include &lt;stdio.h&gt;<br />
void main()<br />
{<br />
print("Hello world \n") ;<br />
}<br />
\stoptyping<br />
</texcode><br />
... even though the leading whitespace is different.<br />
<br />
Defining an environment in TeX that removes the leading spaces but leaves<br />
other spaces untouched is complicated. On the other hand, once we capture the<br />
contents of the environment, removing the leading indent or ''dedenting'' the<br />
content in Lua is easy. Here is a Lua function that uses simple string<br />
substitutions.<br />
<br />
<texcode><br />
\startluacode<br />
-- Initialize a userdata name space to keep our own functions in.<br />
-- That way, we won't interfere with anything ConTeXt keeps in <br />
-- the global name space.<br />
userdata = userdata or {}<br />
<br />
function userdata.dedentedtyping(content)<br />
local lines = string.splitlines(content)<br />
local indent = string.match(lines[1], '^ +') or ''<br />
local pattern = '^' .. indent<br />
for i=1,#lines do<br />
lines[i] = string.gsub(lines[i],pattern,"")<br />
end<br />
<br />
content = table.concat(lines,'\n')<br />
<br />
tex.sprint("\\starttyping\n" .. content .. "\\stoptyping\n")<br />
<br />
-- The typing environment looks for an explicit \type{\stoptyping}. So,<br />
-- context.starttyping() context(content) context.stoptyping()<br />
-- does not work. But<br />
-- context.starttyping() context(content) tex.sprint("\\stoptyping")<br />
-- does.<br />
end<br />
\stopluacode<br />
</texcode><br />
<br />
Here is the code for defining the <code>\startdedentedtyping...\stopdedentedtyping</code> pair:<br />
<br />
<texcode><br />
% Create an environment that stores everything <br />
% between \startdedentedtyping and \stopdedentedtyping <br />
% in a buffer named 'dedentedtyping'.<br />
\def\startdedentedtyping<br />
{\dostartbuffer<br />
[dedentedtyping]<br />
[startdedentedtyping]<br />
[stopdedentedtyping]}<br />
<br />
% On closing the dedentedtyping environment, call the LuaTeX<br />
% function dedentedtyping(), and pass it the contents of <br />
% the buffer called 'dedentedtyping'<br />
\def\stopdedentedtyping<br />
{\ctxlua<br />
{userdata.dedentedtyping(buffers.getcontent('dedentedtyping'))}}<br />
</texcode><br />
<br />
That's all. Finally, we will go into a little more detail on how TeX and Lua communicate with each other.<br />
<br />
== Other examples ==<br />
<br />
* [[Calculations_in_Lua|Calculations in Lua]] (warning date 2012)<br />
* [[LPeg|Writing a parser with LPeg]] (Lua Parsing Expression Grammars)<br />
* [[Random|Random numbers]] in ConTeXt and MetaPost<br />
* [[SQL|An example with SQL database]] <br />
<br />
= In detail: the interaction between TeX and Lua =<br />
<br />
To a first approximation, the interaction between TeX and Lua is straightforward. When TeX (i.e., the LuaTeX engine) starts, it loads the input file in memory and processes it token by token. When TeX encounters <code>\directlua</code>, it stops reading the file in memory, <em>fully expands the argument of <code>\directlua</code></em>, and passes the control to a Lua instance. The Lua instance, which runs with a few preloaded libraries, processes the expanded arguments of <code>\directlua</code>. This Lua instance has a special output stream which can be accessed using <code>tex.print(...)</code>. The function <code>tex.print(...)</code> is just like the Lua function <code>print(...)</code> except that <code>tex.print(...)</code> prints to a <em>TeX stream</em> rather than to the standard output. When the Lua instance finishes processing its input, it passes the contents of the <em>TeX stream</em> back to TeX.<ref>The output of <code>tex.print(...)</code> is buffered and not passed to TeX until the Lua instance has stopped.</ref> TeX then inserts the contents of the <em>TeX stream</em> at the current location of the file that it was reading; expands the contents of the <em>TeX stream</em>; and continues. If TeX encounters another <code>\directlua</code>, the above process is repeated. <br />
<br />
As an exercise, imagine what happens when the following input is processed by LuaTeX. The answer is in the footnotes. <ref>In this example, two different kinds of quotations are used to avoid escaping quotes. Escaping quotes inside <code>\directlua</code> is tricky. The above was a contrived example; if you ever need to escape quotes, you can use the <code>\startluacode ... \stopluacode</code> syntax.</ref><br />
<br />
<texcode><br />
\directlua%<br />
{tex.print("Depth 1 <br />
\\directlua{tex.print('Depth 2')}")}<br />
</texcode><br />
<br />
For more on this, see the [http://wiki.luatex.org/index.php/Writing_Lua_in_TeX] article on the [http://wiki.luatex.org/index.php/Main_Page LuaTeX wiki].<br />
<br />
= Notes =<br />
<references /><br />
<br />
{{note | This article is originally based on [https://www.tug.org/members/TUGboat/tb30-2/tb95mahajan-luatex.pdf this TugBoat article ]. Feel free to modify it.}}<br />
<br />
[[Category:Programming and Databases]]</div>Otaredhttps://wiki.contextgarden.net/index.php?title=variable_color&diff=29391variable color2020-06-22T09:21:50Z<p>Otared: </p>
<hr />
<div>In Metapost (or rather in Metafun) one can define arrays of colors, strings, paths, pens and other variables.<br />
An example is the following:<br />
<texcode><br />
\starttext<br />
\startMPpage<br />
string MyColors[] ;<br />
path MyPaths [] ;<br />
pen MyPens[] ;<br />
for j = -1 step 1 until 3 :<br />
MyPens[j] := pencircle scaled ((2+j)*pt) ;<br />
endfor<br />
MyPaths[-1] := (-1.5cm,1.5cm) -- (1.5cm,1.5cm) ; MyColors[-1] := "magenta" ; <br />
MyPaths[0] := (-2cm,2cm) -- (2*cos(0)*cm,-2cm) ; MyColors[0] := "darkyellow" ;<br />
MyPaths[1] := fullcircle scaled 3cm ; MyColors[1] := "darkred" ;<br />
MyPaths[2] := fullsquare scaled 2cm ; MyColors[2] := "darkgreen" ;<br />
MyPaths[3] := (-2cm,-2cm) -- (2cm,2cm) ; MyColors[3] := "darkblue" ;<br />
<br />
for j = -1 step 1 until 3 :<br />
draw MyPaths[j] withpen MyPens[j] withcolor MyColors[j] ;<br />
endfor<br />
\stopMPpage<br />
\stoptext<br />
</texcode><br />
In the above example, instead of <br />
<texcode><br />
string MyColors[] ;<br />
</texcode><br />
one could have said<br />
<texcode><br />
color MyColors[] ;<br />
</texcode><br />
and then we should have defined each component of {{code|1=MyColors}} as colors instead of strings:<br />
<texcode><br />
\starttext<br />
\startMPpage<br />
color MyColors[] ;<br />
path MyPaths [] ;<br />
pen MyPens[] ;<br />
for j = -1 step 1 until 3 :<br />
MyPens[j] := pencircle scaled ((2+j)*pt) ;<br />
endfor<br />
MyPaths[-1] := (-1.5cm,1.5cm) -- (1.5cm,1.5cm) ; MyColors[-1] := magenta ; <br />
MyPaths[0] := (-2cm,2cm) -- (2*cos(0)*cm,-2cm) ; MyColors[0] := darkyellow ;<br />
MyPaths[1] := fullcircle scaled 3cm ; MyColors[1] := darkred ;<br />
MyPaths[2] := fullsquare scaled 2cm ; MyColors[2] := darkgreen ;<br />
MyPaths[3] := (-2cm,-2cm) -- (2cm,2cm) ; MyColors[3] := darkblue ;<br />
<br />
for j = -1 step 1 until 3 :<br />
draw MyPaths[j] withpen MyPens[j] withcolor MyColors[j] ;<br />
endfor<br />
\stopMPpage<br />
\stoptext<br />
</texcode><br />
Also note that in order to specify the size of the pen in {{code|1=MyPens[j]}} we have to say {{code|1=((2+j)*pt)}} in order for Metapost to interpret correctly the desired size of the pen. The outer parentheses are to prevent MetaPost from interpreting the definition as <br />
<texcode><br />
MyPens[j] := (pencircle scaled (2+j)) * pt ;<br />
</texcode><br />
which would result in an error.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=variable_color&diff=29390variable color2020-06-22T09:20:51Z<p>Otared: </p>
<hr />
<div>In Metapost (or rather in Metafun) one can define arrays of colors, strings, paths, pens and other variables.<br />
An example is the following:<br />
<texcode><br />
\starttext<br />
\startMPpage<br />
string MyColors[] ;<br />
path MyPaths [] ;<br />
pen MyPens[] ;<br />
for j = -1 step 1 until 3 :<br />
MyPens[j] := pencircle scaled ((2+j)*pt) ;<br />
endfor<br />
MyPaths[-1] := (-1.5cm,1.5cm) -- (1.5cm,1.5cm) ; MyColors[-1] := "magenta" ; <br />
MyPaths[0] := (-2cm,2cm) -- (2*cos(0)*cm,-2cm) ; MyColors[0] := "darkyellow" ;<br />
MyPaths[1] := fullcircle scaled 3cm ; MyColors[1] := "darkred" ;<br />
MyPaths[2] := fullsquare scaled 2cm ; MyColors[2] := "darkgreen" ;<br />
MyPaths[3] := (-2cm,-2cm) -- (2cm,2cm) ; MyColors[3] := "darkblue" ;<br />
<br />
for j = -1 step 1 until 3 :<br />
draw MyPaths[j] withpen MyPens[j] withcolor MyColors[j] ;<br />
endfor<br />
\stopMPpage<br />
\stoptext<br />
</texcode><br />
In the above example, instead of <br />
<texcode><br />
string MyColors[] ;<br />
</texcode><br />
one could have said<br />
<texcode><br />
color MyColors[] ;<br />
</texcode><br />
and then we should have defined each component of {{code|1=MyColors}} as colors instead of strings:<br />
<texcode><br />
\starttext<br />
\startMPpage<br />
color MyColors[] ;<br />
path MyPaths [] ;<br />
pen MyPens[] ;<br />
for j = -1 step 1 until 3 :<br />
MyPens[j] := pencircle scaled ((2+j)*pt) ;<br />
endfor<br />
MyPaths[-1] := (-1.5cm,1.5cm) -- (1.5cm,1.5cm) ; MyColors[-1] := magenta ; <br />
MyPaths[0] := (-2cm,2cm) -- (2*cos(0)*cm,-2cm) ; MyColors[0] := darkyellow ;<br />
MyPaths[1] := fullcircle scaled 3cm ; MyColors[1] := darkred ;<br />
MyPaths[2] := fullsquare scaled 2cm ; MyColors[2] := darkgreen ;<br />
MyPaths[3] := (-2cm,-2cm) -- (2cm,2cm) ; MyColors[3] := darkblue ;<br />
<br />
for j = -1 step 1 until 3 :<br />
draw MyPaths[j] withpen MyPens[j] withcolor MyColors[j] ;<br />
endfor<br />
\stopMPpage<br />
\stoptext<br />
</texcode><br />
Also note that in order to specify the size of the pen in {{code|1=MyPens[j]}} we have to say {{code|1=((2+j)*pt)}} in order for Metapost to interpret correctly the desired size of the pen. The outer parentheses are to prevent MetaPost from interpreting the definition as <br />
<texcode><br />
MyPens[j] := (pencircle scaled (2+j)) * pt ;<br />
</texcode></div>Otaredhttps://wiki.contextgarden.net/index.php?title=variable_color&diff=29389variable color2020-06-22T09:14:08Z<p>Otared: Created page with "In Metapost (or rather in Metafun) one can define arrays of colors, strings, paths, pens and other variables. An example is the following: <texcode> \starttext \startMPpage s..."</p>
<hr />
<div>In Metapost (or rather in Metafun) one can define arrays of colors, strings, paths, pens and other variables.<br />
An example is the following:<br />
<texcode><br />
\starttext<br />
\startMPpage<br />
string MyColors[] ;<br />
path MyPaths [] ;<br />
pen MyPens[] ;<br />
for j = -1 step 1 until 3 :<br />
MyPens[j] := pencircle scaled ((2+j)*pt) ;<br />
endfor<br />
MyPaths[-1] := (-1.5cm,1.5cm) -- (1.5cm,1.5cm) ; MyColors[-1] := "magenta" ; <br />
MyPaths[0] := (-2cm,2cm) -- (2*cos(0)*cm,-2cm) ; MyColors[0] := "darkyellow" ;<br />
MyPaths[1] := fullcircle scaled 3cm ; MyColors[1] := "darkred" ;<br />
MyPaths[2] := fullsquare scaled 2cm ; MyColors[2] := "darkgreen" ;<br />
MyPaths[3] := (-2cm,-2cm) -- (2cm,2cm) ; MyColors[3] := "darkblue" ;<br />
<br />
for j = -1 step 1 until 3 :<br />
draw MyPaths[j] withpen MyPens[j] withcolor MyColors[j] ;<br />
endfor<br />
\stopMPpage<br />
\stoptext<br />
</texcode><br />
In the above example, instead of <br />
<texcode><br />
string MyColors[] ;<br />
</texcode><br />
one could have said<br />
<texcode><br />
color MyColors[] ;<br />
</texcode><br />
and then we should have defined each component of {{code|1=MyColors}} as colors instead of strings:<br />
<texcode><br />
\starttext<br />
\startMPpage<br />
color MyColors[] ;<br />
path MyPaths [] ;<br />
pen MyPens[] ;<br />
for j = -1 step 1 until 3 :<br />
MyPens[j] := pencircle scaled ((2+j)*pt) ;<br />
endfor<br />
MyPaths[-1] := (-1.5cm,1.5cm) -- (1.5cm,1.5cm) ; MyColors[-1] := magenta ; <br />
MyPaths[0] := (-2cm,2cm) -- (2*cos(0)*cm,-2cm) ; MyColors[0] := darkyellow ;<br />
MyPaths[1] := fullcircle scaled 3cm ; MyColors[1] := darkred ;<br />
MyPaths[2] := fullsquare scaled 2cm ; MyColors[2] := darkgreen ;<br />
MyPaths[3] := (-2cm,-2cm) -- (2cm,2cm) ; MyColors[3] := darkblue ;<br />
<br />
for j = -1 step 1 until 3 :<br />
draw MyPaths[j] withpen MyPens[j] withcolor MyColors[j] ;<br />
endfor<br />
\stopMPpage<br />
\stoptext<br />
</texcode><br />
Also note that in order to specify the size of the pen in {{code|1=MyPens[j]}} we have to say {{code|1=((2+j)*pt)}} in order for Metapost to interpret correctly the desired size of the pen.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Installing_ConTeXt_LMTX_on_MacOS&diff=25326Installing ConTeXt LMTX on MacOS2019-12-13T08:34:47Z<p>Otared: </p>
<hr />
<div>Since April 2019, there is a new flavor of ConTeXt, named « LuaMetaTeX » meant to integrate Lua, Metapost and TeX, the latter meaning ConTeXt.<br />
It is going to be self-contained and smaller than previous versions of ConTeXt mkiv and mkii. <br />
<br />
See http://www.pragma-ade.com/install.htm [http://www.pragma-ade.com/install.htm] where you can find the installer script for the machine you are using.<br />
<br />
Since a few users have encountered difficulties in installing, and updating to, beta releases of LuaMetaTeX on MacOS, below we describe an installtion procedure for MacOS. For other platforms the procedures are more or less analogous.<br />
<br />
<br />
== Installing on MacOS up to 10.14 ==<br />
<br />
* '''Beware:''' As of December 2019, the following instructions work for MacOS up to version 10.14 but do not work for MacOS 10.15 Catalina, due to the Apple's System Integrity Protection (SIP). Please see the dedicated section for installing on MacOS 10.15 Catalina<br />
<br />
* First download http://lmtx.pragma-ade.nl/install-lmtx/context-osx-64.zip <br />
<br />
* Put the unzipped folder « context-osx-64 « wherever you wish: for instance in your home directory. Thus the path to your future LuaMetaTeX directory will be (and below this is what we assume)<br />
~/context-osx-64/<br />
<br />
* Open a Terminal window (Terminal.app is found in /Applications/Utilities). In the Terminal window type<br />
cd ~/context-osx-64/<br />
<br />
* Then you should be able to execute the command<br />
sh install.sh<br />
<br />
* It may happen that MacOS will not allow you to execute the above shell command, or the mtxrun script which is called by install.sh. In that case, you should open the System Préferences (from the Apple menu) and there choose Security & Privacy, and go to the General pane. There you should be able to authorize the shell script, which has been blocked, to be executed. After that you issue again the command<br />
sh install.sh<br />
and MacOS will ask you again whether you really want to execute mtxrun: you say YES!<br />
<br />
* After that, if everything goes smoothly a complete functionning tree of LuaMetaTeX, complete with dozens of manuals, will be installed in the folder<br />
~/context-osx-64/<br />
and whenever you want to update your installation of LuaMetaTeX, it is enough to go to the above directory in Terminal and do again<br />
sh install.sh<br />
<br />
* If you want to use LuaMetaTeX from the Terminal interface you should add the path of your installation to $PATH by adding to your<br />
.bashrc<br />
(or any other flavor of shell interface you are using) the line<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin:$PATH<br />
(if you have installed « context-osx-64 » elsewhere, please replace the segment « ~/context-osx-64 » with the path to that folder).<br />
<br />
* A better choice is to use TeXShop [https://pages.uoregon.edu/koch/texshop/obtaining.html]. Once TeXShop is installed, go to<br />
~/Library/TeXShop/Engine<br />
and there duplicate for instance the file « ConTeXt (LuaTeX).engine » (or any other of the files « .engine »). Name the duplicate<br />
LuaMetaTeX.engine<br />
and open it in TeXShop (or any text editor). Replace the content of the file « LuaMetaTeX.engine » with the following lines:<br />
#!/bin/bash<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin<br />
~/context-osx-64/tex/texmf-osx-64/bin/mtxrun --autogenerate --script context --directives="system.showerror" --autopdf "$1" --purgeall<br />
then save and close the file. <br />
<br />
* If you want to use LuaMetaTeX as your default TeX typesetter in TeXShop, go to TeXShop -> Preferences, then under the tab « Typesetting » and the « Default Command » section choose the radio button « Command listed below « and below that type<br />
LuaMetaTeX<br />
<br />
* In principle, once the above PATH has been added, typing in a Terminal window<br />
which context<br />
or <br />
context --version<br />
you should get the path to context binary and its version. If this is the case, then you can typeset a file named « myfile.tex » from the Terminal by typing:<br />
context myfile.tex<br />
<br />
* It may happen that for some reasons not everything goes smoothly and you cannot typeset any document. In that case, try typing in a Terminal window:<br />
which luametatex<br />
in order to see whether<br />
~/context-osx-64/tex/texmf-osx-64/bin/luametatex<br />
is present or not.<br />
<br />
* If not, the download has not been complete.<br />
<br />
* If luametatex is present, in the Terminal type:<br />
luametatex --version<br />
and see whether it says, among other things:<br />
Functionality : level 20191206<br />
(here 20191206 is the date stamp of luametatex, of course this will change during time). If not, there might be an issue with the files you have downloaded.<br />
<br />
* If yes, in the folder<br />
~/context-osx-64/tex<br />
remove the folder « texmf-cache » and then in the Terminal run:<br />
mtxrun --generate<br />
In principle, after that, the folder texmf-cache is present again.<br />
<br />
* If this is the case, in the Terminal window type:<br />
context --make --all<br />
If everything goes nicely, you should be able to see LuaMetaTeX functionning.<br />
<br />
== Installing on MacOS 10.15 Catlina ==<br />
<br />
* First get your hands on a zip file of a complete tree of<br />
context-osx-64<br />
which works on MacOS 10.14 or previous, from someone you know... You can always contact the ConTeXt mailing list and ask this from someone (one day there might be a direct distribution of this zip file). If you have yourself access to a MacOS less than or equal to 10.14, you can follow the instructions above, create a context-osx-64 tree, and then copy that tree on a USB memory stick and transfer it to your MacOS 10.15. In thi scase you should wipe the <br />
context-osx-64/tex/texmf-cache<br />
folder in your copy destined to MacOS 10.15.<br />
<br />
* Disable the SIP (System Integrity Protection) following (for example) the instructions given here<br />
https://www.macworld.co.uk/how-to/mac/how-turn-off-mac-os-x-system-integrity-protection-rootless-3638975/<br />
Later, you can put back the SIP, once the installation of LuaMetaTeX is over.<br />
<br />
* In a Terminal window, reinstate the « Anywhere » option in Gatekeeper (which is visible in the Apple menu -> Préferences -> Security & Privacy) by doing<br />
sudo spctl --master-disable<br />
Later, you can put back the Gatekeeper default by saying<br />
sudo spctl --master-enable<br />
once the installation of LuaMetaTeX is over.<br />
<br />
* Restart your Mac and put the folder « context-osx-64 « wherever you wish: for instance in your home directory. Thus the path to your future LuaMetaTeX directory will be (and below this is what we assume)<br />
~/context-osx-64/<br />
<br />
* In a Terminal window export the PATH<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin:$PATH<br />
You can add this path to your .zshrc or .bashrc if you wish to use LuaMetaTeX from everywhere on your Mac.<br />
<br />
* You can test your installation by saying in the Terminal window<br />
which luametatex<br />
and see whether it points to something in your context-osx-64 tree.<br />
<br />
* In the Terminal window run<br />
mtxrun --generate<br />
and next<br />
context --make --all<br />
<br />
* Now in principle you are done and you can typeset a test file named » mytest.tex » by saying<br />
context mytest.tex<br />
<br />
* At this point, you can enable again SIP and Gatekeeper as indicated above.<br />
<br />
* In order to update to the latest version of LuaMetaTeX in a Terminal do<br />
cd ~/context-osx-64/<br />
sh install.sh<br />
The update should go ahead without any interference from Gatekeeper or SIP, even if you have trurned them to their default settings.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Installing_ConTeXt_LMTX_on_MacOS&diff=25325Installing ConTeXt LMTX on MacOS2019-12-13T08:04:25Z<p>Otared: /* Installing on MacOS */</p>
<hr />
<div>Since April 2019, there is a new flavor of ConTeXt, named « LuaMetaTeX » meant to integrate Lua, Metapost and TeX, the latter meaning ConTeXt.<br />
It is going to be self-contained and smaller than previous versions of ConTeXt mkiv and mkii. <br />
<br />
See http://www.pragma-ade.com/install.htm [http://www.pragma-ade.com/install.htm] where you can find the installer script for the machine you are using.<br />
<br />
Since a few users have encountered difficulties in installing, and updating to, beta releases of LuaMetaTeX on MacOS, below we describe an installtion procedure for MacOS. For other platforms the procedures are more or less analogous.<br />
<br />
<br />
== Installing on MacOS up to 10.14 ==<br />
<br />
* '''Beware:''' As of December 2019, the following instructions work for MacOS up to version 10.14 but do not work for MacOS 10.15 Catalina, due to the Apple's System Integrity Protection (SIP). Please see the dedicated section for installing on MacOS 10.15 Catalina<br />
<br />
* First download http://lmtx.pragma-ade.nl/install-lmtx/context-osx-64.zip <br />
<br />
* Put the unzipped folder « context-osx-64 « whereever you wish: for instance in your home directory. Thus the path to your future LuaMetaTeX directory will be (and below this is what we assume)<br />
~/context-osx-64/<br />
<br />
* Open a Terminal window (Terminal.app is found in /Applications/Utilities). In the Terminal window type<br />
cd ~/context-osx-64/<br />
<br />
* Then you should be able to execute the command<br />
sh install.sh<br />
<br />
* It may happen that MacOS will not allow you to execute the above shell command, or the mtxrun script which is called by install.sh. In that case, you should open the System Préferences (from the Apple menu) and there choose Security & Privacy, and go to the General pane. There you should be able to authorize the shell script, which has been blocked, to be executed. After that you issue again the command<br />
sh install.sh<br />
and MacOS will ask you again whether you really want to execute mtxrun: you say YES!<br />
<br />
* After that, if everything goes smoothly a complete functionning tree of LuaMetaTeX, complete with dozens of manuals, will be installed in the folder<br />
~/context-osx-64/<br />
and whenever you want to update your installation of LuaMetaTeX, it is enough to go to the above directory in Terminal and do again<br />
sh install.sh<br />
<br />
* If you want to use LuaMetaTeX from the Terminal interface you should add the path of your installation to $PATH by adding to your<br />
.bashrc<br />
(or any other flavor of shell interface you are using) the line<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin:$PATH<br />
(if you have installed « context-osx-64 » elsewhere, please replace the segment « ~/context-osx-64 » with the path to that folder).<br />
<br />
* A better choice is to use TeXShop [https://pages.uoregon.edu/koch/texshop/obtaining.html]. Once TeXShop is installed, go to<br />
~/Library/TeXShop/Engine<br />
and there duplicate for instance the file « ConTeXt (LuaTeX).engine » (or any other of the files « .engine »). Name the duplicate<br />
LuaMetaTeX.engine<br />
and open it in TeXShop (or any text editor). Replace the content of the file « LuaMetaTeX.engine » with the following lines:<br />
#!/bin/bash<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin<br />
~/context-osx-64/tex/texmf-osx-64/bin/mtxrun --autogenerate --script context --directives="system.showerror" --autopdf "$1" --purgeall<br />
then save and close the file. <br />
<br />
* If you want to use LuaMetaTeX as your default TeX typesetter in TeXShop, go to TeXShop -> Preferences, then under the tab « Typesetting » and the « Default Command » section choose the radio button « Command listed below « and below that type<br />
LuaMetaTeX<br />
<br />
* In principle, once the above PATH has been added, typing in a Terminal window<br />
which context<br />
or <br />
context --version<br />
you should get the path to context binary and its version. If this is the case, then you can typeset a file named « myfile.tex » from the Terminal by typing:<br />
context myfile.tex<br />
<br />
* It may happen that for some reasons not everything goes smoothly and you cannot typeset any document. In that case, try typing in a Terminal window:<br />
which luametatex<br />
in order to see whether<br />
~/context-osx-64/tex/texmf-osx-64/bin/luametatex<br />
is present or not.<br />
<br />
* If not, the download has not been complete.<br />
<br />
* If luametatex is present, in the Terminal type:<br />
luametatex --version<br />
and see whether it says, among other things:<br />
Functionality : level 20191206<br />
(here 20191206 is the date stamp of luametatex, of course this will change during time). If not, there might be an issue with the files you have downloaded.<br />
<br />
* If yes, in the folder<br />
~/context-osx-64/tex<br />
remove the folder « texmf-cache » and then in the Terminal run:<br />
mtxrun --generate<br />
In principle, after that, the folder texmf-cache is present again.<br />
<br />
* If this is the case, in the Terminal window type:<br />
context --make --all<br />
If everything goes nicely, you should be able to see LuaMetaTeX functionning.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Installing_ConTeXt_LMTX_on_MacOS&diff=25309Installing ConTeXt LMTX on MacOS2019-12-08T11:01:36Z<p>Otared: </p>
<hr />
<div>Since April 2019, there is a new flavor of ConTeXt, named « LuaMetaTeX » meant to integrate Lua, Metapost and TeX, the latter meaning ConTeXt.<br />
It is going to be self-contained and smaller than previous versions of ConTeXt mkiv and mkii. <br />
<br />
See http://www.pragma-ade.com/install.htm [http://www.pragma-ade.com/install.htm] where you can find the installer script for the machine you are using.<br />
<br />
Since a few users have encountered difficulties in installing, and updating to, beta releases of LuaMetaTeX on MacOS, below we describe an installtion procedure for MacOS. For other platforms the procedures are more or less analogous.<br />
<br />
<br />
== Installing on MacOS ==<br />
<br />
* '''Beware:''' As of December 8th 2019, the following instructions do not work for MacOS 10.15 Catalina, due to the Apple's System Integrity Protection (SIP). This page will be updated whenever we find a solution to this problem.<br />
<br />
* First download http://lmtx.pragma-ade.nl/install-lmtx/context-osx-64.zip <br />
<br />
* Put the unzipped folder « context-osx-64 « whereever you wish: for instance in your home directory. Thus the path to your future LuaMetaTeX directory will be (and below this is what we assume)<br />
~/context-osx-64/<br />
<br />
* Open a Terminal window (Terminal.app is found in /Applications/Utilities). In the Terminal window type<br />
cd ~/context-osx-64/<br />
<br />
* Then you should be able to execute the command<br />
sh install.sh<br />
<br />
* It may happen that MacOS will not allow you to execute the above shell command, or the mtxrun script which is called by install.sh. In that case, you should open the System Préferences (from the Apple menu) and there choose Security & Privacy, and go to the General pane. There you should be able to authorize the shell script, which has been blocked, to be executed. After that you issue again the command<br />
sh install.sh<br />
and MacOS will ask you again whether you really want to execute mtxrun: you say YES!<br />
<br />
* After that, if everything goes smoothly a complete functionning tree of LuaMetaTeX, complete with dozens of manuals, will be installed in the folder<br />
~/context-osx-64/<br />
and whenever you want to update your installation of LuaMetaTeX, it is enough to go to the above directory in Terminal and do again<br />
sh install.sh<br />
<br />
* If you want to use LuaMetaTeX from the Terminal interface you should add the path of your installation to $PATH by adding to your<br />
.bashrc<br />
(or any other flavor of shell interface you are using) the line<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin:$PATH<br />
(if you have installed « context-osx-64 » elsewhere, please replace the segment « ~/context-osx-64 » with the path to that folder).<br />
<br />
* A better choice is to use TeXShop [https://pages.uoregon.edu/koch/texshop/obtaining.html]. Once TeXShop is installed, go to<br />
~/Library/TeXShop/Engine<br />
and there duplicate for instance the file « ConTeXt (LuaTeX).engine » (or any other of the files « .engine »). Name the duplicate<br />
LuaMetaTeX.engine<br />
and open it in TeXShop (or any text editor). Replace the content of the file « LuaMetaTeX.engine » with the following lines:<br />
#!/bin/bash<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin<br />
~/context-osx-64/tex/texmf-osx-64/bin/mtxrun --autogenerate --script context --directives="system.showerror" --autopdf "$1" --purgeall<br />
then save and close the file. <br />
<br />
* If you want to use LuaMetaTeX as your default TeX typesetter in TeXShop, go to TeXShop -> Preferences, then under the tab « Typesetting » and the « Default Command » section choose the radio button « Command listed below « and below that type<br />
LuaMetaTeX<br />
<br />
* In principle, once the above PATH has been added, typing in a Terminal window<br />
which context<br />
or <br />
context --version<br />
you should get the path to context binary and its version. If this is the case, then you can typeset a file named « myfile.tex » from the Terminal by typing:<br />
context myfile.tex<br />
<br />
* It may happen that for some reasons not everything goes smoothly and you cannot typeset any document. In that case, try typing in a Terminal window:<br />
which luametatex<br />
in order to see whether<br />
~/context-osx-64/tex/texmf-osx-64/bin/luametatex<br />
is present or not.<br />
<br />
* If not, the download has not been complete.<br />
<br />
* If luametatex is present, in the Terminal type:<br />
luametatex --version<br />
and see whether it says, among other things:<br />
Functionality : level 20191206<br />
(here 20191206 is the date stamp of luametatex, of course this will change during time). If not, there might be an issue with the files you have downloaded.<br />
<br />
* If yes, in the folder<br />
~/context-osx-64/tex<br />
remove the folder « texmf-cache » and then in the Terminal run:<br />
mtxrun --generate<br />
In principle, after that, the folder texmf-cache is present again.<br />
<br />
* If this is the case, in the Terminal window type:<br />
context --make --all<br />
If everything goes nicely, you should be able to see LuaMetaTeX functionning.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Installing_ConTeXt_LMTX_on_MacOS&diff=25308Installing ConTeXt LMTX on MacOS2019-12-08T05:40:26Z<p>Otared: </p>
<hr />
<div>Since April 2019, there is a new flavor of ConTeXt, named « LuaMetaTeX » meant to integrate Lua, Metapost and TeX, the latter meaning ConTeXt.<br />
It is going to be self-contained and smaller than previous versions of ConTeXt mkiv and mkii. <br />
<br />
See http://www.pragma-ade.com/install.htm [http://www.pragma-ade.com/install.htm] where you can find the installer script for the machine you are using.<br />
<br />
Since a few users have encountered difficulties in installing, and updating to, beta releases of LuaMetaTeX on MacOS, below we describe an installtion procedure for MacOS. For other platforms the procedures are more or less analogous.<br />
<br />
<br />
== Installing on MacOS ==<br />
<br />
* First download http://lmtx.pragma-ade.nl/install-lmtx/context-osx-64.zip <br />
<br />
* Put the unzipped folder « context-osx-64 « whereever you wish: for instance in your home directory. Thus the path to your future LuaMetaTeX directory will be (and below this is what we assume)<br />
~/context-osx-64/<br />
<br />
* Open a Terminal window (Terminal.app is found in /Applications/Utilities). In the Terminal window type<br />
cd ~/context-osx-64/<br />
<br />
* Then you should be able to execute the command<br />
sh install.sh<br />
<br />
* It may happen that MacOS will not allow you to execute the above shell command, or the mtxrun script which is called by install.sh. In particular this happens beginning with MacOS 10.15 Catalina, where GateKeeper tries to protect you against yourself… In that case, you should open the System Préferences (from the Apple menu) and there choose Security & Privacy, and go to the General pane. There you should be able to authorize the shell script, which has been blocked, to be executed. After that you issue again the command<br />
sh install.sh<br />
and MacOS will ask you again whether you really want to execute mtxrun: you say YES!<br />
<br />
* After that, if everything goes smoothly a complete functionning tree of LuaMetaTeX, complete with dozens of manuals, will be installed in the folder<br />
~/context-osx-64/<br />
and whenever you want to update your installation of LuaMetaTeX, it is enough to go to the above directory in Terminal and do again<br />
sh install.sh<br />
<br />
* If you want to use LuaMetaTeX from the Terminal interface you should add the path of your installation to $PATH by adding to your<br />
.bashrc<br />
(or any other flavor of shell interface you are using) the line<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin:$PATH<br />
(if you have installed « context-osx-64 » elsewhere, please replace the segment « ~/context-osx-64 » with the path to that folder).<br />
<br />
* A better choice is to use TeXShop [https://pages.uoregon.edu/koch/texshop/obtaining.html]. Once TeXShop is installed, go to<br />
~/Library/TeXShop/Engine<br />
and there duplicate for instance the file « ConTeXt (LuaTeX).engine » (or any other of the files « .engine »). Name the duplicate<br />
LuaMetaTeX.engine<br />
and open it in TeXShop (or any text editor). Replace the content of the file « LuaMetaTeX.engine » with the following lines:<br />
#!/bin/bash<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin<br />
~/context-osx-64/tex/texmf-osx-64/bin/mtxrun --autogenerate --script context --directives="system.showerror" --autopdf "$1" --purgeall<br />
then save and close the file. <br />
<br />
* If you want to use LuaMetaTeX as your default TeX typesetter in TeXShop, go to TeXShop -> Preferences, then under the tab « Typesetting » and the « Default Command » section choose the radio button « Command listed below « and below that type<br />
LuaMetaTeX<br />
<br />
* In principle, once the above PATH has been added, typing in a Terminal window<br />
which context<br />
or <br />
context --version<br />
you should get the path to context binary and its version. If this is the case, then you can typeset a file named « myfile.tex » from the Terminal by typing:<br />
context myfile.tex<br />
<br />
* It may happen that for some reasons not everything goes smoothly and you cannot typeset any document. In that case, try typing in a Terminal window:<br />
which luametatex<br />
in order to see whether<br />
~/context-osx-64/tex/texmf-osx-64/bin/luametatex<br />
is present or not.<br />
<br />
* If not, the download has not been complete.<br />
<br />
* If luametatex is present, in the Terminal type:<br />
luametatex --version<br />
and see whether it says, among other things:<br />
Functionality : level 20191206<br />
(here 20191206 is the date stamp of luametatex, of course this will change during time). If not, there might be an issue with the files you have downloaded.<br />
<br />
* If yes, in the folder<br />
~/context-osx-64/tex<br />
remove the folder « texmf-cache » and then in the Terminal run:<br />
mtxrun --generate<br />
In principle, after that, the folder texmf-cache is present again.<br />
<br />
* If this is the case, in the Terminal window type:<br />
context --make --all<br />
If everything goes nicely, you should be able to see LuaMetaTeX functionning.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Installing_ConTeXt_LMTX_on_MacOS&diff=25306Installing ConTeXt LMTX on MacOS2019-12-07T21:04:50Z<p>Otared: </p>
<hr />
<div>Since April 2019, there is a new flavor of ConTeXt, named « LuaMetaTeX » meant to integrate Lua, Metapost and TeX, the latter meaning ConTeXt.<br />
It is going to be self-contained and smaller than previous versions of ConTeXt mkiv and mkii. <br />
<br />
See http://www.pragma-ade.com/install.htm [http://www.pragma-ade.com/install.htm] where you can find the installer script for the machine you are using.<br />
<br />
Since a few users have encountered difficulties in installing, and updating to, beta releases of LuaMetaTeX on MacOS, below we describe an installtion procedure for MacOS. For other platforms the procedures are more or less analogous.<br />
<br />
<br />
== Installing on MacOS ==<br />
<br />
* First download http://lmtx.pragma-ade.nl/install-lmtx/context-osx-64.zip <br />
<br />
* Put the unzipped folder « context-osx-64 « whereever you wish: for instance in your home directory. Thus the path to your future LuaMetaTeX directory will be (and below this is what we assume)<br />
~/context-osx-64/<br />
<br />
* Open a Terminal window (Terminal.app is found in /Applications/Utilities). In the Terminal window type<br />
cd ~/context-osx-64/<br />
<br />
* Then you should be able to execute the command<br />
sh install.sh<br />
<br />
* It may happen that MacOS will not allow you to execute the above shell command, in particular this happens beginning with MacOS 10.15 Catalina, where GateKeeper tries to protect you against yourself… In that case, you should open the System Préferences (from the Apple menu) and there choose Security & Privacy, and go to the General pane. There you should be able to authorize the shell script which has been blocked to be executed.<br />
<br />
* After that, if everything goes smoothly a complete functionning tree of LuaMetaTeX, complete with dozens of manuals, will be installed in the folder<br />
~/context-osx-64/<br />
and whenever you want to update your installation of LuaMetaTeX, it is enough to go to the above directory in Terminal and do again<br />
sh install.sh<br />
<br />
* If you want to use LuaMetaTeX from the Terminal interface you should add the path of your installation to $PATH by adding to your<br />
.bashrc<br />
(or any other flavor of shell interface you are using) the line<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin:$PATH<br />
(if you have installed « context-osx-64 » elsewhere, please replace the segment « ~/context-osx-64 » with the path to that folder).<br />
<br />
* A better choice is to use TeXShop [https://pages.uoregon.edu/koch/texshop/obtaining.html]. Once TeXShop is installed, go to<br />
~/Library/TeXShop/Engine<br />
and there duplicate for instance the file « ConTeXt (LuaTeX).engine » (or any other of the files « .engine »). Name the duplicate<br />
LuaMetaTeX.engine<br />
and open it in TeXShop (or any text editor). Replace the content of the file « LuaMetaTeX.engine » with the following lines:<br />
#!/bin/bash<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin<br />
~/context-osx-64/tex/texmf-osx-64/bin/mtxrun --autogenerate --script context --directives="system.showerror" --autopdf "$1" --purgeall<br />
then save and close the file. <br />
<br />
* If you want to use LuaMetaTeX as your default TeX typesetter in TeXShop, go to TeXShop -> Preferences, then under the tab « Typesetting » and the « Default Command » section choose the radio button « Command listed below « and below that type<br />
LuaMetaTeX<br />
<br />
* In principle, once the above PATH has been added, typing in a Terminal window<br />
which context<br />
or <br />
context --version<br />
you should get the path to context binary and its version. If this is the case, then you can typeset a file named « myfile.tex » from the Terminal by typing:<br />
context myfile.tex<br />
<br />
* It may happen that for some reasons not everything goes smoothly and you cannot typeset any document. In that case, try typing in a Terminal window:<br />
which luametatex<br />
in order to see whether<br />
~/context-osx-64/tex/texmf-osx-64/bin/luametatex<br />
is present or not.<br />
<br />
* If not, the download has not been complete.<br />
<br />
* If luametatex is present, in the Terminal type:<br />
luametatex --version<br />
and see whether it says, among other things:<br />
Functionality : level 20191206<br />
(here 20191206 is the date stamp of luametatex, of course this will change during time). If not, there might be an issue with the files you have downloaded.<br />
<br />
* If yes, in the folder<br />
~/context-osx-64/tex<br />
remove the folder « texmf-cache » and then in the Terminal run:<br />
mtxrun --generate<br />
In principle, after that, the folder texmf-cache is present again.<br />
<br />
* If this is the case, in the Terminal window type:<br />
context --make --all<br />
If everything goes nicely, you should be able to see LuaMetaTeX functionning.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Installing_ConTeXt_LMTX_on_MacOS&diff=25305Installing ConTeXt LMTX on MacOS2019-12-07T20:59:41Z<p>Otared: Created page with "Since April 2019, there is a new flavor of ConTeXt, named « LuaMetaTeX » meant to integrate Lua, Metapost and TeX, the latter meaning ConTeXt. It is going to be self-contai..."</p>
<hr />
<div>Since April 2019, there is a new flavor of ConTeXt, named « LuaMetaTeX » meant to integrate Lua, Metapost and TeX, the latter meaning ConTeXt.<br />
It is going to be self-contained and smaller than previous versions of ConTeXt mkiv and mkii. <br />
<br />
See http://www.pragma-ade.com/install.htm [http://www.pragma-ade.com/install.htm] where you can find the installer script for the machine you are using.<br />
<br />
Since a few users have encountered difficulties in installing, and updating to, beta releases of LuaMetaTeX on MacOS, below we describe an installtion procedure for MacOS. For other platforms the procedures are more or less analogous.<br />
<br />
<br />
== Installing on MacOS ==<br />
<br />
* First download http://lmtx.pragma-ade.nl/install-lmtx/context-osx-64.zip <br />
<br />
* Put the unzipped folder « context-osx-64 « whereever you wish: for instance in your home directory. Thus the path to your future LuaMetaTeX directory will be (and below this is what we assume)<br />
~/context-osx-64/<br />
<br />
* Open a Terminal window (Terminal.app is found in /Applications/Utilities). In the Terminal window type<br />
cd ~/context-osx-64/<br />
<br />
* Then you should be able to execute the command<br />
sh install.sh<br />
<br />
* It may happen that MacOS will not allow you to execute the above shell command, in particular this happens beginning with MacOS 10.15 Catalina, where GateKeeper tries to protect you against yourself… In that case, you should open the System Préferences (from the Apple menu) and there choose Security & Privacy, and go to the General pane. There you should be able to authorize the shell script which has been blocked to be executed.<br />
<br />
* After that, if everything goes smoothly a complete functionning tree of LuaMetaTeX, complete with dozens of manuals, will be installed in the folder<br />
~/context-osx-64/<br />
and whenever you want to update your installation of LuaMetaTeX, it is enough to go to the above directory in Terminal and do again<br />
sh install.sh<br />
<br />
* If you want to use LuaMetaTeX from the Terminal interface you should add the path of your installation to $PATH by adding this to your<br />
.bashrc<br />
(or any other flavor of shell interface you are using) by adding to that file the line<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin:$PATH<br />
(if you have installed « context-osx-64 » elsewhere, please replace the segment « ~/context-osx-64 » with the path to that folder).<br />
<br />
* A better choice is to use TeXShop [https://pages.uoregon.edu/koch/texshop/obtaining.html]. Once TeXShop is installed, go to<br />
~/Library/TeXShop/Engine<br />
and there duplicate for instance the file « ConTeXt (LuaTeX).engine » (or any other of the files « .engine »). Name the duplicate<br />
LuaMetaTeX.engine<br />
and open it in TeXShop (or any text editor). Replace the content of the file « LuaMetaTeX.engine » with the following lines:<br />
#!/bin/bash<br />
export PATH=~/context-osx-64/tex/texmf-osx-64/bin:~/context-osx-64/bin:~/context-osx-64/tex/texmf-osx-64/bin<br />
~/context-osx-64/tex/texmf-osx-64/bin/mtxrun --autogenerate --script context --directives="system.showerror" --autopdf "$1" --purgeall<br />
then save and close the file. <br />
<br />
* If you want to use LuaMetaTeX as your default TeX typesetter in TeXShop, go to TeXShop -> Preferences, then under the tab « Typesetting » and the « Default Command » section choose the radio button « Command listed below « and below that type<br />
LuaMetaTeX<br />
<br />
* In principle, once the above PATH has been added, typing in a Terminal window<br />
which context<br />
or <br />
context --version<br />
you should get the path to context binary and its version. If this is the case, then you can typeset a file named « myfile.tex » from the Terminal by typing:<br />
context myfile.tex<br />
<br />
* It may happen that for some reasons not everything goes smoothly and you cannot typeset any document. In that case, try typing in a Terminal window:<br />
which luametatex<br />
in order to see whether<br />
~/context-osx-64/tex/texmf-osx-64/bin/luametatex<br />
is present or not.<br />
<br />
* If not, the download has not been complete.<br />
<br />
* If luametatex is present, in the Terminal type:<br />
luametatex --version<br />
and see whether it says, among other things:<br />
Functionality : level 20191206<br />
(here 20191206 is the date stamp of luametatex, of course this will change during time). If not, there might be an issue with the files you have downloaded.<br />
<br />
* If yes, in the folder<br />
~/context-osx-64/tex<br />
remove the folder « texmf-cache » and then in the Terminal run:<br />
mtxrun --generate<br />
In principle, after that, the folder texmf-cache is present again.<br />
<br />
* If this is the case, in the Terminal window type:<br />
context --make --all<br />
If everything goes nicely, you should be able to see LuaMetaTeX functionning.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=CLD_passing_variables&diff=23752CLD passing variables2018-02-14T12:21:35Z<p>Otared: </p>
<hr />
<div>CLD (Context Lua Document) is the environment which lets you interact with TeX from inside Lua code. For more informations on this see the manual cld-mkiv.pdf, which is in <br />
[ConTeXt-StandAlone]/tex/texmf-context/doc/context/documents/general/manuals/cld-mkiv.pdf<br />
inside your ConTeXt directory.<br />
<br />
Now if one needs to pass arguments from Lua to a macro command defined in ConTeXt, one has several possibilities.<br />
<br />
First, if {{code|\mycommand}} is defined on the TeX end and takes one parameter, as in {{code|\mycommand{Myvariable}}}, then in Lua one can say {{code|context.mycommand("Myvariable")}}.<br />
<br />
The same applies to all the commands which take one or more parameters, provided that in their definition no specific delimiters is used. If {{code|\mycommand}} takes two arguments, then saying {{code|context.mycommand("Myfirstargument","Mysecondargument")}} in the Lua end, results in {{code|\mycommand{Myfirstargument}{Mysecondargument}}} on the TeX end.<br />
<br />
Next, for commands which use specific delimiters, such as the {{code|\MyGoTo}} command defined below, then one has to use the construction {{code|context.MyGoTo({"Myfirstargument"},{"Mysecondargument"})}}.<br />
<br />
Finally another construction (which works also for the above cases) which is there more specifically for some Lua reserved words (such as {{code|goto, end, if}}, present also as TeX commands) is to use the construction {{code|context["goto"]("some argument",{"ref:somewhere"})}} in order to obtain {{code|\goto{some argument}[ref:somewhere]}}.<br />
<br />
All this being said, it is better to give some examples below (try the code by putting and removing the Lua comment signs --) <br />
<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A6][A6]<br />
\setupinteraction[state=start]<br />
\def\MyGoTo[#1][#2]{\goto{#1}[#2]}<br />
\define[2]\YourGoTo{\goto{#1}[#2]}<br />
\starttext<br />
<br />
\startchapter[title={Ward},reference={ch:ward}]<br />
<br />
\startluacode<br />
context.index("Knuth")<br />
-- context.index("Ward")<br />
context["index"]("Ward")<br />
context("Read Knuth and see also the ")<br />
local s,t = "Index","ref:index"<br />
-- context.MyGoTo({s},{t})<br />
context["goto"]("Index",{"ref:index"})<br />
-- context["goto"](s,{t})<br />
-- context.YourGoTo("Index","ref:index")<br />
-- context.YourGoTo(s,t)<br />
-- context.goto("Index",{"ref:index"}) -- this does not work because goto is a reserved word in Lua<br />
\stopluacode<br />
<br />
\input ward.tex<br />
<br />
\stopchapter<br />
<br />
\starttitle[title={Index}]<br />
\startluacode<br />
context.pagereference({"ref:index"})<br />
\stopluacode<br />
\placeindex<br />
\stoptitle<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=CLD_passing_variables&diff=23751CLD passing variables2018-02-14T12:20:33Z<p>Otared: </p>
<hr />
<div>CLD (Context Lua Document) is the environment which lets you interact with TeX from inside Lua code. For more informations on this see the manual cld-mkiv.pdf, which is in <br />
[ConTeXt-StandAlone]/tex/texmf-context/doc/context/documents/general/manuals/cld-mkiv.pdf<br />
inside your ConTeXt directory.<br />
<br />
Now if one needs to pass arguments from Lua to a macro command defined in ConTeXt, one has several possibilities.<br />
<br />
First, if {{code|\mycommand}} is defined on the TeX end and takes one parameter, as in {{code|\mycommand{Myvariable}}}, then in Lua one can say {{code|context.mycommand("Myvariable")}}.<br />
<br />
The same applies to all the commands which take one or more parameters, provided that in their definition no specific delimiters is used. If {{code|\mycommand}} takes two arguments, then saying {{code|context.mycommand("Myfirstargument","Mysecondargument")}} in the Lua end, results in {{code|\mycommand{Myfirstargument}{Mysecondargument}}} on the TeX end.<br />
<br />
Next, for commands which use specific delimiters, such as the {{code|\MyGoTo}} command defined below, then one has to use the construction {{code|context.MyGoTo({"Myfirstargument"},{"Mysecondargument"})}}.<br />
<br />
Finally another construction (which works also for the above cases) which is there more specifically for some Lua reserved words (such as {{code|goto, end, if}}, present also as TeX commands) is to use the construction {{code|context["goto"]("some argument",{"ref:somewhere"})}} in order to obtain {{code|\goto{some argument}[ref:somewhere]}}.<br />
<br />
All this being said, it is better to give some examples below (try the code by putting and removing the Lua comment signs --) <br />
<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A6][A6]<br />
\setupinteraction[state=start]<br />
\def\MyGoTo[#1][#2]{\goto{#1}[#2]}<br />
\define[2]\YourGoTo{\goto{#1}[#2]}<br />
\starttext<br />
<br />
\startchapter[title={Ward},reference={ch:ward}]<br />
<br />
\startluacode<br />
context.index("Knuth")<br />
-- context.index("Ward")<br />
context["index"]("Ward")<br />
context("Read Knuth and see also the ")<br />
local s,t = "Index","ref:index"<br />
-- context.MyGoTo({s},{t})<br />
context["goto"]("Index",{"ref:index"})<br />
-- context["goto"](s,{t})<br />
-- context.YourGoTo("Index","ref:index")<br />
-- context.YourGoTo(s,t)<br />
-- context.goto("Index",{"ref:index"}) -- this does not work because goto is a rserved word in Lua<br />
\stopluacode<br />
<br />
\input ward.tex<br />
<br />
\stopchapter<br />
<br />
\starttitle[title={Index}]<br />
\startluacode<br />
context.pagereference({"ref:index"})<br />
\stopluacode<br />
\placeindex<br />
\stoptitle<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=CLD_passing_variables&diff=23750CLD passing variables2018-02-14T12:17:03Z<p>Otared: </p>
<hr />
<div>CLD (Context Lua Document) is the environment which lets you interact with TeX from inside Lua code. For more informations on this see the manual cld-mkiv.pdf, which is in <br />
[ConTeXt-StandAlone]/tex/texmf-context/doc/context/documents/general/manuals/cld-mkiv.pdf<br />
inside your ConTeXt directory.<br />
<br />
Now if one needs to pass arguments from Lua to a macro command defined in ConTeXt, one has several possibilities.<br />
<br />
First, if {{code|\mycommand}} is defined on the TeX end and takes one parameter, as in {{code|\mycommand{Myvariable}}}, then in Lua one can say {{code|context.mycommand("Myvariable")}}.<br />
<br />
The same applies to all the commands which take one or more parameters, provided that in their definition no specific delimiters is used. If {{code|\mycommand}} takes two arguments, then saying {{code|context.mycommand("Myfirstargument","Mysecondargument")}} in the Lua end, results in {{code|\mycommand{Myfirstargument}{Mysecondargument}}} on the TeX end.<br />
<br />
Next, for commands which use specific delimiters, such as the {{code|\MyGoTo}} command defined below, then one has to use the construction {{code|context.MyGoTo({"Myfirstargument"},{"Mysecondargument"})}}.<br />
<br />
Finally another construction (which works also for the above cases) which is there more specifically for some Lua reserved words (such as {{code|goto, end, if}}) is to use the construction {{code|context["goto"]("some argument",{"ref:somewhere"})}} in order to obtain {{code|\goto{some argument}[ref:somewhere]}}.<br />
<br />
All this being said, it is better to give some examples below (try the code by putting and removing the Lua comment signs --) <br />
<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A6][A6]<br />
\setupinteraction[state=start]<br />
\def\MyGoTo[#1][#2]{\goto{#1}[#2]}<br />
\define[2]\YourGoTo{\goto{#1}[#2]}<br />
\starttext<br />
<br />
\startchapter[title={Ward},reference={ch:ward}]<br />
<br />
\startluacode<br />
context.index("Knuth")<br />
-- context.index("Ward")<br />
context["index"]("Ward")<br />
context("Read Knuth and see also the ")<br />
local s,t = "Index","ref:index"<br />
-- context.MyGoTo({s},{t})<br />
context["goto"]("Index",{"ref:index"})<br />
-- context["goto"](s,{t})<br />
-- context.YourGoTo("Index","ref:index")<br />
-- context.YourGoTo(s,t)<br />
-- context.goto("Index",{"ref:index"}) -- this does not work because goto is a rserved word in Lua<br />
\stopluacode<br />
<br />
\input ward.tex<br />
<br />
\stopchapter<br />
<br />
\starttitle[title={Index}]<br />
\startluacode<br />
context.pagereference({"ref:index"})<br />
\stopluacode<br />
\placeindex<br />
\stoptitle<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=CLD_passing_variables&diff=23749CLD passing variables2018-02-14T12:12:17Z<p>Otared: </p>
<hr />
<div>CLD (Context Lua Document) is the environment which lets you interact with TeX from inside Lua code. For more informations on this see the manual cld-mkiv.pdf, which is in <br />
[ConTeXt-StandAlone]/tex/texmf-context/doc/context/documents/general/manuals/cld-mkiv.pdf<br />
inside your ConTeXt directory.<br />
<br />
Now if one needs to pass arguments from Lua to a macro command defined in ConTeXt, one has several possibilities.<br />
<br />
First, if {{code|\mycommand}} is defined on the TeX end and takes one parameter, as in {{code|\mycommand{Myvariable}}}, then in Lua one can say {{code|context.mycommand("Myvariable")}}.<br />
<br />
The same applies to all the commands which take one or more parameters, provided that in their definition no specific delimiters is used. If {{code|\mycommand}} takes two arguments, then saying {{code|context.mycommand("Myfirstargument","Mysecondargument")}} in the Lua end, results in {{code|\mycommand{Myfirstargument}{Mysecondargument}}} on the TeX end.<br />
<br />
Next, for commands which use specific delimiters, such as the {{code|\MyGoTo}} command defined below, then one has to use the construction {{code|context.MyGoTo({"Myfirstargument"},{"Mysecondargument"})}}.<br />
<br />
Finally another construction (which works also for the above cases) which is there more specifically for some Lua reserved words (such as {{code|goto, end, if}}) is to use the construction {{code|context["goto"]("some argument",{"ref:somewhere"})}} in order to obtain {{code|\goto{some argument}[ref:somewhere]}}.<br />
<br />
All this being said, it is better to give some examples below (try the code by putting and removing the Lua comment signs --) <br />
<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A6][A6]<br />
\setupinteraction[state=start]<br />
\def\MyGoTo[#1][#2]{\goto{#1}[#2]}<br />
\define[2]\YourGoTo{\goto{#1}[#2]}<br />
\starttext<br />
<br />
\startchapter[title={Ward},reference={ch:ward}]<br />
<br />
\startluacode<br />
context.index("Knuth")<br />
-- context.index("Ward")<br />
context["index"]("Ward")<br />
context("Read Knuth and see also the ")<br />
local s,t = "Index","ref:index"<br />
-- context.MyGoTo({s},{t})<br />
context["goto"]("Index",{"ref:index"})<br />
-- context.YourGoTo("Index","ref:index")<br />
-- context.goto("Index",{"ref:index"}) -- this does not work because goto is a reserved word in Lua<br />
\stopluacode<br />
<br />
\input ward.tex<br />
<br />
\stopchapter<br />
<br />
\starttitle[title={Index}]<br />
\startluacode<br />
context.pagereference({"ref:index"})<br />
\stopluacode<br />
\placeindex<br />
\stoptitle<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=CLD_passing_variables&diff=23748CLD passing variables2018-02-14T12:04:48Z<p>Otared: </p>
<hr />
<div>CLD (Context Lua Document) is the environment which lets you interact with TeX from inside Lua code. For more informations on this see the manual cld-mkiv.pdf, which is in <br />
[ConTeXt-StandAlone]/tex/texmf-context/doc/context/documents/general/manuals/cld-mkiv.pdf<br />
inside your ConTeXt directory.<br />
<br />
Now if one needs to pass arguments from Lua to a macro command defined in ConTeXt, one has several possibilities.<br />
<br />
First, if {{code|\mycommand}} is defined on the TeX end and takes one parameter, as in {{code|\mycommand{Myvariable}}}, then in Lua one can say {{code|context.mycommand("Myvariable")}}.<br />
<br />
The same applies to all the commands which take one or more parameters, provided that in their definition no specific delimiters is used. If {{code|\mycommand}} takes two arguments, then saying {{code|context.mycommand("Myfirstargument","Mysecondargument")}} in the Lua end, results in {{code|\mycommand{Myfirstargument}{Mysecondargument}}} on the TeX end.<br />
<br />
Next, for commands which use specific delimiters, such as the {{code|\MyGoTo}} command defined below, then one has to use the construction {{code|context.MyGoTo({"Myfirstargument"},{"Mysecondargument"})}}.<br />
<br />
Finally another construction (which works also for the above cases) which is there more specifically for some Lua reserved words (such as {{code|goto, end, if}}) is to use the construction {{code|context["goto"]("some argument",{"ref:somewhere"})}} in order to obtain {{code|\goto{some argument}[ref:somewhere]}}.<br />
<br />
All this being said, it is better to give some examples below (try the by removing the Lua comment signs --) <br />
<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A6][A6]<br />
\setupinteraction[state=start]<br />
\def\MyGoTo[#1][#2]{\goto{#1}[#2]}<br />
\define[2]\YourGoTo{\goto{#1}[#2]}<br />
\starttext<br />
<br />
\startchapter[title={Ward},reference={ch:ward}]<br />
<br />
\startluacode<br />
context.index("Knuth")<br />
-- context.index("Ward")<br />
context["index"]("Ward")<br />
context("Read Knuth and see also the ")<br />
local s,t = "Index","ref:index"<br />
-- context.MyGoTo({s},{t})<br />
context["goto"]("Index",{"ref:index"})<br />
-- context.YourGoTo("Index","ref:index")<br />
-- context.goto("Index",{"ref:index"}) -- this does not work because goto is a reserved word in Lua<br />
\stopluacode<br />
<br />
\input ward.tex<br />
<br />
\stopchapter<br />
<br />
\starttitle[title={Index}]<br />
\startluacode<br />
context.pagereference({"ref:index"})<br />
\stopluacode<br />
\placeindex<br />
\stoptitle<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=CLD_passing_variables&diff=23745CLD passing variables2018-02-14T08:04:42Z<p>Otared: Created page with "CLD (Context Lua Document) is the environment which lets you interact with TeX from inside Lua code. For more informations on this see the manual cld-mkiv.pdf, which is in [ConT..."</p>
<hr />
<div>CLD (Context Lua Document) is the environment which lets you interact with TeX from inside Lua code. For more informations on this see the manual cld-mkiv.pdf, which is in <br />
[ConTeXt-StandAlone]/tex/texmf-context/doc/context/documents/general/manuals/cld-mkiv.pdf<br />
inside your ConTeXt directory.<br />
<br />
Now if one needs to pass arguments from Lua to a macro command defined in ConTeXt, one has several possibilities.<br />
<br />
First, if {{code|\mycommand}} is defined on the TeX end and takes one parameter, as in {{code|\mycommand{Myvariable}}}, then in Lua one can say {{code|context.mycommand("Myvariable")}}.<br />
<br />
The same applies to all the commands which take one or more parameters, provided that in their definition no specific delimiters is used. If {{code|\mycommand}} takes two arguments, then saying {{code|context.mycommand("Myfirstargument","Mysecondargument")}} in the Lua end, results in {{code|\mycommand{Myfirstargument}{Mysecondargument}}} on the TeX end.<br />
<br />
Next, for commands which use specific delimiters, such as the {{code|\MyGoTo}} command defined below, then one has to use the construction {{code|context.MyGoTo({"Myfirstargument"},{"Mysecondargument"})}}.<br />
<br />
Finally another construction (which works also for the above cases) which is there more specifically for some Lua reserved words (such as {{code|goto, end, if}}) is to use the construction {{code|context["goto"]("some argument",{"ref:somewhere"})}} in order to obtain {{code|\goto{some argument}[ref:somewhere]}}.<br />
<br />
All this being said, it is better to give some examples below<br />
<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A6][A6]<br />
\setupinteraction[state=start]<br />
\def\MyGoTo[#1][#2]{\goto{#1}[#2]}<br />
\define[2]\YourGoTo{\goto{#1}[#2]}<br />
\starttext<br />
<br />
\startchapter[title={Ward},reference={ch:ward}]<br />
<br />
\startluacode<br />
context.index("Knuth")<br />
-- context.index("Ward")<br />
context["index"]("Ward")<br />
context("Read Knuth and see also the ")<br />
local s,t = "Index","ref:index"<br />
-- context.MyGoTo({s},{t})<br />
context["goto"]("Index",{"ref:index"})<br />
-- context.YourGoTo("Index","ref:index")<br />
-- context.goto("Index",{"ref:index"}) -- this does not work because goto is a reserved word in Lua<br />
\stopluacode<br />
<br />
\input ward.tex<br />
<br />
\stopchapter<br />
<br />
\starttitle[title={Index}]<br />
\startluacode<br />
local s,t = "ref:","index"<br />
context.pagereference({"ref:index"})<br />
\stopluacode<br />
\placeindex<br />
\stoptitle<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=Interaction_and_button&diff=23723Interaction and button2018-01-27T11:43:08Z<p>Otared: </p>
<hr />
<div>In an interactive document it is useful to be able to put buttons which do specific navigation tasks. The {{cmd|button}} does precisely this, and it can be included in a layer which can be put anywhere on a page. Here is an example:<br />
<br />
<context source=yes mode=mkiv text="This results in the following:"><br />
\setuppapersize[A7][A7]<br />
\setupinteraction[state=start]<br />
\setupbodyfont[10pt]<br />
<br />
\definelayer[MyButton]<br />
[width=\paperwidth,height=\paperheight]<br />
<br />
\setlayerframed[MyButton]<br />
[x=30mm,y=40mm,<br />
location={lo,hi}]<br />
[width=50mm,height=30mm,<br />
%offset=-1mm,<br />
frame=on]<br />
{\button[frame=off]{\scale[height=2em]{\symbol[navigation 1][nextpage]}}[NextPage] \endgraf<br />
We have a nice button here}<br />
<br />
\setupbackgrounds[page]<br />
[background={MyButton}]<br />
\starttext<br />
We have here a page with a layer containing a button.<br />
\page<br />
This is a second page, and here is a button to go back <br />
\button{\symbol[navigation 1][previouspage]}[PreviousPage].<br />
\stoptext<br />
</context><br />
<br />
Indeed, instead of {{cmd|symbol}} one can use any other character, or even a figure with the command {{cmd|externalfigure}}.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Interaction_and_button&diff=23722Interaction and button2018-01-27T11:41:31Z<p>Otared: </p>
<hr />
<div>In an interactive document it is useful to be able to put buttons which do specific navigation tasks. The {{cmd|button}} does precisely this, and it can be included in a layer which can be put anywhere on a page. Here is an example:<br />
<context source=yes mode=mkiv text="This results in the following:"><br />
\setuppapersize[A7][A7]<br />
\setupinteraction[state=start]<br />
\setupbodyfont[10pt]<br />
<br />
\definelayer[MyButton]<br />
[width=\paperwidth,height=\paperheight]<br />
<br />
\setlayerframed[MyButton]<br />
[x=30mm,y=40mm,<br />
location={lo,hi}]<br />
[width=50mm,height=30mm,<br />
%offset=-1mm,<br />
frame=on]<br />
{\button[frame=off]{\scale[height=2em]{\symbol[navigation 1][nextpage]}}[NextPage] \endgraf<br />
We have a nice button here}<br />
<br />
\setupbackgrounds[page]<br />
[background={MyButton}]<br />
\starttext<br />
We have here a page with a layer containing a button.<br />
\page<br />
This is a second page, and here is a button to go back <br />
\button{\symbol[navigation 1][previouspage]}[PreviousPage].<br />
\stoptext<br />
</context><br />
Indeed, instead of {{cmd|symbol}} one can use any other character, or even a figure with the command {{cmd|externalfigure}}.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Interaction_and_button&diff=23721Interaction and button2018-01-27T10:58:05Z<p>Otared: Created page with "In an interactive document it is useful to be able to put buttons which do specific navigation tasks. The {{cmd|button}} does precisely this, and it can be included in a layer wh..."</p>
<hr />
<div>In an interactive document it is useful to be able to put buttons which do specific navigation tasks. The {{cmd|button}} does precisely this, and it can be included in a layer which can be put anywhere on a page. Here is an example:<br />
<context source=yes mode=mkiv text="This results in the following:"><br />
\setuppapersize[A5][A5]<br />
\setupinteraction[state=start]<br />
\setupbodyfont[10pt]<br />
<br />
\definelayer[MyButton]<br />
[width=\paperwidth,height=\paperheight]<br />
<br />
\setlayerframed[MyButton]<br />
[x=50mm,y=70mm,<br />
location={lo,hi}]<br />
[width=50mm,height=30mm,<br />
%offset=-1mm,<br />
frame=on]<br />
{\button[frame=off]{\scale[height=2em]{\symbol[navigation 1][nextpage]}}[NextPage] \endgraf<br />
We have a nice button here}<br />
<br />
\setupbackgrounds[page]<br />
[background={MyButton}]<br />
\starttext<br />
We have here a page with a layer containing a button.<br />
\page<br />
This is a second page, and here is a button to go back <br />
\button{\symbol[navigation 1][previouspage]}[PreviousPage].<br />
\stoptext<br />
</context><br />
Indeed, instead of {{cmd|symbol}} one can use any other character, or even a figure with the command {{cmd|externalfigure}}.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Command/setupregister&diff=23719Command/setupregister2018-01-23T16:49:33Z<p>Otared: </p>
<hr />
<div>{{Reference<br />
|name=setupregister<br />
|attributes=<br />
}}<br />
<br />
== [[Help:Reference|Syntax]] ==<br />
<table cellspacing="4" cellpadding="2" class="cmd"><br />
<tr><br />
<td colspan="2" class="cmd">\setupregister<span class="first" >[...]</span><span class="second" style="color:red;">[...]</span><span class="third" >[...,...=...,...]</span></td><br />
</tr><br />
<tr valign="top" class="first"><br />
<td class="cmd">[...]</td><br />
<td>singular </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[...]</td><br />
<td>name </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:n|n]]</td><br />
<td>[[Command/value:number|number]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:balance|balance]]</td><br />
<td>[[Command/value:yes|yes]] [[Command/value:no|no]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:align|align]]</td><br />
<td>[[Command/value:yes|yes]] [[Command/value:no|no]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:style|style]]</td><br />
<td>[[Command/value:normal|normal]] [[Command/value:bold|bold]] [[Command/value:slanted|slanted]] [[Command/value:boldslanted|boldslanted]] [[Command/value:type|type]] [[Command/value:cap|cap]] [[Command/value:small...|small...]] [[Command/value:command|command]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:pagestyle|pagestyle]]</td><br />
<td>[[Command/value:normal|normal]] [[Command/value:bold|bold]] [[Command/value:slanted|slanted]] [[Command/value:boldslanted|boldslanted]] [[Command/value:type|type]] [[Command/value:cap|cap]] [[Command/value:small...|small...]] [[Command/value:command|command]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:textstyle|textstyle]]</td><br />
<td>[[Command/value:normal|normal]] [[Command/value:bold|bold]] [[Command/value:slanted|slanted]] [[Command/value:boldslanted|boldslanted]] [[Command/value:type|type]] [[Command/value:cap|cap]] [[Command/value:small...|small...]] [[Command/value:command|command]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:indicator|indicator]]</td><br />
<td>[[Command/value:yes|yes]] [[Command/value:no|no]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:coupling|coupling]]</td><br />
<td>[[Command/value:yes|yes]] [[Command/value:no|no]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:sectionnumber|sectionnumber]]</td><br />
<td>[[Command/value:yes|yes]] [[Command/value:no|no]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:criterium|criterium]]</td><br />
<td>[[Command/value:section|section]] [[Command/value:local|local]] [[Command/value:all|all]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:distance|distance]]</td><br />
<td>[[Command/value:dimension|dimension]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:symbol|symbol]]</td><br />
<td>1 2 ... [[Command/value:n|n]] [[Command/value:a|a]] ... [[Command/value:none|none]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:interaction|interaction]]</td><br />
<td>[[Command/value:pagenumber|pagenumber]] [[Command/value:text|text]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:expansion|expansion]]</td><br />
<td>[[Command/value:yes|yes]] [[Command/value:no|no]] [[Command/value:command|command]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:referencing|referencing]]</td><br />
<td>[[Command/value:on|on]] [[Command/value:off|off]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:location|location]]</td><br />
<td>[[Command/value:left|left]] [[Command/value:middle|middle]] [[Command/value:right|right]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:maxwidth|maxwidth]]</td><br />
<td>[[Command/value:dimension|dimension]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:unknownreference|unknownreference]]</td><br />
<td>[[Command/value:empty|empty]] [[Command/value:none|none]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:alternative|alternative]]</td><br />
<td>[[Command/value:a|a]] [[Command/value:b|b]] [[Command/value:A|A]] [[Command/value:B|B]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:prefix|prefix]]</td><br />
<td>[[Command/value:both|both]] [[Command/value:first|first]] [[Command/value:none|none]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:pagecommand|pagecommand]]</td><br />
<td>[[Command/value:command|command]]</td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:textcommand|textcommand]]</td><br />
<td>[[Command/value:command|command]]</td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:compress|compress]]</td><br />
<td>[[Command/value:yes|yes]] [[Command/value:no|no]]</td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:method|method]]</td><br />
<td>[[Command/value:default|default]] [[Command/value:before|before]] [[Command/value:after|after]] [[Command/value:first|first]] [[Command/value:last|last]]</td><br />
</tr><br />
</table><br />
<br />
== Description == <br />
<br />
The options are as follows:<br />
<br />
<table cellspacing="4" cellpadding="2"><br />
<tr><br />
<th>Option</th><br />
<th>Description</th><br />
</tr><br />
<tr><br />
<td>n</td><br />
<td>number of columns</td><br />
</tr><br />
<tr><br />
<td>balance</td><br />
<td>balance columns?</td><br />
</tr><br />
<tr><br />
<td>align</td><br />
<td>balance related (it’s difficult; try which combination suits you)</td><br />
</tr><br />
<tr><br />
<td>style</td><br />
<td>overall style (no effect, at least in MkIV)</td><br />
</tr><br />
<tr><br />
<td>pagestyle</td><br />
<td>style of page number</td><br />
</tr><br />
<tr><br />
<td>textstyle</td><br />
<td>style of index word</td><br />
</tr><br />
<tr><br />
<td>indicator</td><br />
<td>create letter sections?</td><br />
</tr><br />
<tr><br />
<td>coupling</td><br />
<td>special feature for screen documents (no effect in MkIV?)</td><br />
</tr><br />
<tr><br />
<td>(section)number</td><br />
<td>print part/... numbers?</td><br />
</tr><br />
<tr><br />
<td>criterium</td><br />
<td>for part/chapter/section registers</td><br />
</tr><br />
<tr><br />
<td>distance</td><br />
<td>between text and pagenumber</td><br />
</tr><br />
<tr><br />
<td>symbol</td><br />
<td>for pagenumber (a=a,b,c; n=1,2,3; 1=dots; 2=blocks)</td><br />
</tr><br />
<tr><br />
<td>interaction</td><br />
<td>which part should be clickable?</td><br />
</tr><br />
<tr><br />
<td>expansion</td><br />
<td>When the entry is a command, it is expanded before writing to the register</td><br />
</tr><br />
<tr><br />
<td>referencing</td><br />
<td>create references for letter sections (like "index:a")?</td><br />
</tr><br />
<tr><br />
<td>textcommand</td><br />
<td>custom command for index word</td><br />
</tr><br />
<tr><br />
<td>pagecommand</td><br />
<td>custom command for page number</td><br />
</tr><br />
<tr><br />
<td>location</td><br />
<td>?</td><br />
</tr><br />
<tr><br />
<td>compress</td><br />
<td>collapse page numbers</td><br />
</tr><br />
<tr><br />
<td>maxwidth</td><br />
<td>Set the maximum length for the text of the index entry in the registerlist (the command uses {{cmd|limitatetext}} to do this)</td><br />
</tr><br />
<tr><br />
<td>unknownreference</td><br />
<td>insert a horizontal space before and after the reference in the text if you use interactive mode to move from one entry in the text to next or previous one and there is no previous or next reference.</td><br />
</tr><br />
<tr><br />
<td>alternative</td><br />
<td>?</td><br />
</tr><br />
<tr><br />
<td>method</td><br />
<td>sets up the sorter used for {{cmd|placeregister}}</td><br />
</tr><br />
</table><br />
<br />
Some options don’t seem to have any effect, at least in MkIV.<br />
<br />
== Example ==<br />
<br />
Here is an example for MkII:<br />
<br />
<context source="yes" text="yields"><br />
\defineregister[nickname][nicknames]<br />
<br />
\setupregister [nickname]<br />
[partnumber=no,alternative=A,criterium=all,n=3,balance=yes,pagestyle=normal]<br />
\setupregister [nickname][foto][pagestyle=bold]<br />
<br />
\starttext<br />
<br />
Text about Mamut\nickname{Mamut}.<br />
\placefigure[]<br />
{Kuba\nickname[foto::]{Kuba} on the photo.}<br />
{\externalfigure[kuba][width=0.2\textwidth]}<br />
<br />
\page<br />
And text about Kuba\nickname{Kuba}.<br />
<br />
\completenickname<br />
\stoptext <br />
<br />
</context><br />
<br />
<br />
I found it safer to put \register command after punctuation e.g.: Mamut.\nickname{Mamut} [P.K.]<br />
<br />
For the MkIV version of setting the pagestyle, and more, see the material on {{cmd|defineprocessor}} that is on [[Registers#Prcoessors|this page]].<br />
<br />
== See also ==<br />
<!-- something like {{cmd|goto}} --><br />
<br />
== Help from ConTeXt-Mailinglist/Forum ==<br />
All issues with:<br />
{{Forum|{{SUBPAGENAME}}}}<br />
<br />
[[Category:Command/Registers|setupregister]]</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Command/setupheader&diff=23474Command/setupheader2017-09-04T16:00:12Z<p>Otared: </p>
<hr />
<div>{{Reference<br />
|name=setupheader<br />
|attributes=<br />
}}<br />
<br />
== [[Help:Reference|Syntax]] ==<br />
<table cellspacing="4" cellpadding="2" class="cmd"><br />
<tr><br />
<td colspan="2" class="cmd">\setupheader<span class="first" style="color:red;">[...]</span><span class="second" >[...,...=...,...]</span></td><br />
</tr><br />
<tr valign="top" class="first"><br />
<td class="cmd">[...]</td><br />
<td><strong>text</strong> margin edge </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:state|state]]</td><br />
<td>[[Command/value:normal|normal]] [[Command/value:stop|stop]] [[Command/value:start|start]] [[Command/value:empty|empty]] [[Command/value:high|high]] [[Command/value:none|none]] [[Command/value:nomarking|nomarking]] [[Command/value:name|name]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:strut|strut]]</td><br />
<td>[[Command/value:yes|yes]] [[Command/value:no|no]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:style|style]]</td><br />
<td>[[Command/value:normal|normal]] [[Command/value:bold|bold]] [[Command/value:slanted|slanted]] [[Command/value:boldslanted|boldslanted]] [[Command/value:type|type]] [[Command/value:cap|cap]] [[Command/value:small...|small...]] [[Command/value:command|command]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:leftstyle|leftstyle]]</td><br />
<td>[[Command/value:normal|normal]] [[Command/value:bold|bold]] [[Command/value:slanted|slanted]] [[Command/value:boldslanted|boldslanted]] [[Command/value:type|type]] [[Command/value:cap|cap]] [[Command/value:small...|small...]] [[Command/value:command|command]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:rightstyle|rightstyle]]</td><br />
<td>[[Command/value:normal|normal]] [[Command/value:bold|bold]] [[Command/value:slanted|slanted]] [[Command/value:boldslanted|boldslanted]] [[Command/value:type|type]] [[Command/value:cap|cap]] [[Command/value:small...|small...]] [[Command/value:command|command]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:leftwidth|leftwidth]]</td><br />
<td>[[Command/value:dimension|dimension]]<br /><!--<br />
-->Limit the text inside the header to a certain dimension. <!--<br />
-->Setting this value may interfere with any additional {{cmd|delimitatetext}} or {{cmd|doboundtext}} performed inside the headers<br />
</td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:rightwidth|rightwidth]]</td><br />
<td>[[Command/value:dimension|dimension]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:before|before]]</td><br />
<td>[[Command/value:command|command]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:after|after]]</td><br />
<td>[[Command/value:command|command]] </td><br />
</tr><br />
</table><br />
<br />
<br />
== Description == <br />
The first argument is assumed to be "text" when it is left out. Text sets the position of the header and/or footer directly over or under the pagebody. Margin places the content in the margin of the document.<br />
<br />
The different <tt>state</tt>s and their effects:<br />
{|<br />
|<tt>start</tt>, <tt>stop</tt><br />
|switch headings on and off<br />
|-<br />
|<tt>none</tt><br />
|switch off for whole chapter<br />
|-<br />
|<tt>empty</tt><br />
|no heading on the first chapter page<br />
|-<br />
|<tt>high</tt><br />
|no heading on the first chapter page + expand textarea about space occupied by heading.<br />
|-<br />
|<tt>text</tt><br />
|some 'text' defined by<br />
<texcode><br />
\definetext[text][footer][pagenumber]<br />
</texcode><br />
or<br />
<texcode><br />
\definetext[text][footer][left][right]<br />
</texcode><br />
which is valid on the first chapter page<br />
|}<br />
<br />
Note that when setting the state, before or after each applies to all parts of the page and it is therefore best to set these by themselves as the first example below shows. For this reason state=high also affects the footer in that it removes the footer leaving no whitespace.<br />
<br />
== Examples ==<br />
<texcode>\setupheader[state=empty]</texcode><br />
<texcode>\setupheader[strut=yes,style=bold]</texcode><br />
<texcode>\setupheader[text][after=\vskip 4pt \hrule]</texcode><br />
<br />
=== Header with thin line below ===<br />
<br />
This is how to define header with thin line below, which does not appear on empty/blank page:<br />
<br />
<context mode=mkiv source=yes><br />
\setupheader<br />
[text]<br />
[before={\startframed[frame=off,bottomframe=on,framecolor=blue,]},<br />
after={\stopframed},<br />
]<br />
\setupheadertexts[Text left][Text right]<br />
<br />
\starttext<br />
Page 1<br />
\page[empty,blank]<br />
Page 2<br />
\stoptext<br />
</context><br />
<br />
== See also ==<br />
<!-- something like {{cmd|goto}} --><br />
<br />
{{cmd|setupheadertexts}}<br />
{{cmd|setupfooter}}<br />
{{cmd|setupfootertexts}}<br />
<br />
== Help from ConTeXt-Mailinglist/Forum ==<br />
All issues with:<br />
{{Forum|{{SUBPAGENAME}}}}<br />
<br />
[[Category:Commands|setupheader]]</div>Otaredhttps://wiki.contextgarden.net/index.php?title=symbolset&diff=23469symbolset2017-09-01T07:28:34Z<p>Otared: </p>
<hr />
<div>One can create a symbol set with a specific font by using the commands {{code|1=\startsymbolset}} --- {{code|1=\stopsymbolset}}.<br />
Here is an example which creates the « dangerous bend » used by Donald Kunth with his font {{code|1=manfnt}}.<br />
<br />
Once a {{code|1=symbolset}} has been declared and set up, one can use the declared symbols (for instance here {{code|1=dbend}}) with the command {{code|1=\symbol{dbend}}}, or imbed it in other commands.<br />
<br />
In order to find which slot represents a desired glyph, one may look at the afm file and then by trial and error find the correct address… (in our example the file is {{code|1=manfnt.afm}}). For instance we have found that {{code|1=n:char_7e}} represents the dangerous bend, while {{code|1=n:char_79}} represents a broken arrow.<br />
<br />
<texcode><br />
% First we declare the font where the symbols have to be found<br />
\definefontsynonym[bends][file:manfnt.afm]<br />
<br />
% Then we declare a proper set of symbols:<br />
\startsymbolset [Dangerous Bends]<br />
\definesymbol [dbend] [\resolvedglyphdirect{bends}{n:char_7e}]<br />
\definesymbol [lhdbend] [\resolvedglyphdirect{bends}{n:char_7f}]<br />
\definesymbol[brokenarrow][\resolvedglyphdirect{bends}{n:char_79}]<br />
\definesymbol[pentastar][\resolvedglyphdirect{bends}{n:char_1e}]<br />
\stopsymbolset<br />
<br />
% Here we say which symbolset is to be used:<br />
\setupsymbolset [Dangerous Bends]<br />
<br />
% One can use the symbols directly with the command \symbol<br />
% or imbed it in other commands.<br />
\define\dbend{\inmargin{\symbol{dbend}}}<br />
\define\ddbend{\inmargin{\symbol{dbend}\symbol{dbend}}}<br />
\define\dddbend{\inmargin{\symbol{dbend}\symbol{dbend}\symbol{dbend}}}<br />
<br />
\starttext<br />
Here we use the symbols directly: \symbol{dbend}\quad \symbol{lhdbend}.<br />
\blank[big]<br />
\symbol{brokenarrow}\quad \symbol{pentastar}.<br />
\blank[big]<br />
\dbend This result is difficult to prove.<br />
\blank[big]<br />
\ddbend This result is more difficult to prove.<br />
\blank[big]<br />
\dddbend This result is even more difficult to prove.<br />
\stoptext<br />
<br />
</texcode></div>Otaredhttps://wiki.contextgarden.net/index.php?title=symbolset&diff=23468symbolset2017-08-31T19:41:59Z<p>Otared: Created page with "One can create a symbol set with a specific font by using the commands {{code|1=\startsymbolset}} --- {{code|1=\stopsymbolset}}. Here is an example which creates the « dangerous..."</p>
<hr />
<div>One can create a symbol set with a specific font by using the commands {{code|1=\startsymbolset}} --- {{code|1=\stopsymbolset}}.<br />
Here is an example which creates the « dangerous bend » used by Donald Kunth with his font {{code|1=manfnt}}.<br />
<br />
Once a {{code|1=symbolset}} has been declared and set up, one can use the declared symbols (for instance here {{code|1=dbend}}) with the command {{code|1=\symbol{dbend}}}, or imbed it in other commands.<br />
<br />
<texcode><br />
% First we declare the font where the symbols have to be found<br />
\definefontsynonym[bends][file:manfnt.afm]<br />
<br />
% Then we declare a proper set of symbols:<br />
\startsymbolset [Dangerous Bends]<br />
\definesymbol [dbend] [\resolvedglyphdirect{bends}{n:char_7e}]<br />
\definesymbol [lhdbend] [\resolvedglyphdirect{bends}{n:char_7f}]<br />
\stopsymbolset<br />
<br />
% Here we say which symbolset is to be used:<br />
\setupsymbolset [Dangerous Bends]<br />
<br />
% One can use the symbols directly with the command \symbol<br />
% or imbed it in other commands.<br />
\define\dbend{\inmargin{\symbol{dbend}}}<br />
\define\ddbend{\inmargin{\symbol{dbend}\symbol{dbend}}}<br />
\define\dddbend{\inmargin{\symbol{dbend}\symbol{dbend}\symbol{dbend}}}<br />
<br />
\starttext<br />
Here we use the symbols directly: \symbol{dbend}\quad \symbol{lhdbend}.<br />
\blank[big]<br />
\dbend This result is difficult to prove.<br />
\blank[big]<br />
\ddbend This result is more difficult to prove.<br />
\blank[big]<br />
\dddbend This result is even more difficult to prove.<br />
\stoptext<br />
<br />
</texcode></div>Otaredhttps://wiki.contextgarden.net/index.php?title=mathstackers&diff=23467mathstackers2017-08-30T13:58:56Z<p>Otared: </p>
<hr />
<div>== mathstackers ==<br />
<br />
ConTeXt has a nice structure called {{code|1=mathstackers}} in order to define new math commands in which some characters are stacked over another one. For more information refer to {{src|math-stc.mkiv}}.<br />
<br />
The two command {{code|1=\mathover[symbol]{}{}}} and {{code|\mathunder[symbol]{}{}}} are built and can be used directly. However one may define one's own commands.<br />
<br />
A typical example is when you want to put a small circle on top of a letter (as one would when defining the topological interior of a set…). <br />
To do so we define first a new stacker named here {{code|1=MySymbol}}, and then invoking that with {{code|1=\definemathover}} we define two new commands {{code|1=\interior}} and {{code|1=\mystarred}} which can be used in math mode.<br />
<br />
<texcode><br />
\definemathstackers[MySymbol]<br />
[voffset=-.3\mathexheight,<br />
hoffset=\zeropoint,<br />
mathclass=ord,<br />
topoffset=.4\mathemwidth, % poor man's italic correction<br />
middlecommand=\mathematics]<br />
<br />
\definemathover[MySymbol][interior]["2218] %this a small circle<br />
\definemathover[MySymbol][mystarred]["2717] % this is an asterisk<br />
\starttext<br />
<br />
Here is the new command \type{\interior{K}}: $\interior{K}$. Compare this to \type{\mathring{K}}: $\mathring{K}$.<br />
<br />
Here is the new command \type{\mystarred{K}}: $\mystarred{K}$.<br />
<br />
Here are the built-in commands \type{\mathover} and \type{\mathunder}: <br />
<br />
$\mathover[symbol]{"2217}{A}$, % 2217 is an asterisk<br />
$\mathover[symbol]{"2218}{A}$, % 2218 is a circle<br />
$\mathover[symbol]{"2219}{A}$, % 2219 is a bullet<br />
$\mathunder[symbol]{"2217}{A}$,<br />
$\mathunder[symbol]{"2218}{A}$,<br />
$\mathunder[symbol]{"2219}{A}$.<br />
<br />
<br />
\stoptext<br />
</texcode><br />
When typeset in {{code|1=mkiv}} one can see that the command {{code|1=\interior{K}}} shows the letter K with a small circle above it.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=mathstackers&diff=23466mathstackers2017-08-30T13:34:28Z<p>Otared: </p>
<hr />
<div>== mathstackers ==<br />
<br />
ConTeXt has a nice structure called {{code|1=mathstackers}} in order to define new math commands in which some characters are stacked over another one. For more information refer to {{src|math-stc.mkiv}}.<br />
<br />
The two command {{code|1=\mathover[symbol]{}{}}} and {{code|\mathunder[symbol]{}{}}} are built and can be used directly. However one may define one's own commands.<br />
<br />
A typical example is when you want to put a small circle on top of a letter (as one would when defining the topological interior of a set…). <br />
To do so we define first a new stacker named here {{code|1=MySymbol}}, and then invoking that with {{code|1=\definemathover}} we define two new commands {{code|1=\interior}} and {{code|1=\mystarred}} which can be used in math mode.<br />
<br />
<texcode><br />
\definemathstackers[MySymbol]<br />
[voffset=-.3\mathexheight,<br />
hoffset=\zeropoint,<br />
mathclass=ord,<br />
topoffset=.4\mathemwidth, % poor man's italic correction<br />
middlecommand=\mathematics]<br />
<br />
\definemathover[MySymbol][interior]["2218] %this a small circle<br />
\definemathover[MySymbol][mystarred]["2717] % this is an asterisk<br />
\starttext<br />
<br />
Here is the new command \type{\interior{K}}: $\interior{K}$. Compare this to \type{\mathring{K}}: $\mathring{K}$.<br />
<br />
Here is the new command \type{\mystarred{K}}: $\mystarred{K}$.<br />
<br />
Here are the built-in commands \type{\mathover} and \type{\mathunder}: <br />
<br />
$\mathover[symbol]{"2217}{A}$, % 2217 is a circle<br />
$\mathover[symbol]{"2218}{A}$, % 2218 is an asterisk<br />
$\mathover[symbol]{"2219}{A}$, % 2219 is a bullet<br />
$\mathunder[symbol]{"2217}{A}$,<br />
$\mathunder[symbol]{"2218}{A}$,<br />
$\mathunder[symbol]{"2219}{A}$.<br />
<br />
<br />
\stoptext<br />
</texcode><br />
When typeset in {{code|1=mkiv}} one can see that the command {{code|1=\interior{K}}} shows the letter K with a small circle above it.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=mathstackers&diff=23465mathstackers2017-08-30T13:01:54Z<p>Otared: Created page with "== mathstackers == ConTeXt has a nice structure in order to define new math commands in which some characters are stacked over another one. A typical example is when you want to..."</p>
<hr />
<div>== mathstackers ==<br />
<br />
ConTeXt has a nice structure in order to define new math commands in which some characters are stacked over another one.<br />
A typical example is when you want to put a small circle on top of a letter (as one would when defining the topological interior of a set…). <br />
To do so we define first a new stacker named here {{code|l=MySymbol}}, and then invoking that we define two new commands {{code|l=\interior}} and {{code|l=\starred}}<br />
<br />
\definemathstackers[MySymbol]<br />
[voffset=-.3\mathexheight,<br />
hoffset=\zeropoint,<br />
mathclass=ord,<br />
topoffset=.4\mathemwidth, % poor man's italic correction<br />
middlecommand=\mathematics]<br />
<br />
%\definemathstackers[symbol][voffset=-.30\mathexheight,hoffset=\zeropoint]<br />
\definemathover[MySymbol][interior]["2218]<br />
\definemathover[MySymbol][starred]["2717]<br />
\starttext<br />
<br />
%Here is the new command \type{\interiorset{K}}: $\mathring{K} \neq \interiorset{K}$<br />
<br />
Here is the new command \type{\interior{K}}: $\interior{K}$. Compare this to \type{\mathring{K}}: $\mathring{K}$.<br />
<br />
Here is the new command \type{\starred{K}}: $\starred{K}$<br />
<br />
\stoptext</div>Otaredhttps://wiki.contextgarden.net/index.php?title=autoinsertnextspace&diff=22960autoinsertnextspace2015-08-27T19:43:29Z<p>Otared: </p>
<hr />
<div>The macro {{cmd|autoinsertnextspace}} conditionally adds a space, depending on the next token. <br />
This is very useful when defining macro commands as short hands for text (or other expressions used within text), since by default TeX ignores spaces after a command macro. Compare the following two definitions.<br />
<br />
<context source=yes><br />
\define\myBla{blablabla\autoinsertnextspace}<br />
\define\myBadBla{blablabla}<br />
<br />
\starttext<br />
<br />
With \type{\myBla}:<br />
<br />
Watch the space after \myBla which makes this sentence meaningful. And watch also the lack of space before the period when we say \myBla.<br />
<br />
With \type{\myBadBla}:<br />
<br />
Watch the lack of space after \myBadBla which makes this sentence difficult to read. <br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=autoinsertnextspace&diff=22959autoinsertnextspace2015-08-27T19:38:51Z<p>Otared: </p>
<hr />
<div>The macro<br />
autoinsertnextspace<br />
The macro {{cmd|autoinsertnextspace}} conditionally adds a space, depending on the next token. <br />
This is very useful when defining macro commands as short hands for text (or other expressions used within text), since by default TeX ignores spaces after a command macro. Compare the following two definitions.<br />
<br />
<context source=yes><br />
\define\myBla{blablabla\autoinsertnextspace}<br />
\define\myBadBla{blablabla}<br />
<br />
\starttext<br />
<br />
With \type{\myBla}:<br />
<br />
Watch the space after \myBla which makes this sentence meaningful. And watch also the lack of space before the period when we say \myBla.<br />
<br />
With \type{\myBadBla}:<br />
<br />
Watch the lack of space after \myBadBla which makes this sentence difficult to read. <br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=autoinsertnextspace&diff=22958autoinsertnextspace2015-08-27T19:35:45Z<p>Otared: Created page with "The macro autoinsertnextspace The macro {{cmd|autoinsertnextspace}} conditionally adds a space, depending on the next token. This is very useful when defining macro commands as ..."</p>
<hr />
<div>The macro<br />
autoinsertnextspace<br />
The macro {{cmd|autoinsertnextspace}} conditionally adds a space, depending on the next token. <br />
This is very useful when defining macro commands as short hands for text (or other expressions used within text), since by default TeX ignores spaces after a command macro. Compare the following two definitions.<br />
<br />
<context><br />
\define\myBla{blablabla\autoinsertnextspace}<br />
\define\myBadBla{blablabla}<br />
<br />
\starttext<br />
<br />
With \type{\myBla}:<br />
<br />
Watch the space after \myBla which makes this sentence meaningful. And watch also the lack of space before the period when we say \myBla.<br />
<br />
With \type{\myBadBla}:<br />
<br />
Watch the lack of space after \myBadBla which makes this sentence difficult to read. <br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=Command/definelabel&diff=22944Command/definelabel2015-08-07T16:50:32Z<p>Otared: </p>
<hr />
<div>{{Reference<br />
|name=definelabel<br />
|attributes=<br />
}}<br />
<br />
== [[Help:Reference|Syntax]] ==<br />
<table cellspacing="4" cellpadding="2" class="cmd"><br />
<tr><br />
<td colspan="2" class="cmd">\definelabel<span class="first" >[...]</span><span class="second" >[...,...=...,...]</span></td><br />
</tr><br />
<tr valign="top" class="first"><br />
<td class="cmd">[...]</td><br />
<td>name </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:text|text]]</td><br />
<td>[[Command/value:text|text]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:location|location]]</td><br />
<td>[[Command/value:inmargin|inmargin]] [[Command/value:intext|intext]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:way|way]]</td><br />
<td>[[Command/value:bytext|bytext]] [[Command/value:bysection|bysection]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:blockway|blockway]]</td><br />
<td>[[Command/value:yes|yes]] [[Command/value:no|no]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:headstyle|headstyle]]</td><br />
<td>[[Command/value:normal|normal]] [[Command/value:bold|bold]] [[Command/value:slanted|slanted]] [[Command/value:boldslanted|boldslanted]] [[Command/value:type|type]] [[Command/value:cap|cap]] [[Command/value:small|small]] ... [[Command/value:command|command]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:headcolor|headcolor]]</td><br />
<td>[[Command/value:name|name]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:before|before]]</td><br />
<td>[[Command/value:command|command]] </td><br />
</tr><br />
<tr valign="top" class="second"><br />
<td class="cmd">[[Command/keyword:after|after]]</td><br />
<td>[[Command/value:command|command]] </td><br />
</tr><br />
</table><br />
<br />
<br />
== Description == <br />
Define an autonumbered label type, including a command to place one.<br />
<br />
== Example ==<br />
<context source=yes><br />
\definelabel[mapA][text=Map A, location=inmargin]<br />
\definelabel[mapB][text=Map B, location=intext]<br />
<br />
\mapA One map<br />
<br />
\mapB\ Another map<br />
</context><br />
<br />
== See also ==<br />
* ''{{cmd|labeling}}'' to use a labeling defined with {{cmd|definelabel}}.<br />
* {{cmd|defineenumeration}} for the modern way of doing things.<br />
<br />
== Help from ConTeXt-Mailinglist/Forum ==<br />
All issues with:<br />
{{Forum|{{SUBPAGENAME}}}}<br />
<br />
[[Category:Command/Numbered objects|definelabel]]<br />
[[Category:Command/Mkii only|definelabel]]</div>Otaredhttps://wiki.contextgarden.net/index.php?title=%5Cshowreferences&diff=22799\showreferences2015-04-27T09:23:47Z<p>Otared: </p>
<hr />
<div>When writing a long document with many cross references, at the stage of proof reading, it comes handy if one could visualize what are the names given to various nodes (in ConTeXt parlance « nodes.destinations ») and also to visualize what destinations are invoked in the text (in ConTeXt they are named « nodes.references »). <br />
<br />
There is a command named <br />
<texcode><br />
\showreferences<br />
</texcode><br />
which just does this: it prints in small fonts, to the left of each such node the name of the destination, or the name of the reference used at that point. Let us show it in action in the following (not so minimal) example (to be typeset with mkiv):<br />
<br />
<texcode><br />
\setupinteraction[state=start]<br />
\setupbodyfont[10pt,palatino]<br />
\useMPlibrary[dum]<br />
<br />
%\usemodule[references-show]<br />
<br />
%\enabletrackers[nodes.references,nodes.destinations]<br />
<br />
\showreferences<br />
<br />
%\enabletrackers[nodes.references.show]<br />
<br />
%\enabletrackers[nodes.references]<br />
<br />
%\enabletrackers[nodes.destinations.show]<br />
<br />
%\enabletrackers[nodes.destinations]<br />
<br />
%\enabletrackers[nodes.references.show=2.5]<br />
%\enabletrackers[nodes.destinations.show=1]<br />
<br />
\definereferenceformat[eqref][left=(,right=)] <br />
\setupformulas[location=right]<br />
<br />
\starttext<br />
<br />
\startchapter[title=Some references,reference=ch:New]<br />
<br />
\startsection[title=Some facts,reference=sec:First]<br />
We have<br />
\placeformula[eq:Square]<br />
\startformula<br />
a^2 + 2ab + b^2 = (a + b)^2,<br />
\stopformula<br />
for $a,b\in {\Bbb C}$. Also,<br />
\placeformula<br />
\startformula<br />
\startalign<br />
\NC \alpha^2 -2\alpha\beta + \beta^2 \NC = (\alpha - \beta)^2. \NR[eq:Identity]<br />
\NC ||x||^2 + ||y||^2 \NC= (x+y|x+y) \NR[eq:Pythagore]<br />
\stopalign<br />
\stopformula <br />
Note that \eqref[eq:Square] is true whenever we have $ab = ba$ for $b,a \in {\Bbb A}$. This is not the case when ${\Bbb A}$ is the ring of the matrices.<br />
It is clear that \eqref[eq:Identity] is a consequence of \eqref[eq:Square].<br />
<br />
<br />
\startitemize[n]<br />
<br />
\item[it:1]<br />
Testing items to see where are the reference points and names.<br />
<br />
\startitem[it:2]<br />
Testing items for show reference<br />
\stopitem<br />
<br />
\stopitemize<br />
<br />
See \in{item}[it:2] in \in{section}[sec:First].<br />
<br />
\startplacefigure[reference=fig:1]<br />
\externalfigure[dummy]<br />
\stopplacefigure<br />
<br />
See \in{figure}[fig:1], and then have a look at \in{section}[sec:Testing] of \in{chapter}[ch:New].<br />
<br />
\stopsection<br />
<br />
\startsection[title=Testing \type{\showreferences},reference=sec:Testing]<br />
<br />
\input knuth.tex<br />
<br />
\stopchapter<br />
<br />
\stoptext<br />
<br />
</texcode><br />
<br />
When typeset, with mkiv, this gives all the informations used for cross referencing.<br />
<br />
There is also a module, which is invoked by saying:<br />
<texcode><br />
\usemodule[references-show]<br />
</texcode><br />
which does somewhat more: not only the command \showreferences is activated, but on the top right corner of each page, a short list of all the references on that page is printed.<br />
<br />
Actually, due to the fact that some of the informations is written between lines, depending on the bodyfont used in the document, one may need to fine tune more or less the position of the small prints: you can play with the following commands<br />
<texcode><br />
\enabletrackers[nodes.references.show=2.5]<br />
\enabletrackers[nodes.destinations.show=1]<br />
</texcode><br />
by changing the numerical values according to your needs (remember nodes.destinations means the point where you have given a name to a certain object, and nodes.references corresponds to the point at which you refer to those objects).<br />
<br />
Another useful information is that the command<br />
<texcode><br />
\showreferences<br />
</texcode><br />
is an alias for the following command<br />
<texcode><br />
\enabletrackers[nodes.references.show,nodes.destinations.show]<br />
</texcode><br />
and you may enable only the tracking of either of the nodes, for instance by saying<br />
<texcode><br />
\enabletrackers[nodes.destinations.show]<br />
</texcode><br />
you can visualize only the destinations nodes, or you may say<br />
<texcode><br />
\enabletrackers[nodes.references.show]<br />
</texcode><br />
to see only where the references are invoked.<br />
<br />
Finally, there is a command<br />
<texcode><br />
\enabletrackers[nodes.references,nodes.destinations]<br />
</texcode><br />
which allows to visualize the objects which are destination or reference nodes (again, the above command can be split in two).</div>Otaredhttps://wiki.contextgarden.net/index.php?title=%5Cshowreferences&diff=22785\showreferences2015-04-14T20:22:31Z<p>Otared: </p>
<hr />
<div>When writing a long document with many cross references, at the stage of proof reading, it comes handy if one could visualize what are the names given to various nodes (in ConTeXt parlance « nodes.destinations ») and also to visualize what destinations are invoked in the text (in ConTeXt they are named « nodes.references »). <br />
<br />
There is a command named <br />
<texcode><br />
\showreferences<br />
</texcode><br />
which just does this: it prints in small fonts, to the left of each such node the name of the destination, or the name of the reference used at that point. Let us show it in action in the following (not so minimal) example (to be typeset with mkiv):<br />
<br />
<texcode><br />
\setupinteraction[state=start]<br />
\setupbodyfont[10pt,palatino]<br />
\useMPlibrary[dum]<br />
<br />
%\usemodule[references-show]<br />
<br />
%\enabletrackers[nodes.references,nodes.destinations]<br />
<br />
\showreferences<br />
<br />
%\enabletrackers[nodes.references.show]<br />
<br />
%\enabletrackers[nodes.references]<br />
<br />
%\enabletrackers[nodes.destinations.show]<br />
<br />
%\enabletrackers[nodes.destinations]<br />
<br />
%\enabletrackers[nodes.references.show=2.5]<br />
%\enabletrackers[nodes.destinations.show=1]<br />
<br />
\definereferenceformat[eqref][left=(,right=)] <br />
\setupformulas[location=right]<br />
<br />
\starttext<br />
<br />
\startchapter[title=Some references,reference=ch:New]<br />
<br />
\startsection[title=Some facts,reference=sec:First]<br />
We have<br />
\placeformula[eq:Square]<br />
\startformula<br />
a^2 + 2ab + b^2 = (a + b)^2,<br />
\stopformula<br />
for $a,b\in {\Bbb C}$. Also,<br />
\placeformula<br />
\startformula<br />
\startalign<br />
\NC \alpha^2 -2\alpha\beta + \beta^2 \NC = (\alpha - \beta)^2. \NR[eq:Identity]<br />
\NC ||x||^2 + ||y||^2 \NC= (x+y|x+y) \NR[eq:Pythagore]<br />
\stopalign<br />
\stopformula <br />
Note that \eqref[eq:Square] is true whenever we have $ab = ba$ for $b,a \in {\Bbb A}$. This is not the case when ${\Bbb A}$ is the ring of the matrices.<br />
It is clear that \eqref[eq:Identity] is a consequence of \eqref[eq:Square].<br />
<br />
<br />
\startitemize[n]<br />
<br />
\item[it:1]<br />
Testing items to see where are the reference points and names.<br />
<br />
\startitem[it:2]<br />
Testing items for show reference<br />
\stopitem<br />
<br />
\stopitemize<br />
<br />
See \in{item}[it:2] in \in{section}[sec:First].<br />
<br />
\startplacefigure[reference=fig:1]<br />
\externalfigure[dummy]<br />
\stopplacefigure<br />
<br />
See \in{figure}[fig:1], and then have a look at \in{section}[sec:Testing] of \in{chapter}[ch:New].<br />
<br />
\stopsection<br />
<br />
\startsection[title=Testing \type{\showreferences},reference=sec:Testing]<br />
<br />
\input knuth.tex<br />
<br />
\stopchapter<br />
<br />
\stoptext<br />
<br />
</texcode><br />
<br />
When typeset, with mkiv, this gives all the informations used for cross referencing.<br />
<br />
There is also a module, which is invoked by saying:<br />
<texcode><br />
\usemodule[references-show]<br />
</texcode><br />
which does somewhet more: not only the command \showreferences is activated, but on the top right corner of each page, a short list of all the references on that page is printed.<br />
<br />
Actually, due to the fact that some of the informations is written between lines, depending on the bodyfont used in the document, one may need to fine tune more or less the position of the small prints: you can play with the following commands<br />
<texcode><br />
\enabletrackers[nodes.references.show=2.5]<br />
\enabletrackers[nodes.destinations.show=1]<br />
</texcode><br />
by changing the numerical values according to your needs (remember nodes.destinations means the point where you have given a name to a certain object, and nodes.references corresponds to the point at which you refer to those objects).<br />
<br />
Another useful information is that the command<br />
<texcode><br />
\showreferences<br />
</texcode><br />
is an alias for the following command<br />
<texcode><br />
\enabletrackers[nodes.references.show,nodes.destinations.show]<br />
</texcode><br />
and you may enable only the tracking of either of the nodes, for instance by saying<br />
<texcode><br />
\enabletrackers[nodes.destinations.show]<br />
</texcode><br />
you can visualize only the destinations nodes, or you may say<br />
<texcode><br />
\enabletrackers[nodes.references.show]<br />
</texcode><br />
to see only where the references are invoked.<br />
<br />
Finally, there is a command<br />
<texcode><br />
\enabletrackers[nodes.references,nodes.destinations]<br />
</texcode><br />
which allows to visualize the objects which are destination or reference nodes (again, the above command can be split in two).</div>Otaredhttps://wiki.contextgarden.net/index.php?title=%5Cshowreferences&diff=22784\showreferences2015-04-14T20:05:46Z<p>Otared: Created page with "When writing a long document with many cross references, at the stage of proof reading, it comes handy if one could visualize what are the names given to various nodes (in ConTeX..."</p>
<hr />
<div>When writing a long document with many cross references, at the stage of proof reading, it comes handy if one could visualize what are the names given to various nodes (in ConTeXt parlance « nodes.destinations ») and also to visualize what destinations are invoked in the text (in ConTeXt they are named « nodes.references »). <br />
<br />
There is a command named <br />
<texcode><br />
\showreferences<br />
</texcode><br />
which just does this: it prints in small fonts, to the left of each such node the name of the destination, or the name of the reference used at that point. Let us show it in action in the following (not so minimal) example (to be typeset with mkiv):<br />
<br />
<texcode><br />
\setupinteraction[state=start]<br />
\setupbodyfont[10pt,palatino]<br />
\useMPlibrary[dum]<br />
<br />
%\usemodule[references-show]<br />
<br />
%\enabletrackers[nodes.references,nodes.destinations]<br />
<br />
\showreferences<br />
<br />
%\enabletrackers[nodes.references.show]<br />
<br />
%\enabletrackers[nodes.references]<br />
<br />
%\enabletrackers[nodes.destinations.show]<br />
<br />
%\enabletrackers[nodes.destinations]<br />
<br />
%\enabletrackers[nodes.references.show=2.5]<br />
%\enabletrackers[nodes.destinations.show=1]<br />
<br />
\definereferenceformat[eqref][left=(,right=)] <br />
\setupformulas[location=right]<br />
<br />
\starttext<br />
<br />
\startchapter[title=Some references,reference=ch:New]<br />
<br />
\startsection[title=Some facts,reference=sec:First]<br />
We have<br />
\placeformula[eq:Square]<br />
\startformula<br />
a^2 + 2ab + b^2 = (a + b)^2,<br />
\stopformula<br />
for $a,b\in {\Bbb C}$. Also,<br />
\placeformula<br />
\startformula<br />
\startalign<br />
\NC \alpha^2 -2\alpha\beta + \beta^2 \NC = (\alpha - \beta)^2. \NR[eq:Identity]<br />
\NC ||x||^2 + ||y||^2 \NC= (x+y|x+y) \NR[eq:Pythagore]<br />
\stopalign<br />
\stopformula <br />
Note that \eqref[eq:Square] is true whenever we have $ab = ba$ for $b,a \in {\Bbb A}$. This is not the case when ${\Bbb A}$ is the ring of the matrices.<br />
It is clear that \eqref[eq:Identity] is a consequence of \eqref[eq:Square].<br />
<br />
<br />
\startitemize[n]<br />
<br />
\item[it:1]<br />
Testing items to see where are the reference points and names.<br />
<br />
\startitem[it:2]<br />
Testing items for show reference<br />
\stopitem<br />
<br />
\stopitemize<br />
<br />
See \in{item}[it:2] in \in{section}[sec:First].<br />
<br />
\startplacefigure[reference=fig:1]<br />
\externalfigure[dummy]<br />
\stopplacefigure<br />
<br />
See \in{figure}[fig:1], and then have a look at \in{section}[sec:Testing] of \in{chapter}[ch:New].<br />
<br />
\stopsection<br />
<br />
\startsection[title=Testing \type{\showreferences},reference=sec:Testing]<br />
<br />
\input knuth.tex<br />
<br />
\stopchapter<br />
<br />
\stoptext<br />
<br />
</texcode><br />
<br />
When typeset, with mkiv, this gives all the informations used for cross referencing.<br />
<br />
There is also a module, which is invoked by saying:<br />
<texcode><br />
\usemodule[references-show]<br />
</texcode><br />
which does somewhet more: not only the command \showreferences is activated, but on the top right corner a short list of all the references on that page is printed.<br />
<br />
Actually, due to the fact that some of the informations is written between lines, depending on the bodyfont used in the document one may fine tune more or less the position of the small prints: you can play with the following commands<br />
<texcode><br />
\enabletrackers[nodes.references.show=2.5]<br />
\enabletrackers[nodes.destinations.show=1]<br />
</texcode><br />
by changing the numerical values according to your needs (remember nodes.destinations means the point where you have given a name to a certain object, and nodes.references corresponds to the point you refer to those objects).<br />
<br />
Another useful information is that the command<br />
<texcode><br />
\showreferences<br />
</texcode><br />
is an alias for the following command<br />
<texcode><br />
\enabletrackers[nodes.references.show,nodes.destinations.show]<br />
</texcode><br />
and you may enable only the tracking of either of the nodes, for instance by saying<br />
<texcode><br />
\enabletrackers[nodes.destinations.show]<br />
</texcode><br />
to visualize only the destinations nodes, or you may say<br />
<texcode><br />
\enabletrackers[nodes.references.show]<br />
</texcode><br />
to see only where the references are invoked.<br />
<br />
Finally, there is a command<br />
<texcode><br />
\enabletrackers[nodes.references,nodes.destinations]<br />
</texcode><br />
which allows to visualize the objects which are destination or reference nodes (again, the above command can be split in two).</div>Otaredhttps://wiki.contextgarden.net/index.php?title=Command/setupinterlinespace&diff=22554Command/setupinterlinespace2014-07-27T15:59:30Z<p>Otared: </p>
<hr />
<div>{{Reference<br />
|name=setupinterlinespace<br />
|attributes=<br />
}}<br />
<br />
== [[Help:Reference|Syntax]] ==<br />
<table cellspacing="4" cellpadding="2" class="cmd"><br />
<tr><br />
<td colspan="2" class="cmd">\setupinterlinespace</td><br />
</tr><br />
<tr><br />
<td colspan="2" class="cmd">\setupinterlinespace<span class="first">[...]</span></td><br />
</tr><br />
<tr><br />
<td colspan="2" class="cmd">\setupinterlinespace<span class="third">[...,...=...,...]</span></td><br />
</tr><br />
<tr valign="top" class="first"><br />
<td class="cmd">[...]</td><br />
<td>reset <strong>small</strong> medium auto big on off </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:height|height]]</td><br />
<td>[[Command/value:number|number]]</td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:depth|depth]]</td><br />
<td>[[Command/value:number|number]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:line|line]]</td><br />
<td>[[Command/value:dimension|dimension]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:top|top]]</td><br />
<td>[[Command/value:number|number]] </td><br />
</tr><br />
<tr valign="top" class="third"><br />
<td class="cmd">[[Command/keyword:bottom|bottom]]</td><br />
<td>[[Command/value:number|number]] </td><br />
</tr><br />
</table><br />
<br />
== Description == <br />
<br />
{{cmd|setupinterlinespace}} sets up the distance between lines. can be used in three variants:<br />
<br />
* Without any keywords:<texcode>\setupinterlinespace</texcode> The command sets the baselineskip to <code>2.8ex</code>.<br />
<br />
* With a keyword: <texcode>\setupinterlinespace[...]</texcode> The following keywords are available:<br />
** reset<br />
** small<br />
** medium<br />
** big<br />
** on<br />
** off<br />
<br />
* Setting all the parameters involved in line positioning:\setupinterlinespace[...,...=...,...]<br />
** <code>line</code>: base interline spacing (baselineskip) (e.g. 3ex or 12pt)<br />
** <code>height</code>: size of strut as multiple of <code>line</code> (e.g. 0.72)<br />
** <code>depth</code>: size of strut as multiple of <code>line</code> (e.g. 0.28)<br />
** <code>top</code>: topskip (as multiple of line height)<br />
** <code>bottom</code>: maxdepth (as multiple of line height)<br />
** <code>stretch</code>: (glue) line stretching<br />
<br />
When setting up the interlinespace, it matters which unit one chooses:<br />
<br />
<texcode><br />
% interline space adapts itself to body font size<br />
\setupinterlinespace[line=2.8ex]<br />
<br />
% set interlinespace to 12pt for every font size --- don't do this!<br />
\setupinterlinespace[line=12pt]<br />
<br />
% set interlinespace for a particular body font size<br />
% Useful if you think in terms of "nine-point type on eleven-point leading"<br />
\definebodyfontenvironment[9pt][interlinespace=11pt]<br />
</texcode><br />
<br />
A final note: it seems that <tt>\setupinterlinespace[line=XX\baselineskip]</tt> interacts badly with <tt>\setuplayout</tt> while <tt>\setupinterlinespace[big]</tt> works fine.<br />
<br />
== Example ==<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A5][A5]<br />
\setupwhitespace[big]<br />
<br />
\starttext<br />
<br />
\the\baselineskip<br />
<br />
% The interlinespace does not change automatically when the font changes<br />
{ \definedfont[Serif at 32pt]<br />
This is the long long title of my book\crlf<br />
baselineskip=\the\baselineskip<br />
}<br />
<br />
% Invoking \setupinterlinespace sets baselineskip to 2.8ex of current font<br />
{ \definedfont[Serif at 32pt]<br />
\setupinterlinespace <br />
This is the long long title of my book\crlf<br />
baselineskip=\the\baselineskip<br />
}<br />
<br />
\stoptext<br />
<br />
</context><br />
<br />
== Example within {{cmd|startnarrower}}...{{cmd|stopnarrower}} ==<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A5][A5]<br />
\setupinterlinespace[5] % this has no effect before \starttext<br />
<br />
\starttext<br />
\input tufte % this is default, not 5<br />
\setupinterlinespace[big]<br />
\input tufte % this is big<br />
<br />
\startnarrower[left]<br />
\setupinterlinespace[small]<br />
\input tufte % this is small<br />
\stopnarrower<br />
<br />
\input tufte % this is big again<br />
\stoptext<br />
<br />
</context><br />
<br />
<br />
== See also ==<br />
* {{cmd|setupwhitespace}} to set up inter-paragraph spacing.<br />
* {{cmd|setuplayout}} to set sizes of text areas.<br />
* {{cmd|setuplocalinterlinespace}} to set interline spacing locally.<br />
<br />
== Help from ConTeXt-Mailinglist/Forum ==<br />
All issues with:<br />
{{Forum|{{SUBPAGENAME}}}}<br />
<br />
[[Category:Command/Paragraphs|setupinterlinespace]]<br />
[[Category:Command/Layout|setupinterlinespace]]</div>Otaredhttps://wiki.contextgarden.net/index.php?title=setuplocalinterlinespace&diff=22553setuplocalinterlinespace2014-07-27T09:16:23Z<p>Otared: </p>
<hr />
<div>In some situations one may be willing to have a different interlinespace in a particular part of text, header, etc.<br />
To this end one can use the command<br />
<br />
<texcode><br />
\setuplocalinterlinespace<br />
</texcode><br />
<br />
or its synonym<br />
<br />
<texcode><br />
\switchtointerlinespace<br />
</texcode><br />
<br />
Either of these commands should be enclosed in a {{cmd|start}}...{{cmd|stop}} pair. The arguments taken by {{cmd|setuplocalinterlinespace}} or its synonym {{cmd|switchtointerlinespace}} are analogous to the dimensions used in {{cmd|setupinterlinespace}}. Below is an example, with some explanations of the commands.<br />
<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A5][A5]<br />
\setupinterlinespace[line=3.2ex] % global setting <br />
% (\setupinterlinespace[3.2ex] == \setupinterlinespace[line=3.2ex])<br />
<br />
\starttext<br />
<br />
\input ward<br />
<br />
\blank <br />
\setupinterlinespace[big] % big = 1.5 * global value (small = 1x, medium = 1.25x)<br />
<br />
\input ward<br />
<br />
\blank <br />
<br />
\setupinterlinespace[reset] % here we go back to the global interlinespace set before<br />
<br />
\input ward<br />
<br />
\blank <br />
\start <br />
\setuplocalinterlinespace[line=4ex] % here a local value for the interlinespace is set<br />
<br />
\input ward<br />
<br />
\stop % an empty line or \par before the end of the group is necessary<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=setuplocalinterlinespace&diff=22531setuplocalinterlinespace2014-07-13T19:55:16Z<p>Otared: </p>
<hr />
<div>In some situations one may be willing to have a different interlinespace in a particular part of text, header, etc.<br />
To this end one can use the command<br />
<br />
<texcode><br />
\setupinterlinespace<br />
</texcode><br />
<br />
or its synonym<br />
<br />
<texcode><br />
\switchtointerlinespace<br />
</texcode><br />
<br />
Either of these commands should be enclosed in a {{cmd|start}}...{{cmd|stop}} pair. The arguments taken by {{cmd|setuplocalinterlinespace}} or its synonym {{cmd|switchtointerlinespace}} are analogous to the dimensions used in {{cmd|setupinterlinespace}}. Below is an example, with some explanations of the commands.<br />
<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A5][A5]<br />
\setupinterlinespace[line=3.2ex] % global setting <br />
% (\setupinterlinespace[3.2ex] == \setupinterlinespace[line=3.2ex])<br />
<br />
\starttext<br />
<br />
\input ward<br />
<br />
\blank <br />
\setupinterlinespace[big] % big = 1.5 * global value (small = 1x, medium = 1.25x)<br />
<br />
\input ward<br />
<br />
\blank <br />
<br />
\setupinterlinespace[reset] % here we go back to the global interlinespace set before<br />
<br />
\input ward<br />
<br />
\blank <br />
\start <br />
\setuplocalinterlinespace[line=4ex] % here a local value for the interlinespace is set<br />
<br />
\input ward<br />
<br />
\stop % an empty line or \par before the end of the group is necessary<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=setuplocalinterlinespace&diff=22530setuplocalinterlinespace2014-07-13T19:51:47Z<p>Otared: Created page with "In some situations one may be willing to have a different interlinespace in a particular part of text, header, etc. To this end one can use the command <texcode> \setupinterline..."</p>
<hr />
<div>In some situations one may be willing to have a different interlinespace in a particular part of text, header, etc.<br />
To this end one can use the command<br />
<br />
<texcode><br />
\setupinterlinespace<br />
</texcode><br />
<br />
or its synonym<br />
<br />
<texcode><br />
\switchtointerlinespace<br />
</texcode><br />
<br />
Either of these commands should be enclosed in a {{cmd|start}}...{{cmd|stop}} pair. The arguments taken by {{cmd|setuplocalinterlinespace}} or its synonym {{cmd|switchtointerlinespace}} are analogous to the dimensions used in {{cmd|setupinterlinespace}}. Below is an example, with some explanations of the commands.<br />
<br />
<context source=yes mode=mkiv><br />
\setuppapersize[A5][A5]<br />
\setupinterlinespace[line=3.2ex] % global setting (\setupinterlinespace[3.2ex] == \setupinterlinespace[line=3.2ex])<br />
<br />
\starttext<br />
<br />
\input ward<br />
<br />
\blank <br />
\setupinterlinespace[big] % big = 1.5 * global value (small = 1x, medium = 1.25x)<br />
<br />
\input ward<br />
<br />
\blank <br />
<br />
\setupinterlinespace[reset] % here we go back to the global interlinespace set before<br />
<br />
\input ward<br />
<br />
\blank <br />
\start <br />
\setuplocalinterlinespace[line=4ex] % here a local value for the interlinespace is set<br />
<br />
\input ward<br />
<br />
\stop % an empty line or \par before the end the group is necessary<br />
<br />
\stoptext<br />
</context></div>Otaredhttps://wiki.contextgarden.net/index.php?title=PDF/A&diff=22527PDF/A2014-07-04T07:17:09Z<p>Otared: Created page with "Using {{code|mkiv}} one can create PDF/A compliant outputs. The format PDF/A, where A stands for Archive, is a format supposed to be sustainable for archiving purposes (see http:..."</p>
<hr />
<div>Using {{code|mkiv}} one can create PDF/A compliant outputs. The format PDF/A, where A stands for Archive, is a format supposed to be sustainable for archiving purposes (see http://en.wikipedia.org/?title=PDF/A). See also a paper by Luigi Scarso on the issue of creating PDF/A file with {{code|mkiv}}: http://www.ntg.nl/maps/41/08.pdf.<br />
<br />
A complete list of PDF formats which can be created can be found in {{src|lpdf-fmt.lua}}.<br />
<br />
In ConTeXt mkiv, in order to create such a compliant PDF/A file, one can do as follows: first install (or check that they are present) the following color profiles files<br />
<texcode><br />
default_cmyk.icc<br />
default_gray.icc<br />
default_rgb.icc<br />
ISOcoated_v2_300_eci.icc<br />
</texcode><br />
into <br />
<br />
{{code|tex/texmf-context/colors/icc/profiles}} <br />
<br />
of your Context standalone directory. The {{code|default_*.icc}} files are part of {{code|ghostscript}} and the file {{code|ISOcoated_v2_300_eci.icc}} can be found at: http://www.eci.org/en/downloads.<br />
<br />
Next add the following commands at the top of your file:<br />
<texcode><br />
\setupinteraction<br />
[title=TITLE,<br />
subtitle=SUBTITLE,<br />
author=AUTHOR,<br />
keyword={{KEYWORD1, KEYWORD2}, KEYWORD3}]<br />
<br />
%% For PDF/A<br />
\setupbackend[<br />
format={pdf/a-1b:2005}, % or pdf/a-1a:2005<br />
profile={default_cmyk.icc,default_rgb.icc,default_gray.icc},<br />
intent=ISO coated v2 300\letterpercent\space (ECI)]<br />
<br />
%% Tagged PDF<br />
%% method=auto ==> default tags by Adobe<br />
\setupbackend[export=yes]<br />
\setupstructure[state=start,method=auto]<br />
</texcode><br />
and typeset your file as usual: you'll get a PDF file which is compliant with the requirements of PDF/A-1a or PDF/A-1b, according to what you have chosen to do. <br />
<br />
A short example is:<br />
<texcode><br />
\setupinteraction<br />
[title=TITLE,<br />
subtitle=SUBTITLE,<br />
author=AUTHOR,<br />
keyword={{KEYWORD1, KEYWORD2}, KEYWORD3}]<br />
<br />
%% For PDF/A<br />
\setupbackend[<br />
format={pdf/a-1b:2005}, % or pdf/a-1a:2005<br />
profile={default_cmyk.icc,default_rgb.icc,default_gray.icc},<br />
intent=ISO coated v2 300\letterpercent\space (ECI)]<br />
<br />
%% Tagged PDF<br />
%% method=auto ==> default tags by Adobe<br />
\setupbackend[export=yes]<br />
\setupstructure[state=start,method=auto]<br />
<br />
\starttext<br />
<br />
\chapter[chap:testing]{Testing}<br />
<br />
\input knuth<br />
<br />
\input tufte<br />
<br />
\input knuth<br />
<br />
\placefigure[middle][fig:foo]<br />
{This is an image}<br />
{\externalfigure[cow.jpg]}<br />
<br />
\input tufte<br />
<br />
\input knuth<br />
<br />
\input tufte<br />
<br />
\stoptext<br />
</texcode><br />
<br />
Beware: when figures and other PDF files are included into your file (for instance here {{code|cow.jpg}}) those files may create difficulties for PDF/A compliance, and sometimes they have to be treated separately.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=math_calligraphic&diff=22496math calligraphic2014-05-22T09:09:04Z<p>Otared: </p>
<hr />
<div>== Using math calligraphic or script fonts in mkiv ==<br />
<br />
In mkii it is possible to replace the standard calligraphic (alias script) math fonts with rsfs (Ralph Smith's Formal Script fonts): see the dedicated page on this wiki [http://wiki.contextgarden.net/rsfs].<br />
<br />
However this old method does not work in mkiv where there is a more robust way to use any opentype or truetype font. Indeed in mkiv it is rather easy to specify any such font to be used for the calligraphic (alias script) math fonts.<br />
For instance let us assume that we want to use math calligraphic fonts from Asana Math, while other glyphs come from Latin Modern. To do so, use the following setups:<br />
<br />
<texcode><br />
\definefallbackfamily[mainface][math][Asana Math][range={uppercasescript,lowercasescript}]<br />
<br />
\definefontfamily [mainface] [rm] [Latin Modern]<br />
\definefontfamily [mainface] [math] [Latin Modern Math]<br />
<br />
\setupbodyfont[mainface]<br />
<br />
\starttext<br />
${\cal ABCDEFGHIJKLMNOPQRSTUVWXYZ}$<br />
<br />
${\cal abcdefghijklmnopqrstuvwxyz}$<br />
<br />
The space of distributions on $\Omega$ is denoted by ${\cal D}'(\Omega)$, while ${\cal S}({\Bbb R}^n)$ is the space of Schwartz functions, and ${\cal S}'({\Bbb R}^n)$ is its dual.<br />
\startformula<br />
\langle T,\phi \rangle_{{\cal D}'(\Omega),{\cal D}(\Omega)}<br />
\stopformula<br />
The set of admissible controls is denoted by ${\cal U}_{\rm ad}$.<br />
\stoptext<br />
</texcode><br />
<br />
Here, the command<br />
<texcode><br />
\definefallbackfamily[mainface][math][Asana Math][range={uppercasescript,lowercasescript}]<br />
</texcode><br />
<br />
tells mkiv that the fallback font for mainface in math mode comes from Asana Math, as far as the slots of lowercase and uppercase calligraphic (or script) glyphs are concerned.<br />
<br />
If one wishes to use for instance Palatino instead of Latin Modern as the text font, as well as the font used in math mode, it is enough to replace the two lines<br />
<texcode><br />
\definefontfamily [mainface] [rm] [Latin Modern]<br />
\definefontfamily [mainface] [math] [Latin Modern Math]<br />
</texcode><br />
with<br />
<texcode><br />
\definefontfamily [mainface] [rm] [TeX Gyre Pagella]<br />
\definefontfamily [mainface] [math] [TeX Gyre Pagella Math]<br />
</texcode><br />
since « Palatino » is in fact an alias for TeX Gyre Pagella.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=math_calligraphic&diff=22495math calligraphic2014-05-22T09:06:36Z<p>Otared: </p>
<hr />
<div>== Using math calligraphic fonts in mkiv ==<br />
<br />
In mkii it is possible to replace the standard calligraphic (alias script) math fonts with rsfs (Ralph Smith's Formal Script fonts): see the dedicated page on this wiki [http://wiki.contextgarden.net/rsfs].<br />
<br />
However this old method does not work in mkiv where there is a more robust way to use any opentype or truetype font. Indeed in mkiv it is rather easy to specify any such font to be used for the calligraphic (alias script) math fonts.<br />
For instance let us assume that we want to use math calligraphic fonts from Asana Math, while other glyphs come from Latin Modern. To do so, use the following setups:<br />
<br />
<texcode><br />
\definefallbackfamily[mainface][math][Asana Math][range={uppercasescript,lowercasescript}]<br />
<br />
\definefontfamily [mainface] [rm] [Latin Modern]<br />
\definefontfamily [mainface] [math] [Latin Modern Math]<br />
<br />
\setupbodyfont[mainface]<br />
<br />
\starttext<br />
${\cal ABCDEFGHIJKLMNOPQRSTUVWXYZ}$<br />
<br />
${\cal abcdefghijklmnopqrstuvwxyz}$<br />
<br />
The space of distributions on $\Omega$ is denoted by ${\cal D}'(\Omega)$, while ${\cal S}({\Bbb R}^n)$ is the space of Schwartz functions, and ${\cal S}'({\Bbb R}^n)$ is its dual.<br />
\startformula<br />
\langle T,\phi \rangle_{{\cal D}'(\Omega),{\cal D}(\Omega)}<br />
\stopformula<br />
The set of admissible controls is denoted by ${\cal U}_{\rm ad}$.<br />
\stoptext<br />
</texcode><br />
<br />
Here, the command<br />
<texcode><br />
\definefallbackfamily[mainface][math][Asana Math][range={uppercasescript,lowercasescript}]<br />
</texcode><br />
<br />
tells mkiv that the fallback font for mainface in math mode comes from Asana Math, as far as the slots of lowercase and uppercase calligraphic (or script) glyphs are concerned.<br />
<br />
If one wishes to use for instance Palatino instead of Latin Modern as the text font, as well as the font used in math mode, it is enough to replace the two lines<br />
<texcode><br />
\definefontfamily [mainface] [rm] [Latin Modern]<br />
\definefontfamily [mainface] [math] [Latin Modern Math]<br />
</texcode><br />
with<br />
<texcode><br />
\definefontfamily [mainface] [rm] [TeX Gyre Pagella]<br />
\definefontfamily [mainface] [math] [TeX Gyre Pagella Math]<br />
</texcode><br />
since « Palatino » is in fact an alias for TeX Gyre Pagella.</div>Otaredhttps://wiki.contextgarden.net/index.php?title=math_calligraphic&diff=22494math calligraphic2014-05-21T21:17:04Z<p>Otared: </p>
<hr />
<div>== Using math calligraphic fonts in mkiv ==<br />
<br />
In mkii it is possible to replace the standard calligraphic (alias script) math fonts with rsfs (Ralph Smith's Formal Script fonts): see the dedicated page on this wiki [http://wiki.contextgarden.net/rsfs].<br />
<br />
However this old method does not work in mkiv where there is a more robust way to use any opentype or truetype font. Indeed in mkiv it is rather easy to specify any such font to be used for the calligraphic (alias script) math fonts.<br />
For instance let us assume that we want to use math calligraphic fonts from Asana Math, while other glyphs come from Latin Modern. To do so, use the following setups:<br />
<br />
<texcode><br />
\definefallbackfamily[mainface][math][Asana Math][range={uppercasescript,lowercasescript}]<br />
<br />
\definefontfamily [mainface] [rm] [Latin Modern]<br />
\definefontfamily [mainface] [math] [Latin Modern Math]<br />
<br />
\setupbodyfont[mainface]<br />
<br />
\starttext<br />
${\cal ABCDEFGHIJKLMNOPQRSTUVWXYZ}$<br />
<br />
${\cal abcdefghijklmnopqrstuvwxyz}$<br />
<br />
The space of distributions on $\Omega$ is denoted by ${\cal D}'(\Omega)$, while ${\cal S}({\Bbb R}^n)$ is the space of Schwartz functions, and ${\cal S}'({\Bbb R}^n)$ is its dual.<br />
\startformula<br />
\langle T,\phi \rangle_{{\cal D}'(\Omega),{\cal D}(\Omega)}<br />
\stopformula<br />
The set of admissible controls is denoted by ${\cal U}_{\rm ad}$.<br />
\stoptext<br />
</texcode><br />
<br />
Here, the command<br />
<texcode><br />
\definefallbackfamily[mainface][math][Asana Math][range={uppercasescript,lowercasescript}]<br />
</texcode><br />
<br />
tells mkiv that the fallback font for mainface in math mode comes from Asana Math, as far as the slots of lowercase and uppercase calligraphic (or script) glyphs are concerned.<br />
<br />
If one wishes to use for instance Palatino instead of Latin Modern as the text font, it is enough to replace<br />
<texcode><br />
\definefontfamily [mainface] [rm] [Latin Modern]<br />
</texcode><br />
with<br />
<texcode><br />
\definefontfamily [mainface] [rm] [Palatino]<br />
</texcode></div>Otared