% language=en

% todo
% clip if too high? lh vs. gl
% give cleaner msg?
% in the subcolumnsets: 1,4 2,5 3,6 gave error, os.exit commented. check!

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Various trackers and helpers

% \enabletrackers[columnsets.state]
% \showframe

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SM = Screen Medium (wide)
\setuppapersize[SM][SM]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Setup some colors

\usecolors
  [crayola]

\definepalet
  [C]
  [1=CulturedPearl,
   2=Timberwolf,
   3=BlueI,
   4=VanDykeBrown]

\setupcolors
  [textcolor=C:4]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Setup the font

\definefontfeature
  [default]
  [default]
  [expansion=quality,
   protrusion=quality]

\setupinterlinespace
  [top=height]

\setupbodyfont
  [kurier,12pt]

% Define a user variable gl
% that holds the default line
% height (convenience)

\newdimension \MydimGL
\MydimGL \globalbodyfontlineheight

\pushoverloadmode
    \newuserunit\MydimGL gl
\popoverloadmode

% Define a few helping fonts
\definefont[TitleFont][file:kurierheavy-regular.otf*default at 28pt]
\definefont[TextFont][file:kuriercondheavy-regular.otf*default at 21pt]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Setup the layout. A bit weird numbers, but whatever.
% We will use columns, so we work on the grid.
% We set the height via lines.

\setuplayout
  [grid=yes,
   backspace=1cm,
   topspace=.75cm,
   header=1cm,
   headerdistance=1.25cm,
   footer=0cm,
   lines=22,
   margin=0cm,
   width=fit]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define a macro for the headers
\setupheadertexts[]
\protected\def\SetTitle#1{%
  \setupheadertexts
    [\unframed
      [height=1cm,
       align=low,
       foregroundstyle=\TitleFont]
      {\begstrut #1\endstrut}]}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Setup backgrounds via an overlay

\startuseMPgraphic{Background}
  fill Page withcolor "C:2" ;
  fill Field[Text][Header]
       rightenlarged   .5cm
       leftenlarged    .5cm
       topenlarged    .25cm
       bottomenlarged .25cm
       withcolor "C:1" ;
  fill Field[Text][Text]
       enlarged .5cm
       withcolor "C:1" ;
\stopuseMPgraphic

\defineoverlay
  [Background]
  [{\useMPgraphic{Background}}]

\setupbackgrounds
  [paper]
  [background=Background]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define columnsets

% We start with a setup with various parameters.
\startsetups balance:bt
  \balancetopskip \strutht
  \balancebottomskip \strutdp
  \balanceemergencystretch 0pt
  \balanceemergencyshrink 0pt
  \balanceadjdemerits 0
  \balancetolerance 10
  \balancelineheight \lineheight
  % \balancefinalpenalties 3 10000 9000 8000\relax
\stopsetups

% This defines the columnset
\definecolumnset
  [bt]
  [n=3,
   limit=4,
   %page=right,
   setups=balance:bt,
   align={defaultwidows,defaultclubs,defaultbroken},
   distance=0.5cm]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define a framed used over and over in this document
% We set defaults to 6 lines high and one column wide
% and adapt that when needed
\defineframed
  [Fig]
  [frame=no,
   background=color,
   backgroundcolor=C:4,
   foregroundstyle=\TextFont,
   foregroundcolor=C:2,
   offset=overlay,
   align={middle,lohi},
   width=\columnsethspan{1}{1},
   height=6gl]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% We define and setup paragraph passes
% (This one should probably be one that is easier to setup
% since it often seems to work well

\newinteger\parpassdefaultone
\parpassdefaultone\parpassidentifier{bt}

\startsetups align:pass:bt
    \parpasses  5
        identifier           \parpassdefaultone
        tolerance            100
        adjustspacing          0
        emergencyfactor        0
        hyphenation            0
        mathpenaltyfactor  20000
        orphanlinefactors   4 100 250 500 1000
        toddlerpenalties    1 options 2 200 50
    next
        tolerance            200
        adjustspacing          3
        adjustspacingstep      1
        adjustspacingshrink    5
        adjustspacingstretch  10
    next
        tolerance            300
        adjustspacing          0
    next
        tolerance            200
        adjustspacing          3
        adjustspacingshrink   20
        adjustspacingstretch  40
    next
        tolerance            400
        hyphenation            1
        extrahyphenpenalty   200
        adjustspacing          3
        adjustspacingshrink   30
        adjustspacingstretch  60
        emergencystretch       1\bodyfontsize
        emergencyfactor     1000
        mathpenaltyfactor   1000
     \relax
\stopsetups

\setupalignpass[bt]

% Maybe parpasses will be enabled here in
% the future
\setupalign
  [hanging,
   profile,
   granular,
   lessbroken,
   lessorphans,
   strictmath]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Just a few setups
\setupformula
  [grid=math:-halfline]

\setuphead
  [section]
  [style=\bfa]

\starttext

\startcolumnset[bt]

\SetTitle{typesetting in columns}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=1,r=1]
  \startframed[Fig][height=22gl]
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=1]
  \startframed[Fig][height=22gl]
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=1]
  \startframed[Fig][height=22gl]
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\startcolumnsetsheet[bt][2]

