我是计算机科学专业的学生。我更喜欢用 LaTeX 写家庭作业。我在专业科学课程中大量使用 tikZ。我现在的目标是将我的 TeX 文件转换为 HTML+CSS+MathJax。我希望 MathJax 支持的所有内容都转换为 MathJax,而其他所有内容(复杂的图表和绘图)都简单地转换为图像(SVG/PGF)。我曾尝试使用 Pandoc 和 Make4ht 来实现这个目标,但没有成功。Pandoc 在将我常用的 LaTeX 转换为 HTML+MathJax 方面做得相当不错,但在 TikZ 图表方面却有所欠缺。使用 Make4ht,我成功地处理了 TikZ 图表,但我对 TeX 本身有几个问题。目前我有两个问题:
- make4ht 不会转换我的
\newcommand
宏。 - make4ht 不能使用 HTML 颜色代码来填充表格中的单元格。
- make4ht 生成 TikZ 图表的 SVG 图像,但不包含文本。
这里我提供了一个示例代码及其调用后在我的计算机上显示的结果:make4ht -ux file.tex "mathjax"
\documentclass [11pt, a4paper]{article}
\usepackage[margin=2 cm]{geometry} %
\usepackage{polyglossia}
\usepackage{listings}
\usepackage{fontspec}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{parskip}
\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}
\usepackage[american]{circuitikz}
\newcommand{\n}[1]{\overline{#1}}
\begin{document}
\begin{table}[h!]
\begin{tabular}{|c|c|c|c|c|}
\hline
\rowcolor[HTML]{C0C0C0}
\( \mathbf{x} \) & \( \mathbf{y} \) & \( \mathbf{(\n{x + y})} \) & \( \mathbf{\left( \n{\n{x} + \n{y}}\right)} \) & \( \mathbf{F} \)\\ \hline
0 & 0 & 1 & 0 & \cellcolor[HTML]{67FD9A}0 \\ \hline
0 & 1 & 0 & 0 & \cellcolor[HTML]{67FD9A}0 \\ \hline
1 & 0 & 0 & 0 & \cellcolor[HTML]{67FD9A}0 \\ \hline
1 & 1 & 0 & 1 & \cellcolor[HTML]{67FD9A}0 \\ \hline
\end{tabular}%
\end{table}
\( (\n{A} + B) \cdot \n{B} + \n{A}\n{C} + A \)
\begin{circuitikz} \draw
(0,0) node[] (A) {A}
(0,-0.5) node[] (B) {B}
(0,-1.5) node[] (C) {C}
(4,-0.5) node[or port] (myor) {}
(2,-1) node[nand port] (mynand) {}
(A) -| (myor.in 1)
(B) -| (mynand.in 1)
(C) -| (mynand.in 2)
(mynand.out) -| (myor.in 2)
(myor.out) node[anchor=west] {F}
;\end{circuitikz}
\end{document}
如您所见,make4ht 没有解释\n
宏。此外,在运行命令时,我确实收到了以下警告消息:
Argument of \c:HColor: has an extra }.
<inserted text>
\par
l.27 ... \rowcolor[HTML]{C0C0C0}
?
答案1
更新:
现在可以使用直接颜色。尝试以下版本colortbl.4ht
:
% colortbl.4ht (2019-11-21-17:51), generated from tex4ht-4ht.tex
% Copyright 1997-2009 Eitan M. Gurari
% Copyright 2009-2019 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 2019-11-21-17:51}
\RequirePackage{xcolor}
\CT@everycr{\o:noalign:{\global\let\CT@row@color\relax}\the\everycr}
\def\columncolor#1{{\def\current@color{#1}%
\csname a:cell-colortbl\endcsname}}
\Odef\columncolor[#1]#2{{\if :#1:\def\current@color{#2}%
\else
\gHAdvance\tblcol:N by 1
\convertcolorspec{#1}{#2}{HTML}\tmp:tblcolor
\Configure{HColor}{tblcol-\tblcol:N}{\#\tmp:tblcolor}%
\def\current@color{tblcol-\tblcol:N}%
\fi
\csname a:cell-colortbl\endcsname}%
\futurelet\:temp\left:colcol}
\def\left:colcol{%
\ifx [\:temp \expandafter\left::colcol \fi
}
\def\left::colcol[#1]{
\futurelet\:temp\right:colcol
}
\def\right:colcol{%
\ifx [\:temp \expandafter\right::colcol \fi
}
\def\right::colcol[#1]{}
\HAssign\tblcol:N = 0
\def\CT@cellc#1[#2]#3{{\if :#2:\def\current@color{#3}%
\else
\gHAdvance\tblcol:N by 1
\convertcolorspec{#2}{#3}{HTML}\tmp:tblcolor
\Configure{HColor}{tblcol-\tblcol:N}{\#\tmp:tblcolor}%
\def\current@color{tblcol-\tblcol:N}%
\fi
\csname a:cell-colortbl\endcsname}%
\futurelet\:temp\left:colcol}
\NewConfigure{@classz}{4}
\pend:def\@classz{\pic:gobble\a:@classz}
\append:def\@classz{\pic:gobble\b:@classz}
\pend:def\insert@column{\pic:gobble\c:@classz}
\append:def\insert@column{\pic:gobble\d:@classz}
\def\:temp{\global\let\CT@do@color\relax}
\HLet\CT@@do@color\:temp
\let\::maketitle\o:maketitle:
\def\o:maketitle:{%
\ifx \EndPicture\:UnDef
\NewConfigure{@classz}{4}%
\Configure{@classz}{}{}{}{}%
\fi
\::maketitle }
\def\rowcolor{%
\o:noalign:{\ifnum0=`}\fi
\global\let\CT@do@color\CT@@do@color
\relax
\ifx\LT@head\Un:Def\else
\ifnum \HRow=0\relax
\expandafter\ifx\csname lt:sv\endcsname\relax
\HAssign\HRow = 1\relax
\ifvoid\LT@head
\ifvoid\LT@firsthead \else \HAdvance\HRow by 1\relax\fi
\else \HAdvance\HRow by 1\relax\fi
\else
\HAssign\HRow = \lt:sv \relax
\HAdvance\HRow by 1\relax
\fi
\fi\fi
%
\@ifnextchar[\CT@rowa\CT@rowb}
\def\CT@rowa[#1]#2{%
\save:color#1 #2//%
\pic:gobbleII\a:rowcolor{#2}%
%\pic:gobbleII\a:rowcolor{#1 #2}%
\gdef\CT@row@color{\CT@color{#2}}%
\CT@rowc}
\def\CT@rowb#1{%
\pic:gobbleII\a:rowcolor{#1}%
\gdef\CT@row@color{\CT@color{#1}}%
\CT@rowc}
\NewConfigure{rowcolor}{1}
\def\convert:colorspec#1 #2 #3 #4{%
\edef\current:color{%
\ifx\relax#1\relax\else%
#1\ifx\relax#2\relax\else%
, #2\ifx\relax#3\relax\else%
, #3\ifx\relax#4\relax\else%
, #4%
\fi%
\fi%
\fi%
\fi%
}%
}
\def\save:color#1 #2//{%
\typeout{*******************}
\typeout{color: #1, #2}
\convert:colorspec#2 {} {} {} {}
\convertcolorspec{#1}{\current:color}{HTML}\tmp:col
\def\current@color{#2}
\Configure{HColor}{\current@color}{\#\tmp:col}
}
\def\begin:current@color{\let\sv:curcolor\current@color}
\def\end:current@color{%
\ifx \current@color\sv:curcolor
\else%
\expandafter\save:color\current@color//%
\csname a:text-colortbl\endcsname
\fi
}
\NewConfigure{text-colortbl}{1}
\def\GET@column@color{}
\def\color:ii[#1]#2#3!*?: {\def\:temp{#1 #2}}
\def\color:i#1#2!*?: {\def\:temp{#1}}
\let\ctbl:mcol\multicolumn
\def\multicolumn#1#2#3{%
\ctbl:mcol{#1}{#2}{#3}%
\expand:after{\expandafter\MUL:LMN\meaning\@preamble}\MUL:PA//%
\ignorespaces}
{
\def\MUL:PA{\gdef\MUL:PA}
\def\MUL:LMN{%
\catcode`\C=12
\catcode`\T=12
\catcode`\@=12
\catcode`\c=12
\catcode`\o=12
\catcode`\l=12
\catcode`\r=12
\catcode`\t=12
\catcode`\e=12
\catcode`\m=12
\catcode`\p=12
\catcode`\d=12
\catcode`\i=12
\catcode`\b=12
\gdef\MUL:LMN}
\MUL:LMN#1CT@color #2@tempdimb#3//{\::KOLOR{#2}}
\MUL:PA{CT@color @tempdimb}
}
\def\::KOLOR#1{\if :#1:\else \:KOLOR#1//%
\fi}
\def\:KOLOR{\@ifnextchar[\mc:clr{\mc:clr[]}}
{
\catcode`\{=12
\catcode`\}=12
\catcode`\(=1
\catcode`\)=2
\gdef\mc:clr[#1]{#2}((%
\def\current@color(\if :#1:\else #1 \fi #2)%
\csname a:cell-colortbl\endcsname
)\def\:temp##1//()\:temp)
)
\NewConfigure{cell-colortbl}{1}
\Hinput{colortbl}
\endinput
原始答案:
我可以对您的文件进行tex4ht
少许修改并编译。
有三个问题需要修复:\n
MathJax 中缺少命令定义、颜色问题和 TikZ 图形问题。
对于 TikZ,我会使用替代司机。它使用 Dvisvgm 进行转换,产生的结果比默认驱动程序好得多。尤其是对于文本节点。
命令问题\n
是由于tex4ht
无法在 MathJax 模式下处理数学内容而导致的。只需对 MathJax 进行简单的配置即可修复。
可以使用命名颜色而不是使用直接值来解决颜色问题。还需要为该命令提供替代定义,因为它默认\cellcolor
不受支持。tex4ht
以下是修改后的 TeX 文件:
\documentclass [11pt, a4paper]{article}
\usepackage[margin=2 cm]{geometry} %
\usepackage{polyglossia}
\usepackage{listings}
\usepackage{fontspec}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{parskip}
\usepackage{graphicx}
\ifdefined\HCode
\def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi
\usepackage[table,xcdraw]{xcolor}
\usepackage[american]{circuitikz}
\newcommand{\n}[1]{\overline{#1}}
% \definecolor[HTML]{lightgreen}{rgb}{67FD9A}
\begin{document}
\begin{table}[h!]
\begin{tabular}{|c|c|c|c|c|}
\hline
\rowcolor{lightgray}
\( \mathbf{x} \) & \( \mathbf{y} \) & \( \mathbf{(\n{x + y})} \) & \( \mathbf{\left( \n{\n{x} + \n{y}}\right)} \) & \( \mathbf{F} \)\\ \hline
0 & 0 & 1 & 0 & \cellcolor{lightgreen}0 \\ \hline
0 & 1 & 0 & 0 & \cellcolor{lightgreen}0 \\ \hline
1 & 0 & 0 & 0 & \cellcolor{lightgreen}0 \\ \hline
1 & 1 & 0 & 1 & \cellcolor{lightgreen}0 \\ \hline
\end{tabular}%
\end{table}
\( (\n{A} + B) \cdot \n{B} + \n{A}\n{C} + A \)
\begin{circuitikz} \draw
(0,0) node[] (A) {A}
(0,-0.5) node[] (B) {B}
(0,-1.5) node[] (C) {C}
(4,-0.5) node[or port] (myor) {}
(2,-1) node[nand port] (mynand) {}
(A) -| (myor.in 1)
(B) -| (mynand.in 1)
(C) -| (mynand.in 2)
(mynand.out) -| (myor.in 2)
(myor.out) node[anchor=west] {F}
;\end{circuitikz}
\end{document}
主要的变化是使用替代驱动程序:
\ifdefined\HCode
\def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi
并使用命名的颜色:
\rowcolor{lightgray}
\( \mathbf{x} \) & \( \mathbf{y} \) & \( \mathbf{(\n{x + y})} \) & \( \mathbf{\left( \n{\n{x} + \n{y}}\right)} \) & \( \mathbf{F} \)\\ \hline
0 & 0 & 1 & 0 & \cellcolor{lightgreen}0 \\ \hline
以下是配置文件:
\Preamble{xhtml}
\Configure{HColor}{lightgreen}{\#67FD9A;}
\makeatletter
\def\CT@cellc#1[#2]#3{{\def\current@color{#3}%
\csname a:cell-colortbl\endcsname}}
\makeatother
\Configure{@HEAD}{\HCode{\detokenize{%
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
Macros: {
n : "\\overline",
}
},
}
);
</script>
}}}
\begin{document}
\EndPreamble
它定义lightgreen
颜色,因为所有的颜色都必须配置tex4ht
。lightgray
默认定义:
\Configure{HColor}{lightgreen}{\#67FD9A;}
\cellcolor
下一行重新定义了内部使用的宏,tex4ht
用于单元格颜色的配置:
\def\CT@cellc#1[#2]#3{{\def\current@color{#3}%
\csname a:cell-colortbl\endcsname}}
\makeatother
最后,使用以下配置,我们\n
为 MathJax 定义宏:
\Configure{@HEAD}{\HCode{\detokenize{%
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
Macros: {
n : "\\overline",
}
},
}
);
</script>
}}}
使用以下方式编译文件
make4ht -ul -c config.cfg filename.tex "mathjax,svg"
结果如下: