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:
Compiling with make4ht -ulm default -a debug -c my.cfg index.tex "mathjax,htm"
gives
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
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];