\startsetcolumnset[c=1,r=1]
  \startframed[Fig]
    \TEX\ was not made to work in columns
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=8]
  \startframed[Fig][height=8gl,offset=3ts]
    yet, it has for long been possible to use columns
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=17]
  \startframed[Fig]
    how is this\crlf possible?
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\startuseMPgraphic{splitcolumns}
  path p ; p := unitsquare xyscaled(1cm,9cm) ;
  fill p withcolor "C:3" ;

  draw (1/3[llcorner p,ulcorner p] -- 1/3[lrcorner p,urcorner p])
       shortened -.5cm
       &&
       (2/3[llcorner p,ulcorner p] -- 2/3[lrcorner p,urcorner p])
       shortened -.5cm
       withdashes .25cm
       withpen pencircle scaled 2
       withcolor "C:2" ;

  picture c ;
  c := image(
  fill unitsquare
       xyscaled(1cm,3cm)
       withcolor "C:3" ;

  fill unitsquare
       xyscaled(1cm,3cm)
       xshifted 1.5cm
       withcolor "C:3" ;
  fill unitsquare
       xyscaled(1cm,3cm)
       xshifted   3cm
       withcolor "C:3" ;
  ) xshifted 4cm ;

  draw c ;

  draw (boundingbox c enlarged .5cm)
       withdashes .25cm
       withpen pencircle scaled 2
       withcolor "C:2" ;
\stopuseMPgraphic

\startcolumnsetsheet[bt][3]

\startsetcolumnset[c=1,r=1]
  \startframed[Fig][foregroundcolor=C:1]
    mixedcolumns
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=1,r=8]
  \startframed[Fig][height=8gl,offset=2ts]
    make one big vbox and split it with vsplit
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=1]
  \startframed[Fig][width=\columnsethspan{2}{2},height=22gl]
    \useMPgraphic{splitcolumns}
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\startuseMPgraphic{pagecolumns}
  picture c ;
  c := image(
  fill unitsquare
       xyscaled(1cm,3cm)
       withcolor "C:3" ;
  fill unitsquare
       xyscaled(1cm,3cm)
       xshifted 1.5cm
       withcolor "C:3" ;
  fill unitsquare
       xyscaled(1cm,3cm)
       xshifted 3cm
       withcolor "C:3" ;
  ) ;
  draw c ;
  c := c yshifted -5cm ;
  draw c ;
  draw (boundingbox c enlarged .5cm)
       withdashes .25cm
       withpen pencircle scaled 2
       withcolor "C:2" ;
\stopuseMPgraphic

\startcolumnsetsheet[bt][4]

\startsetcolumnset[c=3,r=1]
  \startframed[Fig][height=6gl,foregroundcolor=C:1]
    \strut pagecolumns
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=8]
  \startframed[Fig][height=8gl,offset=3ts]
    \begstrut combine several \TEX\ pages on one physical page\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=1,r=1]
  \startframed[Fig][width=\columnsethspan{2}{2},height=22gl]
    \useMPgraphic{pagecolumns}
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\startuseMPgraphic{oldcolumnsets}
  picture c ;
  c := image(
  draw unitsquare
       xyscaled (1cm,3cm)
       withcolor "C:1" ;
  draw unitsquare
       xyscaled (1cm,3cm)
       xshifted 1.5cm
       withcolor "C:1" ;
  draw unitsquare
       xyscaled (1cm,3cm)
       xshifted 3cm
       withcolor "C:1" ;
  fill unitsquare
       xyscaled (1cm,0.5cm)
       shifted (0,2.5cm)
       withcolor "C:1" ;
  fill unitsquare
       xyscaled (2.5cm,1cm)
       shifted  (1.5cm,1cm)
       withcolor "C:1" ;
  ) ;
  setbounds c to unitsquare xyscaled (4cm,3cm) ;
  draw c ;
  draw (boundingbox c enlarged .5cm)
       withdashes .25cm
       withpen pencircle scaled 2
       withcolor "C:2" ;
  c := image(
  fill unitsquare
       xyscaled(1cm,3cm)
       withcolor "C:3" ;
  fill unitsquare
       xyscaled (1cm,3cm)
       xshifted 1.5cm
       withcolor "C:3" ;
  fill unitsquare
       xyscaled (1cm,3cm)
       xshifted 3cm
       withcolor "C:3" ;
  fill unitsquare
       xyscaled (1cm,0.5cm)
       shifted  (  0,2.5cm)
       withcolor "C:1" ;
  fill unitsquare
       xyscaled (2.5cm,1cm)
       shifted  (1.5cm,1cm)
       withcolor "C:1" ;
  ) yshifted -5cm ;
  draw c ;
  draw (boundingbox c enlarged .5cm)
       withdashes .25cm
       withpen pencircle scaled 2
       withcolor "C:2" ;
\stopuseMPgraphic

\startcolumnsetsheet[bt][5]

\startsetcolumnset[c=1,r=1]
  \startframed[Fig][foregroundcolor=C:1]
    \strut old columnsets
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=1,r=8]
  \startframed[Fig][height=8gl]
    work on the grid, keep control of all the occupied slots
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=1,r=17]
  \startframed[Fig]
    add to remaining slots with vsplit
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=1]
  \startframed[Fig][width=\columnsethspan{2}{2},height=22gl]
    \useMPgraphic{oldcolumnsets}
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\stopcolumnset

\startcolumnset[bt]
\SetTitle{things to consider}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=3,r=17,ntop=1]
  \startframed[Fig][offset=2ts]
    \begstrut can we split columns evenly?\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=1,nbottom=1]
  \startframed[Fig][backgroundcolor=C:3]
    \null
  \stopframed
\stopsetcolumnset

\reservecolumnset[c=1,r=19,nr=4]

\stopcolumnsetsheet

\dorecurse{8}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{things to consider}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=1,r=1,nbottom=1]
  \startframed[Fig]
    \begstrut can we balance the last page?\endstrut
  \stopframed
\stopsetcolumnset

\reservecolumnset[c=1,r=18,nr=5]
\reservecolumnset[c=2,r=18,nr=5]

\stopcolumnsetsheet

\dorecurse{7}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{things to consider}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=1,r=1,nbottom=1]
  \startframed[Fig]
    \begstrut can we balance the last page?\endstrut
  \stopframed
\stopsetcolumnset

\reservecolumnset[c=1,r=18,nr=5]
\reservecolumnset[c=2,r=17,nr=6]

\stopcolumnsetsheet

