I am using htlatex to translate from LaTeX to HTML. I've created a \ScriptEnv environment for code as suggested by this answer:

htlatex inserting environment in verbatim

It's working well, but it is being rather too literate -- there is no conversion to HTML entities, so any code with "<"'s is being handled badly.

Is there any way to stop this?


In my case, the situation is worse because some of my output looks like an HTML tag with open and close <>.

(isubclasses OntologyBook)
;;=> #{#<OWLClassImpl <bc33d2c3-7ecb413c-bb04-83af2295c465#walk_through#TakeWing>>}

another edit:

of you don't want to postprocess html, you may use different mean to achieve verbatim environment. For example with fancyvrb package:

{\IgnorePar\EndP\StartSuppressPage\HCode{<pre><code class="clojure">}\HtmlParOff\NoFonts\Verbatim}

and sample of resulting html:

tudin, felis odio placerat quam, ac pulvinar elit purus eget enim. Nunc vitae
tortor. Proin tempus nibh sit amet nisl. Vivamus quis tortor vitae risus porta

   <pre><code class="clojure">
<!--l. 15--><p class="indent" >   Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor
lorem non justo. Nam lacus libero, pretium at, lobortis v


it seems that in this case lua filter for make4ht must be used. Use this .mk4 file:

local filter= require "make4ht-filter"
local entities = function(s)
  return '<code class="clojure">' .. s:gsub('<code class="clojure">(.-)</code>', function(a) 
    local escapes = {["<"]="lt",[">"]="gt",["&"]="amp"}
    return a:gsub("([%<%>%&])", function(x)
      return "&"..(escapes[x] or x) .. ";"
   end) .. '</code>'
local process = filter(entities, uj)
if mode == "draft" then
  Make:match("html$", process)
  Make:match("html$", process)

filter entities is defined, which process content of <code class="clojure"> element and convert all <,> and & to html entities:

<pre><code class="clojure"> 
(defclass Pizza
  :super Thing)
  (isubclasses OntologyBook)
  ;;=&gt; #{#&lt;OWLClassImpl &lt;bc33d2c3-7ecb413c-bb04-83af2295c465#walk_through#TakeWing&gt;&gt;}

Old version:

Content of \ScriptEnv is exported verbatim, without any processing, so it seems that this can't be solved at the LaTeX side. But you can post-process html with some tool to replace these prohibited characters with html entities. Some Lua script could be used for that, but easiest way is to use tidy command:

tidy  -m  -utf8 -q -i filename.html


<pre><code class="clojure"> 
  a > b & c


<code class="clojure"> 
  a &gt; b &amp; c

with make4ht you can automatize this. make build file filename.mk4:

if mode == "draft" then
  Make:match("html$", "tidy -m -utf8 -q -i ${filename}")

with this build file, you will be able to quickly view your file with

make4ht -m draft -c yourconfigfile.cfg filename

and tidy will be used only for final build. Other feature is that all output html files will be processed (this is useful if you split sections or chapters to standalone html files)

  • Other environments do this escaping though, so can't understand why it's not possible with ScriptEnv. Unfortunately, the tidy solution doesn't work. I've modified the question with an explanation. – Phil Lord Oct 13 '14 at 16:34
  • @PhilLord that's because \ScriptEnv is used for inclusion of javascript, for example and you don't want to escape html entities in javascript – michal.h21 Oct 13 '14 at 18:43
  • Ah. Is there not a better environment which is "verbatim but with no fonts or spans, or indeed anything"? – Phil Lord Oct 14 '14 at 14:27
  • @PhilLord I had some success with fancyvrb package, I wil edit my answer – michal.h21 Oct 14 '14 at 14:53
  • Okay, so I have two options here. I've got a preprocessor step working also using Emacs. I shall think on which to use. Thanks for the help! – Phil Lord Oct 14 '14 at 15:49

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.