[Tex/LaTex] ConTeXt: Figure scaling to width and text wrapping for Pandoc-generated documents

contextcontext-mkivgraphicspandocwrap

Consider the following Pandoc-generated ConTeXt document:

\setuppapersize [A4][A4]
\setuplayout    [width=middle,  backspace=1.5in, cutspace=1.5in,
                 height=middle, topspace=0.75in, bottomspace=0.75in]

\setuppagenumbering[location={footer,center}]

\setupexternalfigures[
  order={svg,pdf,png,jpg},
  location=global,
  wfactor=fit,
  ]

\definedescription
  [description]
  [headstyle=bold, style=normal, location=hanging, width=4cm]

\starttext
\placefigure[here,nonumber]{Little kitten}{\externalfigure[http://placekitten.com/g/480/300][method=jpg]}

\startdescription{{\externalfigure[http://placekitten.com/g/640/400][method=jpg]}}
  \input bryson
\stopdescription
\stoptext

current output

Question

Little kitten should grow to \textwidth. The other kitten should fit through a width of 4cm with text wrapped around it. Its picture should be flush at the top with the wrapped text.

The kittens may not be touched; only the setup before \starttext may be changed. Furthermore, no references to the image URLs should appear in the preamble for this problem to be solved as generally as possible.

I tried something with \setupexternalfigures and wfactor=fit but it does not seem to work.

Best Answer

Solution

With version: 2012.05.30 11:26 of ConTeXt, \setupexternalfigures[wfactor=fit] has no effect. Hence, the following solution which also keeps in-line figures intact.

This is all really nice, because LaTeX2e has not be found capable (yet) of pulling off a similar feat. In contrast to ConTeXt, float clearing is not automatic in LaTeX2e; instead, one needs to fuddle around with distance measurements.

\setuppapersize [A4][A4]
\setuplayout    [width=middle,  backspace=1.5in, cutspace=1.5in,
                 height=middle, topspace=0.75in, bottomspace=0.75in]

\setuppagenumbering[location={footer,center}]

\setuptolerance[horizontal, tolerant, stretch]

% Keep old definitions
\let\oldplacefigure\placefigure
\let\oldexternalfigure\externalfigure

% For full text-width figures
\def\placefigure[#1]#2#3{%
  \def\externalfigure[##1]{\oldexternalfigure[##1][method=jpg, wfactor=fit]}%
  \oldplacefigure[#1]{#2}{#3}%
  \let\externalfigure\oldexternalfigure% Reset for in-line figures
  }

% For figures with wrapped text
\definedescription[description][
  headstyle=bold,
  style=normal,
  location=hanging,
  width=4cm
  ]

\def\startdescription#1{%
  \def\externalfigure[##1]{\oldexternalfigure[##1][method=jpg, width=4cm]}%
  \oldplacefigure[none,left,high]{}{#1}%
  \let\externalfigure\oldexternalfigure% Reset for in-line figures
  }
\def\stopdescription{\endgraf}

\starttext
  \placefigure[here,nonumber]{Little kitten}{\externalfigure[http://placekitten.com/g/480/300]}

  \startdescription{{\externalfigure[http://placekitten.com/g/640/400]}}
    \input bryson
  \stopdescription
\stoptext

output

Caveat

The cats have a caveat though; When the figure with the description occurs near the end of a page, its wrapped text will fall off the page like in the following example. I distilled a minimum working example out of it and linked it as a follow-up question.

caveat

Newer ConTeXt versions

Eventually, I found how to install ConTeXt Standalone under Debian. I can confirm that \setupexternalfigures[wfactor=fit] is now functional in ConTeXt version: 2013.09.09 19:45. This can significantly reduce the code when, like in the example above, no in-line images are used. However, when in a more general case in-line images are present, [wfactor=fit] cannot be used because it acts also upon these. [wfactor=fit] acting upon in-line images could be considered a ConTeXt bug. Therefore, above solution which also works with older ConTeXt versions, remains the more general solution.

Related Question