\dorecurse{7}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{things to consider}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=2,r=8,ntop=1,nbottom=1]
  \startframed[Fig][height=8gl,offset=3ts]
    \begstrut can we handle figures, tables and other floats?\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=1,r=1,nbottom=1]
  \startframed[Fig]
    \startMPcode
      draw fullcircle scaled 2cm
      withpen pencircle scaled 3
      withcolor "C:2" ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=1,r=17,ntop=1]
  \startframed[Fig]
    \startMPcode
      draw fullsquare scaled 2cm
      withpen pencircle scaled 3
      withcolor "C:2" ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=17,ntop=1]
  \startframed[Fig]
    \startMPcode
      draw fulltriangle scaled 2cm
      withpen pencircle scaled 3
      withcolor "C:2" ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\startcolumnsetsheet[bt][2]

\startsetcolumnset[c=1,r=1,nbottom=1]
  \startframed[Fig][offset=3ts]
    \begstrut even spanning several columns?\endstrut
  \stopframed
\stopsetcolumnset

\startbuffer
\setupTABLE[frame=off,offset=overlay]
\setupTABLE[r][height=\dimexpr\paperheight/21\relax]
\setupTABLE[r][align={middle,lohi}]
\setupTABLE[c][odd] [width=\dimexpr\paperwidth/17\relax]
\setupTABLE[c][even][width=\dimexpr\paperwidth*3/34\relax]
\setupTABLE[odd] [even][background=color,backgroundcolor=C:2]
\setupTABLE[odd] [odd] [background=color,backgroundcolor=C:3]
\setupTABLE[even][odd] [background=color,backgroundcolor=C:2]
\setupTABLE[even][even][background=color,backgroundcolor=C:3]

\startluacode
local ln = math.log
local nr = 0
local lnnr = 0
context.bTABLE()
for i = 1, 10 do
  context.bTR()
  for j = 0, 3 do
    nr = j/10 + i/1000
    lnnr = ln(nr)
    context.bTD()
    context(string.format("%0.3f", nr))
    context.eTD()
    if lnnr < 0 then
      context.bTD()
      context.m(string.format("%0.6f", -lnnr))
      context.eTD()
    else
      context.bTD()
      context.m(string.format("%0.6f", lnnr))
      context.eTD()
    end
  end
  context.eTR()
end
context.eTABLE()
\stopluacode
\stopbuffer

\startsetcolumnset[c=2,r=8,ntop=1]
  \startframed
    [Fig]
    [height=15gl,
     width=\columnsethspan{2}{2},
     backgroundcolor=,
     foregroundstyle=\switchtobodyfont[kurier,12pt],
     foregroundcolor=C:4]
    \getbuffer
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\dorecurse{10}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{things to consider}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=2,r=1,nbottom=1]
  \startframed[Fig][offset=2ts]
    \begstrut where to\crlf put footnotes?\endstrut
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\dorecurse{8}{Footnote\footnote{bar} \samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{things to consider}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=1,r=1,nbottom=1]
  \startframed[Fig][height=2gl,width=\columnsethspan{1}{2}]
    \begstrut can we handle headers \unknown \endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=21,ntop=1]
  \startframed[Fig][height=2gl,width=\columnsethspan{2}{1}]
    \begstrut \unknown\ and footers? \endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=8,ntop=7,nbottom=7]
  \startframed[Fig][height=8gl,offset=2ts]
    \begstrut what about adding content to the margin?\endstrut
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\dorecurse{5}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{things to consider}

\showgrid

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=3,r=6]
  \startframed[Fig][height=2gl]
    \begstrut can\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=9]
  \startframed[Fig][height=2gl]
    \begstrut we\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=12]
  \startframed[Fig][height=2gl]
    \begstrut work\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=15]
  \startframed[Fig][height=2gl]
    \begstrut on\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=18]
  \startframed[Fig][height=2gl]
    \begstrut the\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=21]
  \startframed[Fig][height=2gl]
    \begstrut grid?\endstrut
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\dorecurse{8}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{new columnsets}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=1,r=1,nbottom=1]
  \startframed[Fig][offset=2ts]
    \begstrut always work on the grid\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=17,ntop=1]
  \startframed[Fig][width=\columnsethspan{2}{2}]
    \begstrut place content like images first, make pages out of remaining slots\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=1,nbottom=1]
  \startframed[Fig]
    \begstrut internally work\crlf on spreads\endstrut
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\startcolumnsetsheet[bt][2]

