1

This is tex4ht only issue.

There is a good answer in How to add frame and background color with listing when used with text4ht That shows how to make tex4ht put frame and background color when using listings package with \lstset{...} and then \begin{lstlisting}

But this solution did not work when using listings styles. A small example will make this clear. This MWE works, since it uses \lstset{...}

\documentclass[12pt]{article}
\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}

\begin{lstlisting}
          This is a test
\end{lstlisting}
\end{document}

Compiled with make4ht -ulm default -a debug -c ./my.cfg foo.tex "mathjax,htm"

Gives this HTML (the .cfg file used above is given at the bottom. It is the same from the answer in the link above)

enter image description here

All is well. But since I have many languages, I need to use \lstdefinestyle to name the style, and then use \lstset with the language name. This way I can use different listing in same latex document, each with named style.

When I did this, now tex4ht no longer adds frame and background color.

Here is the same example above but done using listings style environment.

\documentclass[12pt]{article}
\usepackage{parskip}
\usepackage{listings,xcolor}
\definecolor{bg}{RGB}{255,255,226}
    
\lstdefinestyle{plainText}{%
    basicstyle=\ttfamily\small,
    breaklines=true,
    backgroundcolor=\color{bg},
    rulecolor=\color{gray},
    language=,
    frame=single,
    frameround=tttt,
    aboveskip=12pt,belowskip=12pt
}

\lstnewenvironment{plainTextEnv}{%
  \lstset{style=plainText}}{}

\begin{document}

\begin{plainTextEnv}
This is a test
\end{plainTextEnv}

\end{document}

Compiled using same command gives

enter image description here

So the decorations are not used. I think this is because the .cfg is only looking for lstlisting and lstinputlisting hooks and not for lstnewenvironment

Is it possible to make the .cfg also work for the above setup? Otherwise, I will have to add \ifdefined\HCode around each listing which makes the code hard to read.

Appendix

Here is the .cfg used

%see https://tex.stackexchange.com/questions/637695/how-to-add-frame-and-background-color-with-listing-when-used-with-text4ht
%code thanks to michal.h21 

\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

1 Answer 1

1

TeX4ht supports custom Listings environments, but the default configuration don't add numeric ID to the generated HTML, so the CSS instructions for borders and background color don't take an effect. It should be fixed with this configuration file:

\Preamble{xhtml}

\catcode`\:=11
\makeatletter
    \Configure{lstinline}
   {\HCode{<code class="lstinline">}\NoFonts}
   {\EndNoFonts\HCode{</code>}}
\Css{code.lstinline{font-family:monospace,monospace;}}
\Log:Note{to ensure proper alignments use
   fixed size fonts (see listings.dtx)}
\HAssign\listingN=0
\HAssign\listings:Depth = 0
\Configure{listings}
   {\ifvmode \IgnorePar\fi \EndP
   \gdef\start:LstLn{}%
    {\everypar{}\leavevmode}\HCode{<!--l. \the\inputlineno-->}%
     \ifnum \listings:Depth=0 \HCode{<pre class="listings" id="listing-\listingN">}\else
                              \HCode{<span class="listings-nested" id="listing-\listingN">}\fi
     \gHAdvance\listings:Depth by 1
   }
   {\gHAdvance\listings:Depth by -1
    \ifnum \listings:Depth=0 \HCode{</pre>}\else
                             \HCode{</span>}\fi
   }
   {\start:LstLn%\:nbsp%
   \gdef\start:LstLn{\HCode{\Hnewline}}%
\HCode{<span class="label">}}%
   {\HCode{</span>}}%
\Configure{listings-init}{\ttfamily\special{t4ht@(}}{\special{t4ht@)}}
\ConfigureEnv{lstlisting}
   {\ifvmode \IgnorePar\fi \EndP
    %\gHAdvance\listingN by 1
    \HCode{<!--l. \the\inputlineno-->}%
    \gdef\start:LstLn{%
       \HCode{<pre class="lstlisting" id="listing-\listingN">}%
       \gdef\start:LstLn{\HCode{\Hnewline}}}
    \bgroup%
    % we put the closing </div> here in order to support floating listings
    % https://tex.stackexchange.com/a/615703/2891
    \pend:def\lst@DeInit{\ifvmode\IgnorePar\fi\EndP\HCode{</pre>}}%
       \Configure{listings}
         {{\everypar{}\leavevmode}}
         {{\everypar{}\leavevmode}}
         {\start:LstLn \HCode{<span class="label">}}
         {\HCode{</span>}}%
   }
   {\egroup\par}
   {} {}
\Css{.lstlisting .label{margin-right:0.5em; }}
\Css{pre.lstlisting{font-family: monospace,monospace; white-space: pre-wrap;
                    margin-top:0.5em; margin-bottom:0.5em;
                   }}
\Configure{lstinputlisting}
   {\ifvmode \IgnorePar\fi \EndP
    \HCode{<!--l. \the\inputlineno-->}%
    \gdef\start:LstLn{\gdef\start:LstLn{\HCode{\Hnewline}}}% ignore first newline, to prevent spurious line
    \HCode{<pre class="lstinputlisting" id="listing-\listingN">}%
    \bgroup\ttfamily%\special{t4ht@(}%
       \Configure{listings}%
         {{\everypar{}\leavevmode}}%
         {{\everypar{}\leavevmode}}%
         {\start:LstLn \HCode{<span class="label">}}%
         {\HCode{</span>}}%
   }
   {%\special{t4ht@)}
    \egroup%
    \ifvmode \IgnorePar\fi \EndP  \HCode{</pre>}\par}%
\Css{pre.lstinputlisting{
   font-family: monospace,monospace;
   white-space: pre-wrap;
}}
\Css{.lstinputlisting .label{margin-right:0.5em;}}
\lst@AddToHook{PostOutput}{\HCode{</span>}}%

\lst@AddToHook{OutputBox}{%
  % get current color in the CSS form
  \extractcolorspec{.}\html@color%
  \expandafter\convertcolorspec\html@color{HTML}\html@color%
  \HCode{<span style="color:\#\html@color">}}% init
\lst@AddToHook{Init}{%
\gHAdvance\listingN by 1%
\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%
}
\makeatother
\catcode`\:=12
\begin{document}
\EndPreamble

This is the result for your sample:

enter image description here

3
  • Thanks Michael. I am testing it and found it removes newlines from the input. For example, if I have \begin{plainTextEnv} This is line 1 This is line 2 \end{plainTextEnv} (but each on separate line in the latex file , hard to show here but here is screen shot !Mathematica graphics), and everything the same, and using your above .cfg, I see the HTML output all one line. I hope this is easy to fix. Here is screen shot. !Mathematica graphics
    – Nasser
    Mar 31, 2022 at 17:17
  • 1
    @Nasser I see. Try the updated answer. It unfortunately inserts extra line at the end of listings, because it cannot detect an empty line.
    – michal.h21
    Mar 31, 2022 at 20:53
  • This worked Great. Having an extra empty line at end of listing is no problem at all. Thanks again for all support and help.
    – Nasser
    Mar 31, 2022 at 21:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .