Added support for boxed and numbered exercises.
1 parent a24207d commit 4f45f648840c3b3623875cf103235d9064125a2e
Nigel Stanger authored on 4 Sep 2013
Showing 3 changed files
View
21
modules/cross-references.xml
</common>
</template>
<!-- Exercise reference. -->
<template name="exercise-reference" match="exercise" mode="reference">
<common formats="/latex/xelatex/">
<xsl:text>Exercise~\ref*{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<xsl:text disable-output-escaping="yes">Exercise&amp;nbsp;</xsl:text>
<xsl:choose>
<xsl:when test="$showanswers='yes'">
<xsl:number count="exercise" level="any" format="1" />
</xsl:when>
<xsl:otherwise>
<xsl:number count="exercise[not(ancestor::answer) and not(ancestor::omit) and not(ancestor::comment)]" level="any" format="1" />
</xsl:otherwise>
</xsl:choose>
</common>
</template>
<!--
Common template for item page references, as they're identical for all items. Standalone page references (i.e., not to a specific item) are dealt with below.
-->
<template name="item-page-reference" match="section|figure|table" mode="page-reference">
View
184
modules/q-and-a.xml
<?xml version="1.0" encoding="utf-8"?>
 
<!--
Elements for generating question and sample answer text in the likes of tutorial and lab exercises.
Elements for generating question and sample answer text in the likes of tutorial and lab exercises.
-->
 
<stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
 
<!--
Question text. This only exists so that we can ensure that appropriate <p> tags are correctly inserted. If this template didn't exist, showing the answers would generate invalid HTML. The answers are enclosed in a <div class="answer">...</div>. Without this template, the answer markup ends up embedded inside <p>...</p>, which is invalid.
-->
<template name="question" match="question">
<common formats="/latex/xelatex/"><xsl:apply-templates /></common>
<common formats="/html/xhtml/">
<xsl:choose>
<!--
Check whether there are actual paragraphs inside the question. If so, let them worry about inserting the <p> tags.
-->
<xsl:when test="count(paragraph|para|p|question|answer|code-block) != 0">
<xsl:apply-templates />
</xsl:when>
<!-- Otherwise, insert <p> tags surrounding the question content. -->
<xsl:otherwise>
<p><xsl:apply-templates /></p>
</xsl:otherwise>
</xsl:choose>
</common>
</template>
<!--
Question text. This only exists so that we can ensure that appropriate <p> tags are correctly inserted. If this template didn't exist, showing the answers would generate invalid HTML. The answers are enclosed in a <div class="answer">...</div>. Without this template, the answer markup ends up embedded inside <p>...</p>, which is invalid.
-->
<template name="question" match="question">
<common formats="/latex/xelatex/"><xsl:apply-templates /></common>
<common formats="/html/xhtml/">
<xsl:choose>
<!--
Check whether there are actual paragraphs inside the question. If so, let them worry about inserting the <p> tags.
-->
<xsl:when test="count(paragraph|para|p|question|answer|code-block) != 0">
<xsl:apply-templates />
</xsl:when>
<!-- Otherwise, insert <p> tags surrounding the question content. -->
<xsl:otherwise>
<p><xsl:apply-templates /></p>
</xsl:otherwise>
</xsl:choose>
</common>
</template>
 
<!--
Sample answers, which may optionally be excluded from the output. This can be done globally for all answers by setting the style sheet parameter $showanswers to "no", or it can be done locally on a question-by-question basis by setting the @hide attribute of the element to "yes".
@hide: If "yes", don't include this answer in the output stream. The default is to include the answer.
-->
<template name="answer" match="answer">
<common formats="/latex/xelatex/">
<!--
It's nice to weed out the sample answer markup at the XSLT processing stage to make LaTeX run faster. Note that \showanswers still needs to be called so that LaTeX can format chapter headings like "Answers for ..."
-->
<xsl:if test="$showanswers='yes'">
<xsl:text>\begin{answer}</xsl:text>
<xsl:call-template name="newline-internal" />
<xsl:apply-templates />
<xsl:text>\end{answer}</xsl:text>
<xsl:call-template name="newline-internal" />
<xsl:call-template name="newline-internal" />
</xsl:if>
</common>
<common formats="/html/xhtml/">
<xsl:if test="$showanswers='yes'">
<div class="answer">
<xsl:apply-templates />
</div>
</xsl:if>
</common>
</template>
<!--
Match hidden answers and do nothing.
-->
<template name="hidden-answer" match="answer[@hide='yes']" />
<!--
Sample answers, which may optionally be excluded from the output. This can be done globally for all answers by setting the style sheet parameter $showanswers to "no", or it can be done locally on a question-by-question basis by setting the @hide attribute of the element to "yes".
@hide: If "yes", don't include this answer in the output stream. The default is to include the answer.
-->
<template name="answer" match="answer">
<common formats="/latex/xelatex/">
<!--
It's nice to weed out the sample answer markup at the XSLT processing stage to make LaTeX run faster. Note that \showanswers still needs to be called so that LaTeX can format chapter headings like "Answers for ..."
-->
<xsl:if test="$showanswers='yes'">
<xsl:text>\begin{answer}</xsl:text>
<xsl:call-template name="newline-internal" />
<xsl:apply-templates />
<xsl:text>\end{answer}</xsl:text>
<xsl:call-template name="newline-internal" />
<xsl:call-template name="newline-internal" />
</xsl:if>
</common>
<common formats="/html/xhtml/">
<xsl:if test="$showanswers='yes'">
<div class="answer">
<xsl:apply-templates />
</div>
</xsl:if>
</common>
</template>
<!--
Match hidden answers and do nothing.
-->
<template name="hidden-answer" match="answer[@hide='yes']" />
<!--
Generate an "exercise". These are numbered and labelled, and are displayed in a box.
@label
-->
<template name="exercise" match="exercise">
<common formats="/latex/xelatex/">
<xsl:text>\medskip</xsl:text>
<xsl:call-template name="newline-internal" />
<xsl:text>\begin{exercise}</xsl:text>
<xsl:if test="@label">
<xsl:text>\label{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
<xsl:call-template name="newline-internal" />
</xsl:if>
<xsl:apply-templates />
<xsl:call-template name="newline-internal" />
<xsl:text>\end{exercise}</xsl:text>
<xsl:call-template name="newline-internal" />
<xsl:text>\medskip</xsl:text>
</common>
<common formats="/html/xhtml/">
<xsl:if test="@label"><a id="{@label}"></a></xsl:if>
<p style="border: 1px solid black; padding: 0.25em; margin-top: 0.5em; margin-bottom: 0.5em;">
<strong>
<xsl:text>Exercise </xsl:text>
<xsl:number count="exercise" level="any" format="1" />
<xsl:text>: </xsl:text>
</strong>
<xsl:apply-templates />
</p>
</common>
</template>
 
</stylesheet>
View
8
xml2xslt.xsl
\usepackage{pifont}
\usepackage{siunitx} % number and SI unit formatting
\usepackage{listings} % nicely formatted code listings
\usepackage[normalem]{ulem} % fancy underlining, strikeout, etc.
\usepackage{boxedminipage}
 
</xsl-out:text>
 
<xsl-out:call-template name="latex-preamble" />
% up. Other languages will still work, just not quite as quickly.
\lstloadlanguages{Oracle}
\lstset{basicstyle=\ttfamily,basewidth=0.5em,escapeinside={(@}{@)},
showspaces=false,showstringspaces=false}
 
% Environment for worksheet exercises.
\newcounter{exercise}
\setcounter{exercise}{0}
\newenvironment{exercise}%
{\noindent\refstepcounter{exercise}\begin{boxedminipage}{\columnwidth}\textbf{Exercise \theexercise:}}%
{\end{boxedminipage}}
 
</xsl-out:text>
 
<xsl-out:apply-templates select="title" mode="preamble" />