\startsetcolumnset[c=1,r=1,nbottom=1]
  \startframed[Fig]
    \begstrut first assumption:\crlf keep it simple!\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=8,ntop=1,nbottom=1]
  \startframed[Fig][height=8gl,offset=2ts]
    \begstrut we don't know where we are at the moment\endstrut
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=17,ntop=1]
  \startframed[Fig]
    \begstrut more features added during work\endstrut
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\dorecurse{13}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{define the columnset}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=3,r=1]
  \startframed[Fig][height=3gl]
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=7,nbottom=1]
  \startframed[Fig][height=3gl]
  \stopframed
\stopsetcolumnset

\reservecolumnset[c=2,r=7,nr=4]

\stopcolumnsetsheet

\starttyping
\definecolumnset
  [bt]
  [n=3,
   limit=4,
   page=right,
   distance=0.5cm,
   % setups=balance:bt,
   % align={defaultwidows,defaultclubs,defaultbroken},
   ]
\stoptyping

\dorecurse{7}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{define the columnset}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=1,r=1,nbottom=1]
  \startframed[Fig][height=10gl]
    page=right
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=1,r=13,ntop=1]
  \startframed[Fig][height=10gl]
    page=
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=1,nbottom=1]
  \startframed[Fig][height=10gl,width=\columnsethspan{2}{2}]
    \startMPcode
      numeric u ; u := .75cm ;
      path co ;
      co := unitsquare
            xyscaled (u,5u) ;
      picture pg[] ;
      pg[3] := image(
        fill co withcolor "C:2" ;
        fill co xshifted 1.5u withcolor "C:2" ;
        fill co xshifted   3u withcolor "C:2" ;
        label("4", centerofmass co) withcolor "C:4" ;
        label("5", centerofmass co xshifted 1.5u) withcolor "C:4" ;
        label("6", centerofmass co xshifted   3u) withcolor "C:4" ;
      ) ;
      pg[4] := image(
        draw pg[3] ;
        draw boundingbox pg[3]
             enlarged .5u
             withpen pencircle scaled 2
             withcolor "C:2" ;
        draw boundingbox pg[3]
             enlarged .5u
             xshifted (-bbwidth pg[3] - u)
             withpen pencircle scaled 2
             withcolor "C:2" ;
      ) ;
      draw pg[4] ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=13,ntop=1]
  \startframed[Fig][height=10gl,width=\columnsethspan{2}{2}]
   \startMPcode
      numeric u ; u := .75cm ;
      path co ;
      co := unitsquare
            xyscaled (u,5u) ;
      picture pg[] ;
      pg[1] := image(
        fill co withcolor "C:2" ;
        fill co xshifted 1.5u withcolor "C:2" ;
        fill co xshifted   3u withcolor "C:2" ;
        label("1", centerofmass co) withcolor "C:4" ;
        label("2", centerofmass co xshifted 1.5u) withcolor "C:4" ;
        label("3", centerofmass co xshifted   3u) withcolor "C:4" ;
      ) ;
      pg[2] := image(
        draw pg[1] ;
        draw boundingbox pg[1]
             enlarged .5u
             withpen pencircle scaled 2
             withcolor "C:2" ;
      ) ;
      draw pg[2] ;
      pg[3] := image(
        fill co withcolor "C:2" ;
        fill co xshifted 1.5u withcolor "C:2" ;
        fill co xshifted   3u withcolor "C:2" ;
        label("4", centerofmass co) withcolor "C:4" ;
        label("5", centerofmass co xshifted 1.5u) withcolor "C:4" ;
        label("6", centerofmass co xshifted   3u) withcolor "C:4" ;
      ) ;
      pg[4] := image(
        draw pg[3] ;
        draw boundingbox pg[3]
             enlarged .5u
             withpen pencircle scaled 2
             withcolor "C:2" ;
      ) ;
      draw pg[4] xshifted (bbwidth pg[2] - 2bp) ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\stopcolumnset

