设置

设置

几年前,我在tex4ht邮件列表上问过一个非常类似的问题,并得到了非常有帮助的回答来自 Michal Hoftich。他描述了使用 Unicode 的某些限制,tex4ht我怀疑这些限制可能在过去几年的开发中已经得到解决。因此,我本质上再次提出了同样的问题(尽管有些细节不同),希望了解是否有更简单或更自动化的方法将我的 XeLaTeX 文档转换为.docx格式。

设置

源文件

首先我生成一个文件,将 和head-simplified.tex的内容连接起来,其中三个文件如下:main.textail.tex

head-simplified.tex

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass[12pt]{memoir}
\usepackage{ifmtarg}
\usepackage{calc}

%Greek
\newcommand{\greek}[1]{#1}
\newcommand{\gk}[1]{#1}

%Arabic
\newcommand{\ar}[1]{#1}
\newenvironment{arab}{}{}

% Bibliography etc
\usepackage[american]{babel} 
\usepackage{csquotes}
\usepackage[notes,
        alldates=       comp,
        backend=        biber,
        bibwarn=        false,
        cmsdate=        both, % reprints: print `origdate` as well as `year`
        compresspages=  true,
        doi=            false,
        eprint=     false,
        ibidtracker=        true,
        inheritshorthand,       % feature added for me by D.Fussner
        isbn=           false,
        mincrossrefs =  2,
        shorthandibid,
        strict=     true,
        url=            true,
        usetranslator=  true,
        uniquename= true
    ]{biblatex-chicago}
\bibliography{word.converter.alexhistory-ent.bib}

\usepackage{textcomp} % For angle brackets `\textlangle` and `\textrangle`

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% code which C.V. Radhakrishnan added to make the unicode carry
% over smoothly to the word world.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\def\hshchr{\expandafter\@gobble\string\#}
\def\ampchr{\expandafter\@gobble\string\&}
\def\entity#1{\HCode{\ampchr\hshchr#1;}}
\makeatother

main.tex

\newcommand{%
\ص%
}[1]{$\rangle$#1$\langle$}


This is a test file using a macro whose name uses Arabic Unicode characters:
\ar{%
العربية%
}.
Then we can try it as a block quote:
\begin{quote}
\begin{arab}
% Text with Unicode macro:
أن يبد\ص{أ}
% Text without Unicode macro:
%أن يبدأ
\end{arab}
\end{quote}
If that doesn't work, here is some non-Arabic Unicode: \emph{kitābat al-ʿarabiyyah bi-l-ḥurūf al-lātīniyyah}, \gk{καὶ τὸ στοιχείοις Ἑλληνικοῖς γράφειν}...

tail.tex

\end{document}

转换

然后我使用一个名为的脚本texbody2word来自动执行指令,这些指令是感谢 CV Radhakrishnan、Michal Hoftich 和其他帮助实现这一转换的人提供的:

#!/bin/bash

#This takes a tex file with only the body
#(no header or `\end{document}`) and compiles
#it as a PDF

#filename without extension `.tex`
filename=$(basename "$1")
filename="${filename%.*}"
converterdir="/Users/alexandre/Dropbox/scripts/texbody2word-files"

#produce standalone tex file
cat $converterdir/head-tail/head-simplified.tex $filename.tex $converterdir/head-tail/tail.tex > $filename.out.tex

#convert unicode to ascii codes in tex file and bib file
perl $converterdir/utf2ent.pl $filename.out.tex > $filename.out-ent.tex
perl $converterdir/utf2ent.pl ~/Dropbox/bib-dbs/alexhistory.bib > word.converter.alexhistory-ent.bib

#apply tex4ht to tex file; then biber; then tex4ht again 
mk4ht oolatex $filename.out-ent "xhtml, superscript, charset=utf-8"  -utf8
biber $filename.out-ent
mk4ht oolatex $filename.out-ent "xhtml, superscript, charset=utf-8"  -utf8

#convert resulting odt file to docx
#(LibreOffice must NOT be open for this to work)
soffice --headless --invisible --convert-to docx $filename.out-ent.odt

结果

当我在我的系统上运行它时(使用 MacTeX 2018,软件包于 2018 年 1 月 13 日更新,使用 TeX Live Utility),输出是一个将.docx好的输出与像“ntity1589Δ1”这样的乱码混合在一起的文件(后跟两个无法渲染的“图像”并包含“无法显示图片”字样),这是因为我的设置无法理解在宏名中使用 Unicode 的宏,因为它将原始 TeX 文件中的所有 Unicode 字符转换为\entity{....}

在经过一番冗长的解释后,文件的剩余部分看起来是这样的:

这是一个使用宏的测试文件,其名称使用阿拉伯 Unicode 字符:العربية。然后我们可以将其作为块引用进行尝试:

entity1589

如果这不起作用,这里有一些非阿拉伯 Unicode:阿拉伯之春和拉提尼之春,然后我们再看看...

如您所见,除了无法处理 Unicode 命名的宏之外,其他都很好。

如果我注释掉 TeX 文件中使用 Unicode 字符定义宏的部分,那么最初的胡言乱语就会消失,只剩下以“这是一个测试文件”开头的最后一部分。

问题

我的问题是:有没有办法继续使用我的 Unicode 宏名并只改变工作流程以生成文件.docx

答案1

是的,这个过程可以大大简化。tex4ht使用 XeTeX 和 LuaTeX 引擎支持完整的 Unicode,因此无需使用 Perl 脚本进行预处理。工作流程的另一个变化是,推荐使用脚本tex4ht来生成 ODT 文档make4ht -f odt,这解决了转换过程中可能发生的许多问题。

我还会修改你的宏,最好使用 < 和 > 的文本命令而不是数学:

\makeatother\newcommand{%
 \ﺹ%
 }[1]{\textless #1\textgreater}

但我认为输出无论如何都是错误的。可能是因为输出中的字体形状,因为它在 XML 中的表示与呈现的文档中的表示不同。

XeTeX 支持的一个问题tex4ht是,必须使用命令明确加载对非拉丁语脚本的支持\xeuniuseblock。它可以在配置文件中使用,例如myconfig.cfg

\Preamble{xhtml}
\xeuniuseblock{Greek}
\xeuniuseblock{Arabic}
\makeatletter
\ConfigureEnv{arab}{\@rltrue}{\@rlfalse}{}{}
\makeatother
\begin{document}
\EndPreamble 

要使用的名称应该是 OpenType 脚本名称(这些名称用作scriptFontspec 字体选择命令中键的值)。

环境配置arab启用了封闭文本的 RTL 方向。这将生成右对齐文本,并且阿拉伯语的文本方向正确。

最后一点是 Biber 支持。可以在make4ht构建文件中请求它。甚至可以有条件地要求它,以节省编译时间。将以下文件保存为main.out.mk4

Make:add("biber", "biber ${input}")
if mode=="draft" then
Make:htlatex {}
else
Make:htlatex {}
Make:biber {}
Make:htlatex {}
Make:htlatex {}
end

可以使用以下命令编译该文件:

make4ht -ux -f odt  -c myconfig.cfg main.out.tex

后续编译可以使用draft只使用一次 LaTeX 编译而不使用 Biber 的模式,这样可以大大节省编译时间:

make4ht -uxm draft -f odt  -c myconfig.cfg main.out.tex

LibreOffice 中的结果:

在此处输入图片描述

可以看到宏的输出确实很奇怪。

相关内容