User:Wolfgang Schuster

From ConTeXt wiki

Sample Code

Full page figures

The following macro allows me to include figures in my document and adjust them to have the size of a normal page.

\unprotect

\definesystemvariable{xx}

\def\fullpagebox
  {\dosingleempty\dofullpagebox}

\def\dofullpagebox[#1]%
  {\bgroup
   \getparameters[\??xx][\c!nx=1,\c!ny=1,#1]%
   \dowithnextbox
     {\!!widtha\dimexpr\paperwidth/\nextboxwd\relax
      \!!widthb\dimexpr\paperheight/\nextboxht\relax
      \ifdim\!!widtha>\!!widthb
        \setbox\nextbox\vbox\!!to\@@xxny\paperheight
          {\vss\expanded{\scale[\c!width=\@@xxnx\paperwidth]{\flushnextbox}}\vss}%
      \else
        \setbox\nextbox\hbox\!!to\@@xxnx\paperwidth
          {\hss\expanded{\scale[\c!height=\@@xxny\paperheight]{\flushnextbox}}\hss}%
      \fi
      \setbox\nextbox\hbox{\clip[\c!width=\@@xxnx\paperwidth,\c!height=\@@xxny\paperheight]{\flushnextbox}}%
      \startTEXpage
      \flushnextbox
      \stopTEXpage
      \egroup}%
   \hbox}

\def\fullpagefigure
  {\dodoubleempty\dofullpagefigure}

\def\dofullpagefigure[#1][#2]%
  {\ifsecondargument
     \fullpagebox[#1]{\externalfigure[#2]}%
   \else
     \fullpagebox[]{\externalfigure[#1]}%
   \fi}

\protect

\starttext

\fullpagefigure[image]

\fullpagefigure[nx=2][image]

\fullpagefigure[ny=2][image]

\stoptext

Flashcards

For tests and vocabulary it is useful to use flashcards for learning. (This version is for mkii. See below for mkiv)

\unprotect

\installpagearrangement FLASHCARD
  {\dosetuparrangement\@@ppnx\@@ppny{\numexpr\@@ppxy*2\relax}%
   {\numexpr\@@ppnx+\plusone\relax}{\numexpr\@@ppny+\plusone\relax}%
   \pusharrangedpageFLASHCARD\poparrangedpagesAB\relax}

\newcounter\arrangedpageshiftX
\newcounter\arrangedpageshiftY

\def\dopusharrangedpageFLASHCARD#1%
 {\ifnum\arrangedpageshiftX<\arrangedpageX
    \ifodd\arrangedpageN
      \handlearrangedpageXandY{#1}0\arrangedpageshiftX\arrangedpageshiftY\arrangedpageA
    \else
      \handlearrangedpageXandY{#1}0{\numexpr\@@ppnx-\arrangedpageshiftX-1\relax}\arrangedpageshiftY\arrangedpageB
      \doglobal\increment\arrangedpageshiftX
    \fi
  \fi
  \ifnum\arrangedpageshiftX=\@@ppnx
    \doglobal\newcounter\arrangedpageshiftX
    \doglobal\increment\arrangedpageshiftY
  \fi}

\def\pusharrangedpageFLASHCARD#1%
 {\doglobal\increment\arrangedpageN
  \reportarrangedpage\arrangedpageN
  \ifnum\arrangedpageN=\numexpr\@@ppxy*2\relax
    \dopusharrangedpageFLASHCARD{#1}%
    \doglobal\newcounter\arrangedpageshiftX
    \doglobal\newcounter\arrangedpageshiftY
    \poparrangedpages
  \else
    \dopusharrangedpageFLASHCARD{#1}%
  \fi}

\protect

To create cards you have to select a number of rows and columns and set the papersize to a value small enough to fit all cards on one sheet of paper.

\setuppapersize[A7][A3,landscape]
\setuppaper    [nx=4,ny=2]
\setuparranging[FLASHCARD]
\setuplayout   [location=middle,marking=on]

\setupbackgrounds[page][frame=on]

\starttext

\dorecurse{20}
  {\startstandardmakeup[align=middle]
   Front side \recurselevel
   \stopstandardmakeup
   \startstandardmakeup[align=middle]
   Back side \recurselevel
   \stopstandardmakeup}

\stoptext


Here is Wolfgang Schuster's own updated mkiv version of ConTeXt flashcards found at http://www.ntg.nl/pipermail/ntg-context/2013/071072.html with attachment http://www.ntg.nl/pipermail/ntg-context/attachments/20130120/ff21c817/attachment-0001.obj

\unprotect

\installpagearrangement FLASHCARD
  {\dosetuparrangement
     \v_page_target_nx
     \v_page_target_ny
     {\numexpr\v_page_target_ny*2\relax}%
     {\numexpr\v_page_target_nx+\plusone\relax}%
     {\numexpr\v_page_target_ny+\plusone\relax}%
   \pusharrangedpageFLASHCARD\poparrangedpagesAB\relax}

\newcount\arrangedpageshiftX
\newcount\arrangedpageshiftY

\def\dopusharrangedpageFLASHCARD#1%
  {\ifnum\arrangedpageshiftX<\arrangedpageX
     \ifodd\arrangedpageN
       \handlearrangedpageXandY{#1}0\arrangedpageshiftX\arrangedpageshiftY\arrangedpageA
     \else
       \handlearrangedpageXandY{#1}0{\numexpr\v_page_target_nx-\arrangedpageshiftX-1\relax}\arrangedpageshiftY\arrangedpageB
       \global\advance\arrangedpageshiftX\plusone
     \fi
   \fi
   \ifnum\arrangedpageshiftX=\v_page_target_nx
     \global\arrangedpageshiftX\zerocount
     \global\advance\arrangedpageshiftY\plusone
   \fi}

\def\pusharrangedpageFLASHCARD#1%
  {\advancearrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifnum\arrangedpageN=\numexpr\v_page_target_xy*2\relax
     \dopusharrangedpageFLASHCARD{#1}%
     \global\arrangedpageshiftX\zerocount
     \global\arrangedpageshiftY\zerocount
     \poparrangedpages
   \else
     \dopusharrangedpageFLASHCARD{#1}%
   \fi}

\protect

The switch to mkiv brings no change in the example invoking the flashcards (see above), but here is my version modified to accept the stationer Avery 5388 paper, which contains three (3) 3 in. by 5 in. perforated cards on each US-letter sheet.

\definepapersize[avery5388][height=76.2mm,width=127mm]
\setuppapersize[avery5388][letter]
\setuppaper    [nx=1,ny=3]
\setuparranging[FLASHCARD]
\setuplayout   [location=middle,marking=on]

\setupbackgrounds[page][frame=on]

\starttext

\dorecurse{20}
  {\startstandardmakeup[align=middle]
   Front side \recurselevel
   \stopstandardmakeup
   \startstandardmakeup[align=middle]
   Back side \recurselevel
   \stopstandardmakeup}

\stoptext



Brochures

\unprotect

\installpagearrangement LEAFLET
  {\dosetuparrangement\@@ppnx1{\numexpr\@@ppnx*2\relax}{\numexpr\@@ppnx+\plusone\relax}2%
   \pusharrangedpageLEAFLET\poparrangedpagesAB\relax}

\def\pusharrangedpageLEAFLET#1%
  {\doglobal\increment\arrangedpageN
   \reportarrangedpage\arrangedpageN
   \ifnum\arrangedpageN=\plusone
     \handlearrangedpageXandY{#1}0{\numexpr\@@ppnx-1\relax}0\arrangedpageA
   \else\ifnum\arrangedpageN>\numexpr\@@ppnx+1\relax
     \handlearrangedpageXandY{#1}0{\numexpr\arrangedpageN-\@@ppnx-2\relax}0\arrangedpageA
   \else
     \handlearrangedpageXandY{#1}0{\numexpr\arrangedpageN-2\relax}0\arrangedpageB
   \fi\fi
   \ifnum\arrangedpageN=\arrangedpageT
     \poparrangedpages
   \fi}

\protect
\setuppapersize[DL][A3,landscape]
\setuppaper    [nx=3]
\setuparranging[LEAFLET]
\setuplayout   [location=middle,marking=on]

\setupbackgrounds[page][frame=on]

\starttext

\dorecurse{6}
 {\startstandardmakeup[align=middle]
  \ssd Page \recurselevel
  \stopstandardmakeup}

\stoptext

Dictionary entries in XML-format

Just a example how to use XML to store and access information.

In my example the content for the dictionary is stored in a buffer, to use a external file for the data the commented lines for MkII and MkIV are required.

% engine=pdftex

\startbuffer[lexicon]
<lexicon>
  <entry name='LaTeX'>
    <name>latex;</name>
    <description>Macro package from Leslie Lamport.</description>
  </entry>
  <entry name='ConTeXt'>
    <name>context;</name>
    <description>Macro package from Hans Hagen.</description>
  </entry>
  <entry name='TeX'>
    <name>tex;</name>
    <description>the program from von Donald E. Knuth.</description>
  </entry>
  <entry name='Pragma Ade'>
    <name>PRAGMA Advanced Document Engineering</name>
    <description>Hans Hagens company.</description>
  </entry>
</lexicon>
\stopbuffer

This is the MkII code to process the data above.

\definesorting[lexiconentry][lexicon]

\setupsorting
  [lexiconentry]
  [criterium=all,
   command=\placelexiconentry]

\definedescription
  [lexicon]
  [width=broad,
   hang=1]

\def\placelexiconentry#1%
  {\bgroup
   \enableXML
   \expanded{\flushXMLelement{name:#1}}%
   \egroup}

\defineXMLentities [tex]     {tex}     {\TeX}
\defineXMLentities [latex]   {latex}   {\LaTeX}
\defineXMLentities [context] {context} {\ConTeXt}

\defineXMLenvironment [entry]
  {\defineXMLsave[name]
   \defineXMLsave[description]}
  {\startlexicon{\XMLflush{name}}
   \XMLflush{description}
   \stoplexicon}

\startXMLmapping[lexicon:load]

\defineXMLenvironmentsave [entry] [name=]
  {}
  {\edef\currentLEXICONENTRY{\XMLop{name}}%
   \gsaveXMLdatainelement{name:\currentLEXICONENTRY}{entry}{entry}%
   \expanded{\lexiconentry[lexicon:\XMLop{name}]{\XMLop{name}}}%
   \setbox\scratchbox\hbox{\getvalue{lexicon:\XMLop{name}}}}

\stopXMLmapping

\def\loaddata
  {\dosingleargument\doloaddata}

\def\doloaddata[#1]%
  {\bgroup
   \endlinechar\minusone
   \startXMLmapping[lexicon:load]%
   %\expanded{\processXMLfilegrouped{#1}}%
   \expanded{\processXMLbuffer[#1]}%
   \stopXMLmapping
   \egroup}

\starttext
\loaddata[lexicon]
\placelistoflexicon
\stoptext

In MkIV we have direct access to the data can process the content without the previous step to save everything in macros.

\definesorting[lexiconentry][lexicon]

\setupsorting
  [lexiconentry]
  [criterium=all,
   command=\placelexiconentry]

\startxmlsetups xml:lexicon
  \xmlsetsetup{\xmldocument}{entry}{*}
\stopxmlsetups

\xmlregistersetup{xml:lexicon}

\startxmlsetups entry
  \expanded{\lexiconentry[lexicon:\xmlatt{#1}{name}]{\xmlatt{#1}{name}}}
  \setbox\scratchbox\hbox{\getvalue{lexicon:\xmlatt{#1}{name}}}
\stopxmlsetups

\definedescription
  [lexicon]
  [width=broad,
   hang=1]

\def\placelexiconentry#1%
  {\bgroup
   \enableXML
   \xmlfilter{lexicon:process}{/lexicon/entry[@name='#1']}
   \egroup}

\defineXMLentities [tex]     {tex}     {\TeX}
\defineXMLentities [latex]   {latex}   {\LaTeX}
\defineXMLentities [context] {context} {\ConTeXt}

\defineXMLenvironment [entry]
  {\defineXMLsave[name]
   \defineXMLsave[description]}
  {\startlexicon{\XMLflush{name}}
   \XMLflush{description}
   \stoplexicon}

\xmlprocessbuffer{lexicon:load}{lexicon}{}

\starttext
\xmlloadbuffer{lexicon:process}{lexicon}
\placelistoflexicon
\stoptext


Feature Requests

  • grid=yes option for \definelayout,
  • temporary user variable for privat macros (see above),
  • macro \defineuvervariable{...} to define namespaces for third party modules.
  • New version of the \startfigure environment


Links