# Xelatex conversion to .doc/.odt/.rtf/.html after running biblatex?

This question is similar to many others posted on these forums, but the answers I have seen have not solved my problem (e.g. at Converting Latex to MsWord .doc or .rtf). Please forgive me if this is repetitive, and point me in the direction of its solution. I will be deeply indebted to anyone who has ideas.

In brief, I wrote my dissertation in XeLaTeX largely because of the flexibility of biblatex. I work in a field wherein I have to cite a lot of East Asian source materials, and getting other reference managers to output these sorts of references correctly has been difficult. Biblatex works beautifully, and I'd be sorry to give it up.

However, now that I am on the other side of my dissertation, I need to submit papers (and manuscripts) for publication. In my backwards field, such papers very often need to be submitted as a Word document.

I need now to consider whether I can keep Xelatex, biblatex, and the large bibliography file I created during my dissertation as part of my workflow--even if the end product sometimes has to be a Word-readable and Word-editable document. I know of several programs that can convert an uncompiled Xelatex document to other formats (pandoc, etc.). And I know that tex4ht can convert some LaTeX documents to html or odt after running biber. But for reasons I can't understand (that may have to do with incompatibilities between Tex4ht and xelatex?), it does not seem to work on my stuff. (I am working on Windows 10 with MikTex.)

The documents I need to be able to produce are fairly simple (although cjk bibliography requires some definitions to be able to work). I don't have graphs, charts, or images to include: just Chinese and Japanese texts.

Here is an extremely minimal example that I cannot get to work. This is the latex file:

\documentclass{article}
\usepackage{xeCJK}
\setmainfont[Ligatures={Common, TeX}]{Times New Roman}
\setCJKmainfont[Scale=1]{SimSun}

\begin{document}

Zhupo shihua 竹坡詩話

\end{document}


Of course, this extremely minimal code compiles perfectly into a pdf with Xelatex. When I then go to run htlatex on it (i.e. in the command prompt, I type: htlatex test.tex), I get the following error message:

C:\Users\lbxxx>latex  \makeatletter\def\HCode{\futurelet\HCode\HChar}\def\HChar{\ifx"\HCode\def\HCode"##1"{\Link##1}\expandafter\HCode\else\expandafter\Link\fi}\def\Link#1.a.b.c.{\g@addto@macro\@documentclasshook{\RequirePackage[#1,html]{tex4ht}}\let\HCode\documentstyle\def\documentstyle{\let\documentstyle\HCode\expandafter\def\csname tex4ht\endcsname{#1,html}\def\HCode####1{\documentstyle[tex4ht,}\@ifnextchar[{\HCode}{\documentstyle[tex4ht]}}}\makeatother\HCode .a.b.c.\input  "C:\Users\lbxxx\Desktop\Tex Testing\onlinetest.tex"
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6300 64-bit)
entering extended mode
LaTeX2e <2017-04-15>
Babel <3.9r> and hyphenation patterns for 72 language(s) loaded.
! Undefined control sequence.
<*> ...}\makeatother\HCode .a.b.c.\input  C:\Users
\lbxxx\Desktop\Tex Testing...

?


I can hit r several times and nothing at all comes out. Same basic thing for htxelatex.

Of course, the actual files I would like to be able to convert this way are much more complicated. Here would be a minimal example, this time including footnotes and bibliography (many thanks to moewe for much of this code):

\documentclass{article}
\usepackage{xltxtra}
\usepackage[fallback]{xeCJK}
\setmainfont[Ligatures={Common, TeX}]{Times New Roman}
\setCJKmainfont[Scale=1]{SimSun}

\makeatletter
\newcommand\blx@unitmark{23sp}
\makeatother

\usepackage{filecontents}
\begin{filecontents*}{chicago-notes.dbx}
\ProvidesFile{chicago-notes.dbx}[2016/07/24 extended name format for biblatex]
\DeclareDatamodelConstant[type=list]{nameparts}{prefix,family,suffix,given,cjk}
\end{filecontents*}
\begin{filecontents*}{\jobname.bib}
@book{zhaos,
title={A Nation-State by Construction: Dynamics of Modern Chinese Nationalism},
author={given=Suisheng, family=Zhao, cjk=趙歲升},
isbn={9780804750011},
year={2011},
publisher={Stanford University Press}}
@book{ZhupoShihua,
author = {family=Zhou, cjk=周紫芝, given=Zizhi},
title = {Zhupo shihua},
series = {Yinying Wenyuange Siku quanshu edition},
year = {1985},
publisher = {Taiwan shangwu yinshu guan}
}
@book{jim,
title={Lorem},
author={Smith, Junior, Jim},
year={2009},
publisher={Stanford University Press}}
\end{filecontents*}