\startcolumnset[bt]
\SetTitle{define the columnset}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=1,r=1,nbottom=1]
  \startframed[Fig][height=10gl]
    singlesided
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=1,r=13,ntop=1]
  \startframed[Fig][height=10gl]
    doublesided
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=1,nbottom=1]
  \startframed[Fig][height=10gl,width=\columnsethspan{2}{2}]
   \startMPcode
      numeric u ; u := .75cm ;
      path co ;
      co := unitsquare
            xyscaled (u,5u) ;
      picture pg[] ;
      pg[1] := image(
        fill co withcolor "C:2" ;
        fill co xshifted 1.5u withcolor "C:2" ;
        fill co xshifted   3u withcolor "C:2" ;
        label("1", centerofmass co) withcolor "C:4" ;
        label("2", centerofmass co xshifted 1.5u) withcolor "C:4" ;
        label("3", centerofmass co xshifted   3u) withcolor "C:4" ;
      ) ;
      pg[2] := image(
        draw pg[1] ;
        draw boundingbox pg[1]
             enlarged .5u
             withpen pencircle scaled 2
             withcolor "C:2" ;
      ) ;
      draw pg[2] ;
      pg[3] := image(
        fill co withcolor "C:2" ;
        fill co xshifted 1.5u withcolor "C:2" ;
        fill co xshifted   3u withcolor "C:2" ;
        label("1", centerofmass co) withcolor "C:4" ;
        label("2", centerofmass co xshifted 1.5u) withcolor "C:4" ;
        label("3", centerofmass co xshifted   3u) withcolor "C:4" ;
      ) ;
      pg[4] := image(
        draw pg[3] ;
        draw boundingbox pg[3]
             enlarged .5u
             withpen pencircle scaled 2
             withcolor "C:2" ;
      ) ;
      draw pg[4] xshifted (bbwidth pg[2] - 2bp) ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=13,ntop=1]
  \startframed[Fig][height=10gl,width=\columnsethspan{2}{2}]
   \startMPcode
      numeric u ; u := .75cm ;
      path co ;
      co := unitsquare
            xyscaled (u,5u) ;
      picture pg[] ;
      pg[1] := image(
        fill co withcolor "C:2" ;
        fill co xshifted 1.5u withcolor "C:2" ;
        fill co xshifted   3u withcolor "C:2" ;
        label("1", centerofmass co) withcolor "C:4" ;
        label("2", centerofmass co xshifted 1.5u) withcolor "C:4" ;
        label("3", centerofmass co xshifted   3u) withcolor "C:4" ;
      ) ;
      pg[2] := image(
        draw pg[1] ;
        draw boundingbox pg[1]
             enlarged .5u
             withpen pencircle scaled 2
             withcolor "C:2" ;
      ) ;
      draw pg[2] ;
      pg[3] := image(
        fill co withcolor "C:2" ;
        fill co xshifted 1.5u withcolor "C:2" ;
        fill co xshifted   3u withcolor "C:2" ;
        label("4", centerofmass co) withcolor "C:4" ;
        label("5", centerofmass co xshifted 1.5u) withcolor "C:4" ;
        label("6", centerofmass co xshifted   3u) withcolor "C:4" ;
      ) ;
      pg[4] := image(
        draw pg[3] ;
        draw boundingbox pg[3]
             enlarged .5u
             withpen pencircle scaled 2
             withcolor "C:2" ;
      ) ;
      draw pg[4] xshifted (bbwidth pg[2] - 2bp) ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\stopcolumnset

\startcolumnset[bt]
\SetTitle{use the columnset}

\starttyping
\startcolumnset[bt]
\stoptyping

\dorecurse{10}{\samplefile{knuthmath}\par}

\starttyping
\stopcolumnset
\stoptyping

\stopcolumnset

