1

I am using the code from how to make tex4ht work with listings styles for only basic decoration? (frame and background coloring)? thanks to Michal.h2 which works great except that the style defined is not loaded when reading listing of code from file first.

Summary of the problem. When doing

\lstdefinestyle{MMA}
{
    language=Mathematica,%
    etc...
}

Then later

\lstinputlisting[style=MMA]{code.m}

The style does not take effect in tex4ht. (it does in PDF).

It seems to be a loading time issue. I have workaround but it is ugly workaround. First I will give MWE and how to reproduce it. At end also have zip file with all the files needed.

\documentclass[12pt]{article}%

\usepackage{listings,lstautogobble}% added lstautogobble oct 30,2020
\usepackage{fancybox,fancyvrb,xcolor}
\definecolor{bg}{RGB}{255,255,226}

\lstdefinestyle{MMA}
{
    language=Mathematica,%
    basicstyle=\ttfamily\small,
    backgroundcolor=\color{bg},
    framerule=0.5pt,
    frame=single,
    rulecolor=\color{gray}
}

\begin{document}
This is my code

\lstinputlisting[style=MMA]{code.m}

\end{document}

Compiling with lualatex gives PDF with correct style in it:

![enter image description here

Compiling with make4ht -ulm default -a debug -c my.cfg index.tex "mathjax,htm"

gives

enter image description here

You see, the style is not there. No frame and no background color. It is as if text4ht did not know anything about the style.

The .cfg file above is the same from the link above (it is also in the zip file below).

I found this workaround. If I add an inline code (i.e. not from a file), and then after that do \lstinputlisting[style=MMA]{code.m} then the style shows up ! It looks like the inline style caused tex4ht to now see the style definition. Here an example

\documentclass[12pt]{article}%

\usepackage{listings,lstautogobble}% added lstautogobble oct 30,2020
\usepackage{fancybox,fancyvrb,xcolor}
\definecolor{bg}{RGB}{255,255,226}

\lstdefinestyle{MMA}
{
    language=Mathematica,%
    basicstyle=\ttfamily\small,
    backgroundcolor=\color{bg},
    framerule=0.5pt,
    frame=single,
    rulecolor=\color{gray}
}

\lstnewenvironment{MMAinline}{%
  \lstset{style=MMA}}{}

\begin{document}
This is my code

\begin{MMAinline}
some text just to force the style to get loaded by tex4ht??
\end{MMAinline}

\lstinputlisting[style=MMA]{code.m}

\end{document}

And now compiling using same command gives the HTML

enter image description here

The order is important. If I had done

\lstinputlisting[style=MMA]{code.m}
\begin{MMAinline}
....
\end{MMAinline}

Then it will not have worked. The inline needs to come first. But in PDF, this is not the case. In PDF it works either way, with and without inline listings.

Is it possible to fix the .cfg so that the style is correctly "seen" without having to do an inline listing before it?

zip file has the tex file, code.m and my.cfg.

Using TL 2022 on Linux Ubuntu 22.04

Appendix

my.cfg file

%see https://tex.stackexchange.com/questions/639045/how-to-make-tex4ht-work-with-listings-styles-for-only-basic-decoration-frame-a
\Preamble{xhtml}
\ifdefined\HCode 
\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
\fi 
\begin{document}
\EndPreamble

code.m file

ClearAll[t, x1, x2, x3, x4, y1, y2, y3, y4];

1 Answer 1

1

You don't have to use any custom configurations for Listings anymore. All previous fixes for your issues are already included in TeX4ht sources. Your example works out of the box with TeX4ht. I've just discovered a small issue, Listings adds some extra newlines at the end of the code. This causes spurious space below the code. I was able to remove one of these newlines, but the second seems to be harder to remove.

This is the updated version of listings.4ht:

% listings.4ht (2022-05-22-19:43), generated from tex4ht-4ht.tex
% Copyright 2001-2009 Eitan M. Gurari
% Copyright 2009-2022 TeX Users Group
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3c of this license or (at your option) any
% later version. The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions
% of LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
%
% The Current Maintainer of this work
% is the TeX4ht Project <http://tug.org/tex4ht>.
%
% If you modify this program, changing the
% version identification would be appreciated.
\immediate\write-1{version 2022-05-22-19:43}

\@ifpackageloaded{xcolor}{}{%
\RequirePackage{xcolor}
}
\def\lst@makecaption#1#2{\cptA: #1\if :#1:\else\cptB:\fi \cptC: #2\cptD:}

\newif\iflstnest
\append:defII\lst@EnterMode{%
  \ifx \lsthk:EveryLine\:UnDef
     \let\lsthk:EveryLine\lsthk@EveryLine
  \fi
  \ifx \lsthk:EveryLine\lsthk@EveryLine
      \pend:def\lsthk@EveryLine{\c:listings
             \def\dd:listings{\d:listings\let\dd:listings\empty}}%
  \fi
  \append:def\lsthk@EveryPar{\dd:listings}%
  \iflstnest\else
  \a:listings\fi\bgroup
  %\Configure{$}{}{}{}%
  \aftergroup\lst:EnterMode  }
\def\lst:EnterMode{\iflstnest\else\b:listings\fi\egroup}
\NewConfigure{listings}{4}
\let\dd:listings=\empty
\append:defI\lst@Init{\csname a:listings-init\endcsname\global\lstnesttrue}
\pend:def\lst@DeInit{\csname b:listings-init\endcsname\global\lstnestfalse}
\NewConfigure{listings-init}{2}
\lst@AddToHook{TextStyle}{%
   \Configure{listings}{}{}{}{}%
   \a:lstinline \bgroup \aftergroup\b:lstinline\aftergroup\egroup
  }
\NewConfigure{lstinline}{2}
\pend:defI\lst@MakeCaption{%
  \let\lst:addcontentsline\addcontentsline
  \def\addcontentsline{\gHAdvance\TitleCount by 1
                       \lst:addcontentsline}%
}
\append:defI\lst@MakeCaption{%
  \let\addcontentsline\lst:addcontentsline
}
\ConfigureToc{lol} {}{\empty}{}{\newline}
\lst@UserCommand\lstlistoflistings{\bgroup%
    \ifdefined\chapter\chapter*{\lstlistlistingname}\else\section*{\lstlistlistingname}\fi%
    \TableOfContents[lol]%
  \egroup}
\def\:tempa{%
   \ifx\lst@OutputBox\@gobble\else \the\everypar \fi
   \global\advance\lst@newlines\m@ne
   \lst@newlinetrue
}%
\HLet\lst@NewLine\:tempa
\def\:tempa#1{
    \begingroup%
      \lsthk@PreSet\gdef\lst@intname{#1}%
      \expandafter\lstset\expandafter{\lst@set}%
      \lsthk@DisplayStyle%
      \catcode\active=\active%
      \a:lstinputlisting\ht:special{t4ht@[}\lst@Init\relax \let\lst@gobble\z@%
      \lst@SkipToFirst%
      \lst@ifprint \def\lst@next{\input{#1}}%
             \else \let\lst@next\@empty \fi%
      \ht:special{t4ht@]}\lst@next\ht:special{t4ht@[}\lst@DeInit\ht:special{t4ht@]}%
      \b:lstinputlisting%
    \endgroup}

\HLet\lst@InputListing\:tempa
\NewConfigure{lstinputlisting}{2}
\def\:tempa#1{%
   \setbox\z@\hbox{{\lst@currstyle{\kern#1}}}%
   \global\advance\lst@currlwidth \wd\z@
   \tmp:dim=#1 \let\:tempc=\empty
   \loop \ifdim \tmp:dim>\a:lst@Kern
      \advance \tmp:dim by -\a:lst@Kern
      \advance \tmp:dim by -\b:lst@Kern
      \append:def\:tempc{\:nbsp}%
   \repeat
   \setbox\z@\hbox{{\lst@currstyle{\:tempc}}}%
   \lst@OutputBox\z@}
\HLet\lst@Kern\:tempa
\NewConfigure{lst@Kern}{2}
\Configure{lst@Kern}{0.499em}{0.1em}
\def\lst@outputspace{\HCode{ }}

\HLet\lst@frameInit=\empty
\HLet\lst@frameExit=\empty

\Hinput{listings}
\endinput

And this is the result:

enter image description here

You must log in to answer this question.

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