Search code examples
pdf-generationdocbookdblatex

How to get colored pdf text output with docbook via dblatex?


I need to output some hex-data with some bytes highlighted with colored background. Bytes in the data that represent red color values should have red background. That's why I chose

<emphasis role="red"></emphasis>

to be the tag of choice.

HTML output

For HTML output I use

  <xsl:param name="emphasis.propagates.style" select="1"/>

in the xsl file and

.red {
  background-color: red;
}

in the stylesheet. This works flawlessly.

PDF output

For PDF output I use dblatex and tried (file fo.xsl)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:d="http://docbook.org/ns/docbook"
    xmlns="http://www.w3.org/TR/xhtml1/transitional"
    version="1.0">

  <xsl:param name="draft.mode">no</xsl:param>
  <xsl:param name="imagedata.default.scale">maxwidth=12cm</xsl:param>

  <xsl:template match="emphasis[@role='red']">
    <xsl:param name="content">
      <xsl:apply-templates/>
    </xsl:param>
    <xsl:text>{\color{red}</xsl:text>
    <xsl:copy-of select="$content"/>
    <xsl:text>}</xsl:text>
  </xsl:template>

</xsl:stylesheet>

And (file test.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
  "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
<article lang="en">
  <title>Test</title>
  <para>
    <screen>0240h: 00 20 <emphasis role="red">00</emphasis> 21</screen>
  </para>
</article>

With the following command to build latex source for checking:

dblatex --backend=pdftex --xslt=xsltproc \
    --input-format=xml --type=tex \
    --xsl-user=fo.xsl --verbose --output=test.tex test.xml

This does not work. Emphasised text is still italic, as shown in an excerpt from the resulting test.tex:

\begin{lstlisting}[firstnumber=1,escapeinside={<t>}{</t>},moredelim={**[is][\bfseries]{<b>}{</b>}},moredelim={**[is][\itshape]{<i>}{</i>}},]
0240h: 00 20 <i>00</i> 21\end{lstlisting}

What am I doing wrong?


Solution

  • As mzjn stated, the screen tag was a problem. After reading the linked resource, the final template that works for me in this case, and produces output similar to the html look, is this:

    <xsl:template match="emphasis[@role='red']" mode="latex.programlisting">
      <xsl:param name="co-tagin" select="'&lt;:'"/>
      <xsl:param name="rnode" select="/"/>
      <xsl:param name="probe" select="0"/>
      <xsl:param name="content">
        <xsl:apply-templates/>
      </xsl:param>
      <xsl:text>&lt;t&gt;\colorbox{red}{</xsl:text>
      <xsl:copy-of select="$content"/>
      <xsl:text>}&lt;/t&gt;</xsl:text>
    </xsl:template>
    

    which leads to the following latex output:

    \begin{lstlisting}[firstnumber=1,escapeinside={<t>}{</t>},moredelim={**[is][\bfseries]{<b>}{</b>}},moredelim={**[is][\itshape]{<i>}{</i>}},]
    0240h: 00 20 <t>\colorbox{red}{00}</t> 21\end{lstlisting}
    

    Maybe it would be better to add another moredelim to lstlisting than using the escapeinside, but with this solution, this question is answered for me.