\startcolumnset[bt]
\SetTitle{placing content in fixed slots}

\startbuffer
\startcolumnsetsheet[bt][1]
\startsetcolumnset
  [c=3,r=8,ntop=1,nbottom=1]
  \startframed[Fig][height=8gl]
    \startMPcode
      draw fullcircle
      xsized 3cm
      withpen pencircle scaled 3
      withcolor "C:2" ;
    \stopMPcode
  \stopframed
\stopsetcolumnset
\stopcolumnsetsheet
\stopbuffer

\getbuffer
\typebuffer


\startcolumnsetsheet[bt][2]

\startsetcolumnset[c=1,r=8,ntop=1,nbottom=1]
  \startframed[Fig][height=8gl]
    \startMPcode
      draw function(1,"x","1.5*sin(x)",0,2*pi,0.01)
      xsized 5cm
      withpen pencircle scaled 3
      withcolor "C:2" ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=1,nbottom=1]
  \startframed[Fig]
    \startMPcode
      draw function(1,"x","3/(1 + x*x)",-4,4,0.01)
      xsized 5cm
      withpen pencircle scaled 3
      withcolor "C:2" ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=2,r=17,ntop=1]
  \startframed[Fig]
    \startMPcode
      draw function(1,"x","1.25*cos(x)",0,2*pi,0.01)
      xsized 5cm
      withpen pencircle scaled 3
      withcolor "C:2" ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=8,ntop=1,nbottom=1]
  \startframed[Fig][height=8gl]
    \startMPcode
      draw fullcircle
      xsized 3cm
      withpen pencircle scaled 3
      withcolor "C:2" ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\startcolumnsetsheet[bt][3]

\startsetcolumnset[c=2,r=1]
  \startframed[Fig][width=\columnsethspan{2}{2},height=22gl]
    \startMPcode
      draw function(1,"x*cos(x)","x*sin(x)",0,8*pi,0.01)
      ysized 10cm
      withpen pencircle scaled 3
      withcolor "C:2" ;
    \stopMPcode
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\dorecurse{16}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{slots get reserved or occupied}

\startbuffer
\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=3,r=1,nbottom=1]
  \startframed[Fig]
    this content has been placed first
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=6,ntop=1,nbottom=1]
  \startframed[Fig]
    this content will not show up
  \stopframed
\stopsetcolumnset

\reservecolumnset[c=2,r=1,nr=11]

\stopcolumnsetsheet
\stopbuffer

\typebuffer
\getbuffer

\dorecurse{5}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{work on sheet or spread}

\startbuffer
\startcolumnsetspreadsheets[bt][1]

\startsetcolumnset[c=5,r=17,ntop=1]
  \startframed[Fig]
    where will this content end up?
  \stopframed
\stopsetcolumnset

\stopcolumnsetspreadsheets
\stopbuffer

\typebuffer
\getbuffer

\dorecurse{19}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{how about footnotes?}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=3,r=1,nbottom=1]
  \startframed[Fig]
    works fine on simple page setups but \unknown
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\startcolumnsetsheet[bt][2]

\startsetcolumnset[c=1,r=7,ntop=1,nbottom=1]
  \startframed[Fig][width=\columnsethspan{1}{3}]
    \startMPcode
      numeric u ; u := 1cm ;
      for i = 1 upto 7 :
        draw fullcircle
             scaled 2u
             randomrotatedcontrols uniformdeviate(90)
             xshifted i*3u
             withpen pencircle scaled 2
             withcolor "C:2" ;
      endfor
    \stopMPcode
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=17,ntop=1]
  \startframed[Fig]
    \unknown remember we work in slots!
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\startbuffer
\setupnotes
  [width=\columnsetspanwidth{1}]
\stopbuffer

\typebuffer
\getbuffer

\dorecurse{5}{%
  \samplefile{knuthmath}\footnote{This is a footnote.}\par
  \samplefile{knuthmath}\footnote{This is in fact an even longer footnote. Maybe it will run over two rows?}\par
}

