有什么办法可以让 tex4ebook 支持 unicode 字符吗?

有什么办法可以让 tex4ebook 支持 unicode 字符吗?

使用下面的代码生成epub文件是没有问题的:

\documentclass{ctexart}

\begin{document}
Hello World
$x_1$
\end{document}
$ tex4ebook -f epub3 ./demo.tex

但是插入unicode字符时:

\documentclass{ctexart}

\begin{document}
你好!世界!
\end{document}

出现以下错误:

[STATUS]  tex4ebook: Conversion started
[STATUS]  tex4ebook: Input file: ./demo.tex
--- warning --- Couldn't find font `unisong75.htf' (char codes: 0--255)

----------------------------------------------------
environment file
----------------------------------------------------
% unix/tex4ht.env (2019-03-07), generated from tex4ht-env.tex, and
% modified by hand for TeX Live (unix).
% Copyright 2009-2019 TeX Users Group
% Copyright 1997-2009 Eitan M. Gurari
%
% 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.%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Notes:                                                            %
%  1. Empty lines might be harmful                                  %
%  2. Tagged script segments <tag>...</tag> are scanned only if     %
%     their names are specified within -ctag switches of tex4ht.c   %
%     and t4ht.c. When -c switches are not supplied, a -cdefault    %
%     is implicitly assumed.                                        %
%        The tags <tag> and </tag>, but not their bodies, are       %
%     ignored if they are not located at the start of their lines.  %
%  3. Place this file in your work directory and/or root directory  %
%     and/or in directory `xxx' of your choice. In the latest case, %
%     compile tex4ht.c and t4ht.c with `#define ENVFILE xxx', or    %
%     provide the address of the file to tex4ht and t4ht throught   %
%     the -e switch                                                 %
%  4. Under kpathsea, the substring `%%~' may be employed in        %
%     commands to indirectly obtain the value of ${TEXMFDIST}.      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% * Replace `path', and possibly what follows, with appropriate     %
%   content                                                         %
% * A path may start with:                                          %
%   ~ for environment variable HOME                                 %
%   ~~ for environment variable TEX4HTTFM                           %
%   %%~ for ${TEXMFDIST} of KPATHSEA                                %
% * A ! requests recursive search into subdirectories               %
% * Multiple t and i entries are allowed                            %
% * When supported by kpathsea                                      %
%   + t entries are ignored                                         %
%   + the path before /ht-fonts/ is ignored in i records that don't %
%     represent real addresses                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
tpath/tex/texmf/fonts/tfm/!
......
----------------------------------------------------
--- warning --- Couldn't find font `unisong4e.htf' (char codes: 0--255)
--- warning --- Couldn't find font `unisongff.htf' (char codes: 0--255)
--- warning --- Couldn't find font `unisong59.htf' (char codes: 0--255)
--- warning --- Couldn't find font `unisong4f.htf' (char codes: 0--255)

我该怎么做才能让 tex4ebook 支持 unicode 字符?

答案1

似乎tex4ebook不支持ctexart类。这对我来说是可行的:

\documentclass{article}
\usepackage{ctex}
\begin{document}
你好!世界!
\end{document}
$ tex4ebook -f epub3 -l ./demo.tex

答案2

问题是ctexart使用了 TeX4ht 不支持的字体,从终端输出可以看出:

--- warning --- Couldn't find font `unisong75.htf'

这些.htf文件包含字体字符和 Unicode 字符之间的映射。没有它们,您就无法获得正确的文本,尤其是非拉丁文本。在进一步调查中,我发现这些字体有一个映射,但对于它们的旧版本,它们的名称不同。所以这只是将文件重命名.htf为正确名称的问题。我已经更新了 TeX4ht 源,因此如果您使用 TeX Live,正确的版本应该会在几天内可用。

与此同时,您可以使用 LuaLaTeX 编译您的文件。您需要使用一些 TeX4ht 配置文件来防止错误,但之后它应该可以编译。

首先,我们需要告诉 TeX4ht,我们想在类本身之前加载配置文件。可以在文件中完成usepackage.4ht

% usepackage.4ht (2022-03-16-14:13), generated from tex4ht-4ht.tex
% Copyright 2003-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-03-16-14:13}

   \def\:temp{tex4ht}\ifx \:temp\@currname
   \:warning{\string\usepackage{tex4ht} again?}
   \def\:temp#1htex4ht.def,tex4ht.sty#2!*?: {\def\:temp{#2}}
\expandafter\:temp \@filelist htex4ht.def,tex4ht.sty!*?: %
\ifx \:temp\empty  \else
   \:warning{if
    \string\RequirePackage[tex4ht]{hyperref} or
    \string\usepackage[tex4ht]{hyperref} was
    used try instead, repectively,
    \string\RequirePackage{hyperref} or
    \string\usepackage{hyperref}}
\fi

