# customized thebibliography environment in tex4ht

I am trying to create the following xml structure through tex4ht

<ref-list>
<title>Bibliography</title>
<ref id="bib_ch0_001" bibid="aa"><label>[1]</label><mixed-citation publication-type="">Sample Bib text</mixed-citation></ref>
<ref id="bib_ch0_002" bibid="ab"><label>[2]</label><mixed-citation publication-type="">Sample Bib text</mixed-citation></ref>
<ref id="bib_ch0_003" bibid="cb"><label>[3]</label><mixed-citation publication-type="">Sample Bib text</mixed-citation></ref>
</reflist>


MWE

\documentclass{book}
\usepackage{Macros}
\begin{document}

\cite{aa}

\begin{thebibliography}{99}
\bibitem{aa}Sample Bib text

\bibitem{ab}Sample Bib text

\bibitem{ac}Sample Bib text
\end{thebibliography}


I have created the macros.4ht with the below structure

\newcount\bibcountid
\newcount\bibitemno
\ConfigureList{thebibliography}%
{\Configure{likechapter}{}{}{}{}%
\HCode{<ref-list>\Hnewline<title>Bibliography</title>}
\let\en:bib=\empty}
{\en:bib
\Close:UntilEnd
\HCode{</reflist>}
}
\def\en:bib{\HCode{\Hnewline</mixed-citation>\Hnewline</ref>}\let\en:bib=\empty}%
\HCode{\Hnewline<ref id="bib_ch\thechapter_00\the\bibcountid" bibid="">}%
\HCode{<label>}%
}%
{\HCode{</label><mixed-citation publication-type="">}}

1. I am getting the output at the same time Head is repeated like below

<h2 class="likechapterHead">Bibliography</h2>

<ref-list>

<title>Bibliography</title>

1. also i need to retain the original label id of \bibitem{labelid} in the xmltag <ref id="bib_ch0_001" bibid="aa">

for this i just copied from latex.4ht and added the \def\bibargforht{#2}' in the\@lbibitem

\def\@lbibitem[#1]#2{{\:SUBOff\:SUPOff\edef\:tempb{{#2}}%
\pend:def\:tempb{\o:@lbibitem:[#1]}%
\global\let\:temp\:tempb}%
\def\bibargforht{#2}\setb:anc{#2}\:temp\bib:anc \let\AnchorLabel\sv:anc\ignorespaces}


when i am using the natbib.sty i am getting the compilation error!

How to configure these 2 points correctly in the bibliography environment?

You can try the following Macros.4ht:

\newcount\bibcountid
\newcount\bibitemno
\def\eattwo#1#2{}

\def\saved:label{\the\value{\@listctr}}

\ConfigureEnv{thebibliography}{
\ifvmode\IgnorePar\fi\EndP%
\HCode{<reflist>}%
\bgroup%
% Local redefinition for \chapter* configuration
\Configure{likechapter}{}{}{\ifvmode\IgnorePar\fi\EndP\HCode{<title>}}{\HCode{</title>}}%
\Configure{toTocLink}{\eattwo}{}% to remove <a id=""> tag
\let\en:bib=\empty%
}
{\ifvmode\IgnorePar\fi\EndP%
\HCode{</reflist>}\egroup%
}%
{}{}

\ConfigureList{thebibliography}{}{\en:bib\Close:UntilEnd}
\DeleteMark}%
{}



There are several changes:

• To get the bibliography title only once, the configuration for \chapter* command is locally redefined to print <title> element in configuration for thebibliography element. It is used also to insert the <reflist> element.

tex4ht linking mechanism is used to insert the <ref> element:

\LinkCommand\BibItemLink{ref,href,bibid}
...
\ConfigureList{thebibliography}{}{\en:bib\Close:UntilEnd}
\DeleteMark}%
{}
...


The \LinkCommand defines two new commands \BibItemLink and \EndBibItemLink. It inserts <ref> tag, with bibid parameter taking the id. We insert the \BibItemLink in \Configure{bibitem}, where linking command is expected.

\BibItemLink[ id="bib_ch\thechapter_00\the\bibcountid"]}


This will create following <ref> element:

 <ref
bibid="Xaa" id="bib_ch0_001"  >


Second part of bibitem configuration is used to insert the <label> and <mixed-citation>:

{\HCode{<label>[\saved:label]</label><mixed-citation publication-type="">}}


The bibitem label is removed from list configuration using \DeleteMark, because it would ruin our xml structure, so it has to be inserted manually at this place.

We must close <mixed-citation> and <ref> in the configuration of the thebibliography list:

 \ConfigureList{thebibliography}{}{\en:bib\Close:UntilEnd}
\DeleteMark}%


I realize that it is not really straightforward configuration, but we must use dirty tricks sometimes if we need to follow strict XML requirements which differ from the LaTeX input structure.

This is the resulting xml:

 <reflist>

<title><a
id="x1-1000"></a>Bibliography</title>
<ref
bibid="Xaa" id="bib_ch0_001"  ><label>[1]</label><mixed-citation publication-type="">Sample Bib text

</mixed-citation>
</ref>
<ref
bibid="Xab" id="bib_ch0_002"  ><label>[2]</label><mixed-citation publication-type="">Sample Bib text

</mixed-citation>
</ref>
<ref
bibid="Xac" id="bib_ch0_003"  ><label>[3]</label><mixed-citation publication-type="">Sample Bib text
</mixed-citation>
</ref>
</reflist>

• @michael: another one doubt what is the use of \Close:UntilEnd ? – vctu May 30 '17 at 5:50
• @michael: I have few more doubts: 1) In the original \bibitem label id is coming with preceding the letter "X" is it for linking purpose? is it possible to delete "X"? 2) the \EndBibItemLink, it will automatically created the end tag </ref>, whatever the element given in the \LinkCommand\BibItemLink{ref,href,bibid}, the end tag will be crated? – vctu May 30 '17 at 6:01
• @michael: when I am using \usepackage[numbers]{natbib} The opening tag is not printing in the xml file <ref bibid="Xab" id="bib_ch0_002" ><label>[2]</label>mixed-citation publication-type=""> ? – vctu May 30 '17 at 6:50
• @VenkatesanRamachandiran I used the \Close:UntilEnd because it was used in your original configuration. It is internal tex4ht command which is used when the output is saved in several files. – michal.h21 May 30 '17 at 13:03
• @VenkatesanRamachandiran I think the letter X is there to ensure that id is unique. It can be configured, but I can't find it how now. the </ref> is included automatically with \EndBibLink` – michal.h21 May 30 '17 at 13:07