\dorecurse{1}{\samplefile{knuthmath}\par}

\stopcolumnset

\startbuffer[bts-a]
\definesubcolumnset[bt][bts][*]
\stopbuffer

\getbuffer[bts-a]

\startbuffer[bts-b]
\dorecurse{7}{%
  \startsubcolumnset[bts]
    \samplefile{knuthmath}
  \stopsubcolumnset
  \balancesubcolumnsets
}
\stopbuffer

\startcolumnset[bt]
\SetTitle{can we balance the columns?}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=3,r=1,nbottom=1]
  \startframed[Fig]
    yes, we can!
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\startsubcolumnset[bts]
\typebuffer[bts-a,bts-b]
\stopsubcolumnset

\flushsubcolumnsets

\getbuffer[bts-b]

\stopcolumnset

\startbuffer[subcols-definitions]
\definesubcolumnset[bt][bts-one]  [1,4]
\definesubcolumnset[bt][bts-two]  [2,5]
\definesubcolumnset[bt][bts-three][3,6]
\stopbuffer

\getbuffer[subcols-definitions]

\startbuffer[subcols-content]

\startsubcolumnset[bts-one]
  \dorecurse{2}{\samplefile{tufte}\par}
\stopsubcolumnset

\startsubcolumnset[bts-two]
  \dorecurse{5}{\italic \samplefile{ward}\par}
\stopsubcolumnset

\startsubcolumnset[bts-three]
  \setupcolors[textcolor=C:3]
  \dorecurse{7}{\samplefile{knuthmath}\par}
\stopsubcolumnset

\flushsubcolumnsets

\stopbuffer

\startcolumnset[bt]
\SetTitle{wait, subcolumnsets?}

\startsubcolumnset[bts-one]
\typebuffer[subcols-definitions,subcols-content]
\stopsubcolumnset

\flushsubcolumnsets

\getbuffer[subcols-content]

\stopcolumnset


\startcolumnset[bt]
\SetTitle{it is a challenge to stay on grid}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=2,r=8,ntop=1,nbottom=1]
  \startframed[Fig][height=8gl,offset=2ts]
    both inline and displayed math are difficult to handle
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\samplefile{math-knuth-dt}\par

\dorecurse{1}{%
  \samplefile{knuthmath}\par
  \startformula
    f(x) = f(a) + \int __ a ^^ x f'(t) \dd t
  \stopformula
  }

  \samplefile{knuthmath}\par

  \startformula
    f(x)
    \alignhere
    = f(a) + \int __ a ^^ x f'(t) \dd t
    \breakhere
    = \frac {\dd}{\dd x}\int_a^x f(t) \dd t
  \stopformula

\dorecurse{1}{\samplefile{knuthmath}\par}

\stopcolumnset

\startcolumnset[bt]
\SetTitle{it is a challenge to stay on grid}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=2,r=8,ntop=1,nbottom=1]
  \startframed[Fig][height=8gl,offset=2ts]
    section heads are typically set in
    a larger font
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\samplefile{knuthmath}\par

\startsection
  [title=this is a section with a title that runs over several lines]

\dorecurse{6}{\samplefile{knuthmath}\par}

\stopsection
\stopcolumnset


\startcolumnset[bt]

\setuphead
  [section]
  [grid=global:tolerant]

\SetTitle{it is a challenge to stay on grid}

\startcolumnsetsheet[bt][1]

\startsetcolumnset[c=2,r=8,ntop=1,nbottom=1]
  \startframed[Fig][height=8gl,offset=2ts]
    section heads are typically set in
    a larger font
  \stopframed
\stopsetcolumnset

\startsetcolumnset[c=3,r=17,ntop=1]
  \startframed[Fig]
    see the details manual for more alternatives
  \stopframed
\stopsetcolumnset

\stopcolumnsetsheet

\samplefile{knuthmath}\par

\startsection
  [title=this is a section with a title that runs over several lines]

\dorecurse{6}{\samplefile{knuthmath}\par}

\stopsection
\stopcolumnset

\stoptext