\usepackage[notes,strict,annotation,cmsdate=both,isbn=false, backend=biber]{biblatex-chicago}

% Based on definitions from biblatex.def

\newbibmacro*{name:cjk}[3]{%
\usebibmacro{name:delim}{#2#3#1}%
\usebibmacro{name:hook}{#2#3#1}%
\mkbibnamefamily{#1}%
\ifdefvoid{#2}{}{\bibnamedelimd\mkbibnamegiven{#2}}%
\ifdefvoid{#3}{}{\bibnamedelimd\mkbibnamecjk{#3}}}

\DeclareNameFormat{given-family}{%
\ifdefvoid{\namepartcjk}
{\iffirstinits
{\usebibmacro{name:given-family}
{\namepartfamily}
{\namepartgiveni}
{\namepartprefix}
{\namepartsuffix}}
{\usebibmacro{name:given-family}
{\namepartfamily}
{\namepartgiven}
{\namepartprefix}
{\namepartsuffix}}}
{\usebibmacro{name:cjk}{\namepartfamily}{\namepartgiven}{\namepartcjk}}%
\usebibmacro{name:andothers}}

\DeclareNameFormat{family-given}{%
\ifdefvoid{\namepartcjk}
{\iffirstinits
{\usebibmacro{name:family-given}
{\namepartfamily}
{\namepartgiveni}
{\namepartprefix}
{\namepartsuffix}}
{\usebibmacro{name:family-given}
{\namepartfamily}
{\namepartgiven}
{\namepartprefix}
{\namepartsuffix}}}
{\usebibmacro{name:cjk}{\namepartfamily}{\namepartgiven}{\namepartcjk}}%
\usebibmacro{name:andothers}}

\DeclareNameFormat{family-given/given-family}{%
\ifdefvoid{\namepartcjk}
{\ifnumequal{\value{listcount}}{1}
{\iffirstinits
{\usebibmacro{name:family-given}
{\namepartfamily}
{\namepartgiveni}
{\namepartprefix}
{\namepartsuffix}}
{\usebibmacro{name:family-given}
{\namepartfamily}
{\namepartgiven}
{\namepartprefix}
{\namepartsuffix}}
\ifboolexpe{%
test {\ifdefvoid\namepartgiven}
and
test {\ifdefvoid\namepartprefix}}
{}
{\usebibmacro{name:revsdelim}}}
{\iffirstinits
{\usebibmacro{name:given-family}
{\namepartfamily}
{\namepartgiveni}
{\namepartprefix}
{\namepartsuffix}}
{\usebibmacro{name:given-family}
{\namepartfamily}
{\namepartgiven}
{\namepartprefix}
{\namepartsuffix}}}}
{\usebibmacro{name:cjk}{\namepartfamily}{\namepartgiven}{\namepartcjk}}%
\usebibmacro{name:andothers}}

\DeclareFieldFormat[book]{title}{%
\mkbibemph{#1}%

\DeclareFieldFormat{booktitle}{%
\mkbibemph{#1}%

\DeclareFieldFormat{maintitle}{%
\mkbibemph{#1}%

\DeclareFieldFormat[article]{title}{%
\iffieldundef{title}{}{\mkbibquote{#1}}%

\DeclareFieldFormat{usere}{\mkbibparens{#1}}

\begin{document}
\cite{zhaos,jim,ZhupoShihua}

\cite{zhaos,jim,ZhupoShihua}\footnote{\cite{zhaos,jim,ZhupoShihua}}

\printbibliography
\end{document}


So, in short, is there any way to be able to continue to use biblatex with my cjk-materials but output to .html, .odt, .rtf, or .doc? I am not particular about the formatting of the final document, except that the text needs to be there and footnotes need to be footnotes. Please do not suggest converting the final pdf to word--I'd rather type in all my bibliographic material by hand than have to deal with all the headaches that causes.

Anyone who can solve this will have my eternal gratitude.

• htlatex is certainly the wrong application for a xelatex file. Also avoid spaces in folder and file names, and don't pass absolute pathes. Call it as htlatex ... file.tex. I get errors at the cjk-font but I don't know if this is a bug or due to wrong use. We will have to wait for @michal-h21 to show the current state-of-the-art-call. – Ulrike Fischer May 3 '17 at 8:47
• @UlrikeFischer yes, htlatex processes file with latex, so it certainly can't work. One should use make4ht -x to compile XeLaTeX file. The issue with path in Windows is strange, I don't remember if I ever seen that. This might be some htlatex in Miktex issue, I guess it shouldn't happen with make4ht. Other than that, the file almost works, it needs only some fixes for xeCJK and biblatex-chicago. I am working on that. – michal.h21 May 3 '17 at 9:57

if mode:match "odt" then
settings.tex4ht_sty_par = settings.tex4ht_sty_par .. ",ooffice"
settings.t4ht_par = settings.t4ht_par  .. " -coo -cooxtpipes"
end

if mode:match "draft" then
Make:htlatex {}
else
Make:htlatex {}
Make:biber {}
Make:htlatex {}
end


With make4ht, you can use --mode or -m command line option. This selects which compiling sequence will be used. You will also need to use -u for Unicode output and -x for XeLaTeX. Basic compilation would be thus:

make4ht -ux -e mybuild.mk4 filename.tex


This will compile the document as HTML and it will run biber after first LaTeX compilation. On subsequent compilations, when the bibliography doesn't change and it is thus unnecessary to run biber, you can use draft mode:

make4ht -uxm draft -e mybuild.mk4 filename.tex


To get an ODT document, you can add odt to the draft option:

make4ht -uxm odt-draft -e mybuild.mk4 filename.tex


This is the resulting HTML:

Edit:

It seems that your distribution is missing some files distributed with tex4ht, so you may also need this one, usepackage-fontspec.4ht. It seems that there is a limit on how many characters post can contain, so I had to upload it to Gist.

• thank you so much for looking into this. Unfortunately, I'm still unable to make it work on my machine--make4ht is returning all sorts of errors trying to find the files. I'm guessing you probably aren't on Windows 10 yourself, but do you have any advice as to where I should put usepackage.4ht, biblatex.4ht, mybuild.mk4, and the tex file I want to compile? Also, I got rid of the space in my folder name, but this doesn't solve the input problem. And a further input problem comes up when mk4ht tries to find biblatex\lbx\english. On Windows, MikTex path is \MikTex 2.9\--with space – LukeB May 3 '17 at 15:05
• @LukeB I would create some temporary directory directly in the root dir, like c:\mytexfiles and put all the files here. – michal.h21 May 3 '17 at 15:11
• That's actually what I am doing. C:\textesting; everything is in there. So I open cmd, type prompt C:\WINDOWS\system32>make4ht -ux -e C:\textesting\mybuild.mk4 C:\textesting\onlinetest.tex . It says "using build file C/textesting/mybuild.mk4" but then gives me "undefined control sequence charset=utf-8.a.b.c.\input C:\textesting \onlinetest.tex – LukeB May 3 '17 at 15:18
• I can actually get it to input the file if I don't have it find it the first time, e.g. "C\WINDOWS\system32>make4ht -ux -e C:\textesting\mybuild.mk4 onlinetest.tex. That returns "cannot find file online test.tex' and asks for another input file name. If I then type C:\textesting\onlinetest.tex, I can get it to run, but it says "Package biblatex Error: Language 'english' not found" even though it is looking in the right place and english.lbx is there when I navigate to the folder. These seem to be Windows specific problems, right? – LukeB May 3 '17 at 15:25
• @LukeB you don't need to use full paths for filename and the build file. so use just make4ht -ux -e mybuild.mk4 onlinetest.tex. But you should go to the directory with your files using cd command, because LaTeX can't find supporting files if you execute it from another directory (C\WINDOWS\system32` in your case) – michal.h21 May 3 '17 at 16:26