Difference between revisions of "User:Wolfgang Schuster"

From Wiki
Jump to navigation Jump to search
(new example code)
(MkIV version for dictionary example.)
Line 169: Line 169:
 
Just a example how to use XML to store and access information.
 
Just a example how to use XML to store and access information.
  
[Note: I will add the MkIV version when I'm able to find the file]
+
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.
  
 
<texcode>
 
<texcode>
Line 198: Line 200:
 
<texcode>
 
<texcode>
 
\stopbuffer
 
\stopbuffer
 +
</texcode>
  
 +
This is the MkII code to process the data above.
 +
 +
<texcode>
 
\definesorting[lexiconentry][lexicon]
 
\definesorting[lexiconentry][lexicon]
  
Line 253: Line 259:
 
\starttext
 
\starttext
 
\loaddata[lexicon]
 
\loaddata[lexicon]
 +
\placelistoflexicon
 +
\stoptext
 +
</texcode>
 +
 +
In MkIV we have direct access to the data can process the content
 +
without the previous step to save everything in macros.
 +
 +
<texcode>
 +
\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
 
\placelistoflexicon
 
\stoptext
 
\stoptext

Revision as of 07:37, 14 October 2008

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 usefull to use flashcards for learning.

\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

Brochures

\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