\fi
\gdef\a:usepackage{\use:package ,!*?: }
\gdef\use:package#1,{%
   \if :#1:\def\:temp##1!*?: {}\else
      \def\:temp{#1}\ifx \@currname\:temp
             \def\:temp##1!*?: {\input usepackage.4ht  }%
      \else \let\:temp=\use:package \fi
   \fi \:temp}
\Configure{PackageHooks}{titlesec.sty}{titlesec-hooks.4ht}
\Configure{PackageHooks}{biblatex-chicago.sty}{biblatex-chicago-hooks.4ht}
\Configure{PackageHooks}{cleveref.sty}{cleveref-hooks.4ht}
\Configure{PackageHooks}{xr.sty}{xr-hooks.4ht}
\Configure{PackageHooks}{xr-hyper.sty}{xrhyper-hooks.4ht}
\Configure{PackageHooks}{eso-pic.sty}{esopic-hooks.4ht}
\Configure{PackageHooks}{showframe.sty}{showframe-hooks.4ht}
\Configure{PackageHooks}{expl3.sty}{expl3-hooks.4ht}
\Configure{PackageHooks}{savetrees.sty}{savetrees-hooks.4ht}
\Configure{PackageHooks}{newcomputermodern.sty}{newcomputermodern-hooks.4ht}
\Configure{PackageHooks}{newcomputermodern.sty}{newcomputermodern-hooks.4ht}
\Configure{PackageHooks}{fontawesome5-utex-helper.sty}%
{fontawesome5-utex-helper-hooks.4ht}
\Configure{PackageHooks}{fontawesome5.sty}{fontawesome5-hooks.4ht}
\Configure{PackageHooks}{biblatex.sty}{biblatex-hooks.4ht}
\Configure{PackageHooks}{xeCJK.sty}{xecjk-hooks.4ht}
\Configure{PackageHooks}{unicode-math.sty}{unicode-math-hooks.4ht}
\Configure{PackageHooks}{ctex.sty}{ctex-hooks.4ht}
% \Configure{PackageHooks}{ctexart.cls}{ctexart-hooks.4ht}
\Configure{PackageHooks}{polyglossia.sty}{polyglossia-hooks.4ht}
\Configure{PackageHooks}{fontspec.sty}{fontspec-hooks.4ht}
\Configure{PackageHooks}{tikz.sty}{tikz-hooks.4ht}
\Configure{PackageHooks}{pdfbase.sty}{pdfbase-hooks.4ht}
\Configure{PackageHooks}{pdfx.sty}{pdfx-hooks.4ht}
\Configure{PackageHooks}{breakurl.sty}{breakurl-hooks.4ht}
\Configure{PackageHooks}{hyperref.sty}{hyperref-hooks.4ht}
\Configure{PackageHooks}{caption.sty}{caption-hooks.4ht}
\Configure{PackageHooks}{minted.sty}{minted-sty-hooks.4ht}
\Configure{PackageHooks}{graphics.sty}{graphics-hooks.4ht}
\Configure{PackageHooks}{graphbox.sty}{graphbox-hooks.4ht}
\Configure{PackageHooks}{xcolor.sty}{xcolor-hooks.4ht}
\Configure{PackageHooks}{imakeidx.sty}{imakeidx-hooks.4ht}
\Configure{PackageHooks}{fancyhdr.sty}{fancyhdr-hooks.4ht}
\Configure{PackageHooks}{exerquiz.sty}{exerquiz-hooks.4ht}
\Configure{PackageHooks}{hyperxmp.sty}{hyperxmp-hooks.4ht}
\Configure{PackageHooks}{datetime2.sty}{datetime2-hooks.4ht}
\AddToHook{class/ctexart/before}{\input{ctexart-hooks.4ht}}    

\endinput

以及ctextart-hooks.4ht修复致命错误的文件:

\ExplSyntaxOn

% prevent multiple execution of this file
\ifdefined\l_save_engine_str\endinput\fi
\str_new:N \l_save_engine_str 
% stop processing if the engine is pdftex, we want to change processing only for LuaTeX
\def\l_save_engine_str{pdftex}
\str_if_eq:NNTF \c_sys_engine_str\l_save_engine_str{\ExplSyntaxOff\endinput}{}

% the luatexja package causes fatal error
\:dontusepackage{luatexja}

% fix compilation errors 
\AddToHook{package/ctexhook/after}{
  % don't let ctex to insert any files
  \cs_set:Npn \ctex_file_input:n #1{}
  % define some macros that are declared in the input files, and which are needed in the class
  \cs_set:Npn \ctex_add_to_selectfont:n #1{}
  \dim_new:N \ccwd
  \skip_new:N \l__ctex_ccglue_skip
  \cs_set_protected:Npn \ctex_update_em_unit:
  { \dim_set:Nn \ccwd { \f@size \p@ } }
  \cs_set_protected:Npn \ctex_update_ccglue: {}
}


\ExplSyntaxOff
\endinput

然后您可以使用以下命令编译文件:

 $ tex4ebook -l -f epub3 demo.tex

结果如下:

在此处输入图片描述

相关内容