2

Background

Using pandoc to convert a Markdown document into ConTeX and would like to frame a section.

Problem

Using \beforesection and \aftersection with \starttextbackground and its stopper only works with start/stop sections, which is unsupported by pandoc.

Code

Here is example code produced by pandoc:

\starttext
    \chapter{Chapter}
    \section{Section 1}
    \subsection{Summary}
    \input tufte
    \section{Section 2}
    \subsection{Summary}
    \input knuth
\stoptext

Question

Without using \startsection and \stopsection, how would you put a frame around an entire section?

  • You can't. It is not possible to determine the end of a section without the \stopsection. – Henri Menke May 14 '17 at 21:37
  • I appreciate your solution to the impossible. :-) – Dave Jarvis May 17 '17 at 19:44
2

Personally, I would favour the pandoc solution proposed in the other answer. It is also possible to implement something (fragile) on the macro level. Whenever a new section or higher level sectioning command begins, check whether we are in a section. You also need to check on \stoptext (unfortunately, you cannot put it in \everystoptext. This leads to weird behaviour.)

\definetextbackground
  [SectionFrame]
  [background=,
   framecolor=red,
   corner=round,
   location=paragraph,
   before={\blank[2*big]}]

\newconditional\insection
\setfalse\insection

\define\checksection{%
  \ifconditional\insection
    \stoptextbackground
  \fi
  \setfalse\insection
}

\setuphead
  [section]
  [before={%
      \checksection
      \starttextbackground[SectionFrame]
      \settrue\insection
    }]

\setuphead
  [subsection]
  [before=]

\setuphead
  [chapter]
  [before=\checksection]

\define\stoptext{%
  \checksection
  \csname clf_stoptext\endcsname
}

\starttext
\chapter{Chapter}
\section{Section 1}
\subsection{Summary}
\input tufte
\section{Section 2}
\subsection{Summary}
\input knuth
\stoptext
5

You can use a pandoc filter to create \startsection and \stopsection. Filters are described in the pandoc manual.

I modified the theorem.py sample filter to create a section for bibliographies as follows. Perhaps it will serve as a first step to solving your problem.

#!/usr/bin/env python

"""
Pandoc filter to convert divs with class="refs" to ConTeXt
\startBibliography-\stopBibliography. Based on theorem.py
"""

from pandocfilters import toJSONFilter, RawBlock, Div

def context(x):
    return RawBlock('context', x)

def reference_block(key, value, format, meta):
    if key == 'Div':
        [[ident, classes, kvs], contents] = value
        if ident == "refs":
            if format == "context":
            return([context('\\startBibliography[reference=refs,title=Sources]')] + contents + [context('\\stopBibliography')])

if __name__ == "__main__":
    toJSONFilter(reference_block)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.