我正在使用 htlatex 将 LaTeX 文件编译my_document.tex
为 HTML 页面my_document.html
,命令如下:
htlatex my_document.tex "xhtml,mathml-" " -cmozhtf"
我有一个 MATLAB 代码文件main.m
,我想将其包含在my_document.html
与 MATLAB 编辑器匹配的格式中。使用 进行编译时pdflatex
,我使用以下内容:
\documentclass{article}
\usepackage{listings}
\usepackage[numbered]{matlab-prettifier}
\lstset{
style=Matlab-editor
}
\begin{document}
\lstinputlisting{main.m}
\end{document}
但是,使用 编译上述代码htlatex
会产生黑白输出。我们目前的方法是使用m2tex将 .m 文件转换为 .tex 文件,但这会使更新 HTML 文件的过程变得更加复杂,因为它增加了一个步骤,所以我正在尝试找到一种方法来简化这个过程。我在下面提供了一个方法作为答案,但我正在寻找其他建议。
编辑:以下是 MATLAB 文件示例main.m
:
% This is a comment
var = "this is a string";
for i = 1:5
doThing(i)
end
disp this_is_some_text
function doThing(i)
fprintf('Number %d\n', i)
end
在 MATLAB 代码编辑器中,main.m
看起来像下面的屏幕截图,所以目标是让 HTML 输出也与此匹配:
答案1
使用 TeX4ht 处理颜色有点复杂。像 这样的功能\textcolor
可以工作,但由 设置的颜色\color
无法开箱即用。您需要自己添加一些包含颜色信息的 HTML 标签。
对于列表,我们可以使用包提供的钩子在每个关键字周围插入一些元素。我们可以获取当前颜色并插入 CSS 指令。
尝试这个配置文件config.cfg
:
\Preamble{xhtml}
\makeatletter
\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{PostOutput}{\HCode{</span>}}%
\begin{document}
\EndPreamble
在OutputBox
钩子中,我们定义了宏。它通过和命令\html@color
保存了当前适合 CSS 的颜色。我们通过命令将其插入到 HTML 输出中。在钩子中,我们插入结束\extractcolorspec
\convertcolorspec
\lst@AddToHook{PostOutput}{\HCode{</span>}}
PostOutput
</span>
标记。
以下是 MWE:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[numbered]{matlab-prettifier}
\lstset{
style=Matlab-editor
}
\begin{document}
Listings environment:
\begin{lstlisting}
function y = f(x)
% comment
y = x^2
disp('1hll')
[t, x] = ode45(@f, [0, 1], 0)
end
\end{lstlisting}
Included mathlab:
\lstinputlisting{main.m}
\end{document}
使用以下方式编译(不要使用htlatex
,它已被弃用):
make4ht -c config.cfg -m draft my_document.tex
结果如下:
最后一件事。我在 Color 包的配置文件中发现了一个错误。color.4ht
它阻止了颜色的正确转换。以下是正确的版本:
% color.4ht (2022-02-01-17:18), generated from tex4ht-4ht.tex
% Copyright 1997-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-02-01-17:18}
\expandafter\ifx \csname color:def\endcsname\relax
\let\color:def\def
\else \expandafter\endinput\fi
\let\:temp\begingroup
\HLet\color@setgroup\:temp
\def\HColor{\:warning{\string\Hcode{...}{...} is deprecated; Use
\string\Configure{HColor}{...}{...}}\Configure{HColor}}
\NewConfigure{HColor}[2]{\if !#1!\if!#2!\let\HColor\relax \else \edef\HColor{#2}\fi
\else
\expandafter\edef\csname CLR:#1\endcsname{#2}\fi}
\def\:temp#1#2#3{%
\@ifundefined{color@#2}%
{\c@lor@error{model `#2'}}%
{\@ifundefined{\string\color @#1}{}%
{\PackageInfo{color}{Redefining color #1}}%
\csname color@#2\expandafter\endcsname
\csname\string\color @#1\endcsname{#3}}}
\ifx \definecolor\:temp
\pend:defIII\definecolor{%
\expandafter\ifx\csname HColor:##2\endcsname\relax\else
\csname HColor:##2\endcsname ##3 //%
\edef\:temp{{HColor}{##1}{\HColor}}%
\expandafter\Configure\:temp
\fi}
\fi
\NewConfigure{color}{1}
\def\:tempc#1#2#3{\protect\leavevmode{\protect\a:textcolor
\color#1{#2}#3\protect\b:textcolor}}
\HLet\@textcolor=\:tempc
\NewConfigure{textcolor}{2}
\def\pagecolor{%
\begingroup \a:pagecolor
\let\ignorespaces\endgroup
\let\set@color\set@page@color
\color}
\NewConfigure{pagecolor}{1}
\def\:tempa[#1]#2{\a:color{#1 #2}\o:@undeclaredcolor:[#1]{#2}}
\HLet\@undeclaredcolor\:tempa
\pend:defI\@declaredcolor{\a:color{##1}}
\NewConfigure{SetHColor}[2]{{%
\expandafter\let\expandafter\:temp \csname CLR:#2\endcsname
\ifx \:temp\relax
\edef\:temp{#2 //}\expandafter\get:HColor\:temp
\ifx \HColor\relax
\expandafter\ifx \csname get!HColor\endcsname\relax \else
\edef\:temp{#2!//}\expandafter\csname get!HColor\expandafter\endcsname\:temp
\fi
%
\fi
\else \let\HColor=\:temp \fi
\ifx \HColor\relax
\:warning{missing \string\Configure{HColor}{#2}{...}
(in LaTeX: \csname\string\color @#2\endcsname)}%
\expandafter\global\expandafter\let\csname CLR:#2\endcsname\empty
\else #1\fi }}
\def\get:HColor#1 #2//{%
\if\relax#2\relax
% \expandafter\ifx \csname HColor:#1\endcsname\relax
\let\HColor=\relax
\expandafter\ifx \csname\string\color @#1\endcsname \relax\else
\expandafter\ifx \csname colortyp:\endcsname\relax \else
\csname colortyp:\expandafter\expandafter\expandafter\endcsname
\csname\string\color @#1\expandafter\endcsname
\space . //%
\fi \fi
%
\else
\csname HColor:#1\endcsname #2 //%
\fi
}
\def\c:HColor:gray:{\def\HColor:gray##1 ##2//}
\Configure{HColor:gray}{\Configure{HColor}{}{}}
\def\c:HColor:rgb:{\def\HColor:rgb##1,##2,##3 ##4//}
\Configure{HColor:rgb}{\Configure{HColor}{}{}}
\def\c:HColor:cmyk:{\def\HColor:cmyk##1,##2,##3,##4 ##5//}
\Configure{HColor:cmyk}{\Configure{HColor}{}{}}
\long\def\:temp#1#2#3{%
\ifcolors@
\def\:temp{#1}\def\:tempa{\relax}\ifx\:temp\:tempa
\a:colorbox \else \a:fcolorbox \fi
\fi
{#1{\leavevmode #2{\set@color#3}}}%
\ifcolors@
\def\:temp{#1}\def\:tempa{\relax}\ifx\:temp\:tempa
\b:colorbox \else \b:fcolorbox \fi
\fi
}
\HLet\color@b@x\:temp
\NewConfigure{colorbox}{2}
\NewConfigure{fcolorbox}{2}
\Hinput{color}
\endinput
答案2
当使用 编译以下 .tex 文件时htlatex
,环境中的代码lstlisting
格式非常好,但使用 导入的代码lstinputlisting
仍然是黑白的。
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage[numbered]{matlab-prettifier} % Pretty print Matlab code
% Set the styles for different categories of code to arbitrary and distinct styles.
\lstset{
style=Matlab-editor,
basicstyle=\ttfamily, % Appears as class=ectt-1000 in HTML
keywordstyle=\rmfamily\bfseries, % Appears as class=ecbx-1000 in HTML
commentstyle=\sffamily, % Appears as class=ecss-1000 in HTML
stringstyle=\sffamily\slshape % Appears as class=ecsi-1000 and tcsi-1000 in HTML
}
\begin{document}
% Set the font-family and color in the CSS based on the class associated with
% each span of text. The values "ectt-1000", etc., were found by inspecting
% the compiled HTML page in Google Chrome.
% Note: The \Css command only works when compiling with htlatex.
\Css{div.lstlisting .ectt-1000 {font-family: monospace;color:black}} % Basic
\Css{div.lstlisting .ecss-1000 {font-family: monospace;color:green}} % Comments
\Css{div.lstlisting .ecbx-1000 {font-family: monospace;color:blue}} % Keywords
\Css{div.lstlisting .tcsi-1000 {font-family: monospace;color:purple}} % String (delimiter)
\Css{div.lstlisting .ecsi-1000 {font-family: monospace;color:purple}} % String (interior)
% Code in the lstlisting environment is rendered reasonable well.
\begin{lstlisting}
function y = f(x)
% comment
y = x^2
disp('1hll')
[t, x] = ode45(@f, [0, 1], 0)
end
\end{lstlisting}
% Code from lstinputlisting is still black and white.
\lstinputlisting{Matlab2tex_1_2/f.m}
\end{document}
这种方法感觉很不专业,而且我不太理解这些\Css
命令。