3

This issue is only for tex4ht. I want to move all my code formatting away from using a Javascript package I am now using, and to use listings instead.

As is known, listings package works as is with PDF. But many of its options do not work with tex4ht such as background color and putting frame around.

I looked at some related questions (some links below), but many are old and each one gives different approach and I am lost as what to do now.

What is the current best practice in tex4ht to at least support the background color and the frame to show up with text4ht? I assume I need special .css. Should I make .4ht? How to do all this is what I need help with.

Here is a MWE

\documentclass[12pt]{article}
\usepackage{parskip}
\usepackage{listings,xcolor}
\definecolor{bg}{RGB}{255,255,226}

\lstset{
basicstyle=\small,
breaklines=true,
backgroundcolor=\color{bg},
language=,
frame = single,
frameround=tttt,
rulecolor=\color{gray}
}

\begin{document}
This is my listing

\begin{minipage}{\textwidth}
\begin{lstlisting}                       
Methods for first order ODEs:
--- Trying classification methods ---
trying a quadrature
<- quadrature successful
\end{lstlisting}
\end{minipage}

\end{document}

Compiled with lualatex gives

enter image description here

Compiled with make4ht -ulm default foo.tex "mathjax,htm" gives this web page

enter image description here

The following are related questions. I just do not know how to use solutions given so far to solve the above, if possible.

References

Simple Color Environment for htlatex

Verbatim environment with background color (PDFLaTeX and tex4ht)

Using TL 2021 on Linux

Update 3/24/2022

Using updated answer for making it work with \lstinputlisting works, but I get lots of extra nbsp at end of each line. Here is a MWE

\documentclass[12pt]{book}

\usepackage{parskip}
\usepackage{listings,xcolor}
\definecolor{bg}{RGB}{255,255,226}

\lstset{
basicstyle=\ttfamily\small,
breaklines=true,
backgroundcolor=\color{bg},
language=,
frame = single,
frameround=tttt,
rulecolor=\color{gray}
}

\begin{document}    

This is my file:

\lstinputlisting{foo2.tex}
\end{document}

Compiled using

 make4ht  -ulm default -c ./my.cfg foo2.tex "mathjax,htm"

Where my.cfg is the updated .cfg in the answer. This gives the HTML

enter image description here

1 Answer 1

2

Try this configuration file:

\Preamble{xhtml}
\makeatletter
\lst@AddToHook{Init}{%
\bgroup%
  % handle backround color and border in listings
  \ifx\lst@fillcolor\@empty\else%
    \lst@fillcolor%
    \extractcolorspec{.}\html@fillcolor%
    \expandafter\convertcolorspec\html@fillcolor{HTML}\html@fillcolor%
    \Css{\#listing-\listingN{background-color:\#\html@fillcolor;}}%
  \fi%
  \ifx\lst@rulecolor\@empty\else%
    \lst@rulecolor%
    \extractcolorspec{.}\html@rulecolor%
    \expandafter\convertcolorspec\html@rulecolor{HTML}\html@rulecolor%
    \Css{\#listing-\listingN{border: 1px solid \#\html@rulecolor;}}%
  \fi%
\egroup%
}
\Configure{lstinputlisting}
   {\ifvmode \IgnorePar\fi \EndP
    \HCode{<!--l. \the\inputlineno-->}%
    \gHAdvance\listingN by 1%
    \HCode{<pre class="lstinputlisting" id="listing-\listingN">}%
    \bgroup\ttfamily%\special{t4ht@(}%
       \Configure{listings}%
         {{\everypar{}\leavevmode}}%
         {{\everypar{}\leavevmode}}%
         {\HCode{\Hnewline<span class="label">}}%
         {\HCode{</span>}}%
   }
   {%\special{t4ht@)}
    \egroup%
    \ifvmode \IgnorePar\fi \EndP  \HCode{</pre>}\par}%
\makeatother
\begin{document}
\EndPreamble

It uses a hook that Listings executes at beginning of each environment, and extracts colors of current background and border, if they are set. It then uses CSS to assign these colors to the current listings environment.

This is the result:

enter image description here

10
  • Thanks Michal. Nice job. Fyi, I added padding-left:3.0pt; to your \Css{\#listing-\listingN{padding-left:3.0pt; border: 1px solid \#\html@rulecolor;}}% line as I noticed the text was too close to the left edge border in Brave browser. This is great. I can now get rid of all the javascript library I was using as it was slowing down loading all the pages.
    – Nasser
    Mar 20 at 14:04
  • Hello Michal. What changes is needed to make your code work with \lstinputlisting{file_name} instead of just \begin{lstlisting} code \end{lstlisting}? Currently using \lstinputlisting{file_name} the code comes out without the background coloring and the Frame. I hope it is not too much change. I could not figure what I need to do. does it need separate hook? If you like me to make separate question on this please let me know. Thanks.
    – Nasser
    Mar 24 at 7:56
  • @Nasser try the updated answer
    – michal.h21
    Mar 24 at 10:42
  • Thanks Michal. It works OK, except for all those nbsp at end of lines. I wonder why they show up? I compile the files on Linux, but these files can reside on windows disk (I use Linux subsystem on windows). I wonder if I need to run dos2unix each time? I see your new code adds nbsp there? What is the correct way to handle this? Please see updated MWE showing the output.
    – Nasser
    Mar 24 at 13:57
  • Fyi, I removed \:nbsp from your code and this seems to have resolved the issue so far for this example. But I do not know yet what side-effect will somewhere else. I am sure you added this for some good reason.
    – Nasser
    Mar 24 at 14:20

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.