如何使 tex4ht 与列表样式配合使用,仅进行基本装饰?(框架和背景颜色)?

如何使 tex4ht 与列表样式配合使用,仅进行基本装饰?(框架和背景颜色)?

这是 tex4ht 独有的问题。

有一个很好的答案与 text4ht 一起使用时如何添加带有列表的框架和背景颜色这显示了如何让 tex4ht 使用listings包时放置框架和背景颜色\lstset{...},然后\begin{lstlisting}

但是这个解决方案在使用样式时不起作用listings。一个小例子可以说明这一点。这个 MWE 有效,因为它使用了\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}

编译为make4ht -ulm default -a debug -c ./my.cfg foo.tex "mathjax,htm"

给出这个 HTML(上面使用的 .cfg 文件在底部给出。它与上面链接中的答案相同)

在此处输入图片描述

一切都很好。但由于我有很多种语言,我需要使用\lstdefinestyle来命名样式,然后使用\lstset语言名称。这样,我可以在同一个 latex 文档中使用不同的列表,每个列表都有命名的样式。

当我这样做时,tex4ht 不再添加框架和背景颜色。

这是与上面相同的例子,但是使用listings样式环境完成。

\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}

使用相同命令编译得到

在此处输入图片描述

所以装饰没有用。我认为这是因为 .cfg 只寻找lstlisting钩子lstinputlisting,而不寻找lstnewenvironment

是否可以让 .cfg 也适用于上述设置?否则,我将不得不\ifdefined\HCode在每个列表周围添加内容,这会使代码难以阅读。

附录

这是使用的 .cfg

%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

TeX4ht 支持自定义 Listings 环境,但默认配置不会将数字 ID 添加到生成的 HTML 中,因此边框和背景颜色的 CSS 指令不会生效。应使用此配置文件修复此问题:

\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

这是您的样本的结果:

在此处输入图片描述

相关内容