我正在使用的代码如何使 tex4ht 与列表样式配合使用,仅进行基本装饰?(框架和背景颜色)? 感谢 Michal.h2,它运行良好,只是在首先从文件读取代码列表时不会加载定义的样式。
问题总结。
\lstdefinestyle{MMA}
{
language=Mathematica,%
etc...
}
然后后来
\lstinputlisting[style=MMA]{code.m}
该样式在 tex4ht 中不起作用。(但在 PDF 中起作用)。
这似乎是一个加载时间问题。我有解决方法,但解决方法很糟糕。首先,我将给出 MWE 以及如何重现它。最后还有一个包含所有所需文件的 zip 文件。
\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}
使用 lualatex 进行编译可得到具有正确样式的 PDF:
使用以下方式编译make4ht -ulm default -a debug -c my.cfg index.tex "mathjax,htm"
给出
你看,样式不存在。没有框架,也没有背景颜色。就好像 text4ht 对样式一无所知。
上面的文件.cfg
与上面的链接中的文件相同(它也在下面的 zip 文件中)。
我找到了这个解决方法。如果我添加排队代码(即不是来自文件),然后执行此操作,\lstinputlisting[style=MMA]{code.m}
样式就会显示出来!看起来内联样式导致 tex4ht 现在可以看到样式定义。下面是一个例子
\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}
现在使用相同的命令进行编译,得到 HTML
顺序很重要。如果我做了
\lstinputlisting[style=MMA]{code.m}
\begin{MMAinline}
....
\end{MMAinline}
那么它就不起作用了。内联需要先出现。但在 PDF 中,情况并非如此。在 PDF 中,无论有无内联列表,它都可以正常工作。
是否可以修复 .cfg 以便正确“看到”样式而不必在其之前进行内联列表?
压缩文件 有tex文件,code.m和my.cfg。
在 Linux Ubuntu 22.04 上使用 TL 2022
附录
我的配置文件文件
%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
代码.m文件
ClearAll[t, x1, x2, x3, x4, y1, y2, y3, y4];
答案1
您不必再对 Listings 使用任何自定义配置。之前针对您的问题的所有修复都已包含在 TeX4ht 源中。您的示例与 TeX4ht 开箱即用。我刚刚发现了一个小问题,Listings 在代码末尾添加了一些额外的换行符。这会导致代码下方出现虚假空间。我可以删除其中一个换行符,但第二个换行符似乎更难删除。
这是的更新版本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
结果如下: