如何在将 LaTeX 文件转换为 ODT 时保存索引?

如何在将 LaTeX 文件转换为 ODT 时保存索引?

最近,我完成了一个 LaTeX 文档,其中我使用该\splitidx包创建了两个索引,索引条目以\sindex术语标记。例如,

\documentclass[b5paper,polish]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{splitidx}
\makeindex
\newindex[Alphabetical index]{idx}
\newindex[Films index]{film}
\begin{document}
This is the first entry\sindex[idx]{The first entry}. And this is the 
second one\sindex[film]{The second entry}.
\printindex{idx}
\printindex{film}
\end{document}

但是,由于发布问题,我必须至少将文档转换为 ODT 文件。转换参考书目并不是什么大问题;主要问题是转换后的文件(使用 Pandoc 或 tex4ht)根本不包含索引。因此,我想知道是否可以强制上述任何转换器将索引条目放入 ODT 文件中。或者有没有更间接的方法可以做到这一点?我将不胜感激任何帮助。

答案1

我能够使用 TeX4ht 生成索引。您需要开发版本的make4ht,因为我必须修复有关 ODT 输出中的超链接的一些问题。

它还需要更新的oo-text.4xt文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE xtpipes SYSTEM "xtpipes.dtd" >
<!-- oo-text.4xt (2020-02-25-09:37), generated from tex4ht-oo-xtpipes.tex
     Copyright (C) 2009-2013 TeX Users Group
     Copyright (C) 2006-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. -->
<xtpipes preamble="yes" signature="oo-text.4xt (2020-02-25-09:37)">
   <sax content-handler="xtpipes.util.ScriptsManager,tex4ht.OoFilter"
        lexical-handler="xtpipes.util.ScriptsManagerLH" >
      <script element="text:bibliography-mark" >
         <set name="bib-mark" >
   <![CDATA[
   <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

      xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
      xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
      xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
      xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
      xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
      xmlns:fo="http://www.w3.org/1999/XSL/Format"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:dc="http://purl.org/dc/elements/1.1/"
      xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
      xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
      xmlns:svg="http://www.w3.org/2000/svg"
      xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
      xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
      xmlns:math="http://www.w3.org/1998/Math/MathML"
      xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
      xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
      xmlns:ooo="http://openoffice.org/2004/office"
      xmlns:ooow="http://openoffice.org/2004/writer"
      xmlns:oooc="http://openoffice.org/2004/calc"
      xmlns:dom="http://www.w3.org/2001/xml-events"
      xmlns:xforms="http://www.w3.org/2002/xforms"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   >
      <xsl:output omit-xml-declaration = "yes" />

    <xsl:template match="text:bibliography-mark" >
   <xsl:copy>
     <xsl:attribute name="text:identifier">
        <xsl:value-of select="normalize-space(.)" />
     </xsl:attribute>
     <xsl:apply-templates select="@*" />
   </xsl:copy>
   <text:span>
      <xsl:attribute name="text:style-name">
         <xsl:text>reference-ref</xsl:text>
      </xsl:attribute>
      <text:reference-ref>
        <xsl:attribute name="text:ref-name">
              <xsl:value-of
                 select="text:span/text:reference-ref/@text:ref-name"/>
        </xsl:attribute>
        <xsl:attribute name="text:reference-format">
              <xsl:text>text</xsl:text>
        </xsl:attribute>
        <xsl:text> </xsl:text>
      </text:reference-ref>
   </text:span>
</xsl:template>

         <xsl:template match="*|@*|text()|comment()" >
        <xsl:copy>
          <xsl:apply-templates select="*|@*|text()|comment()" />
        </xsl:copy>
      </xsl:template>
   </xsl:stylesheet>
]]>

</set>
<xslt name="." xml="." xsl="bib-mark" />

      </script>
      <script element="table:table" >
         <dom name="." xml="." method="table" class="tex4ht.OoUtilities" />

      </script>
      <script element="text:p" >
         <set name="text-p" >
   <![CDATA[
   <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

      xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
      xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
      xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
      xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
      xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
      xmlns:fo="http://www.w3.org/1999/XSL/Format"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:dc="http://purl.org/dc/elements/1.1/"
      xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
      xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
      xmlns:svg="http://www.w3.org/2000/svg"
      xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
      xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
      xmlns:math="http://www.w3.org/1998/Math/MathML"
      xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
      xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
      xmlns:ooo="http://openoffice.org/2004/office"
      xmlns:ooow="http://openoffice.org/2004/writer"
      xmlns:oooc="http://openoffice.org/2004/calc"
      xmlns:dom="http://www.w3.org/2001/xml-events"
      xmlns:xforms="http://www.w3.org/2002/xforms"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   >
      <xsl:output omit-xml-declaration = "yes" />

   <xsl:template match="text:p[ (count(child::*)=1)
                             and child::draw:frame
                             and parent::text:p
                           ]" >
    <xsl:apply-templates select="child::draw:frame" />
</xsl:template>
<xsl:template match="text:p[
     child::text:p
   and
     (count(child::text:p) = count(child::*))
 ]" >
   <!--xsl:copy>
      <xsl:attribute name="text:style-name">
          <xsl:text>start_</xsl:text>
         <xsl:value-of select="@text:style-name" />
      </xsl:attribute-->
      <xsl:comment>
          <xsl:text>start </xsl:text>
          <xsl:value-of select=" @text:style-name " />
     </xsl:comment>
   <!--/xsl:copy-->
       <xsl:apply-templates select="*|text()|comment()" mode="nested-p" />
   <!--xsl:copy>
      <xsl:attribute name="text:style-name">
          <xsl:text>end</xsl:text>
         <xsl:value-of select="@text:style-name" />
      </xsl:attribute-->
      <xsl:comment>
          <xsl:text>end_</xsl:text>
          <xsl:value-of select=" @text:style-name " />
     </xsl:comment>
   <!--/xsl:copy-->
</xsl:template>
<xsl:template match="*[not(text:p)]|text()|comment()" mode="nested-p" >
  <xsl:copy/>
</xsl:template>
<xsl:template match="text:p" mode="nested-p" >
  <xsl:copy>
      <xsl:attribute name="text:style-name">
         <xsl:value-of select="parent::text:p/@text:style-name" />
         <xsl:text>_</xsl:text>
         <xsl:value-of select="@text:style-name" />
         <xsl:text>_</xsl:text>
         <xsl:value-of select="child::*[1]/@text:style-name" />
      </xsl:attribute>
      <xsl:apply-templates select="@*[ name() != 'text:style-name' ]" />
      <xsl:apply-templates select="*|text()|comment()" />
  </xsl:copy>
</xsl:template>

   <xsl:template
    match="text:p[
              parent::text:p/@text:style-name='printthebibliography-dd'
          ]"
    mode="nested-p" >
  <xsl:copy>
      <xsl:attribute name="text:style-name">
         <xsl:value-of select="'printthebibliography-dd'" />
      </xsl:attribute>
      <xsl:apply-templates select="@*[ name() != 'text:style-name' ]" />
      <xsl:apply-templates select="*|text()|comment()" />
  </xsl:copy>
</xsl:template>

   <xsl:template match="text:p[  child::text:span[
                       starts-with(@text:style-name, 'multicolumn-')
                     ] ]" >
   <xsl:copy>
       <xsl:apply-templates
            select="child::text:span[
                       starts-with(@text:style-name, 'multicolumn-') ]
                    /@*
                   " />
       <xsl:apply-templates select="*|text()|comment()" />
   </xsl:copy>
</xsl:template>
<xsl:template match="text:span[
                       starts-with(@text:style-name, 'multicolumn-') ]" />

         <xsl:template match="*|@*|text()|comment()" >
        <xsl:copy>
          <xsl:apply-templates select="*|@*|text()|comment()" />
        </xsl:copy>
      </xsl:template>
   </xsl:stylesheet>
]]>

</set>
<xslt name="." xml="." xsl="text-p" />

      </script>
      <script element="text:h" >
         <set name="headers" >
   <![CDATA[
   <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

      xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
      xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
      xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
      xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
      xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
      xmlns:fo="http://www.w3.org/1999/XSL/Format"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:dc="http://purl.org/dc/elements/1.1/"
      xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
      xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
      xmlns:svg="http://www.w3.org/2000/svg"
      xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
      xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
      xmlns:math="http://www.w3.org/1998/Math/MathML"
      xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
      xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
      xmlns:ooo="http://openoffice.org/2004/office"
      xmlns:ooow="http://openoffice.org/2004/writer"
      xmlns:oooc="http://openoffice.org/2004/calc"
      xmlns:dom="http://www.w3.org/2001/xml-events"
      xmlns:xforms="http://www.w3.org/2002/xforms"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   >
      <xsl:output omit-xml-declaration = "yes" />

   <xsl:template match="text:h[@text:style-name='algorithmic-dt']" >
  <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:apply-templates select="*|text()|comment()" mode="algorithmic"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="*|@*|comment()" mode="algorithmic" >
  <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:apply-templates select="*|text()|comment()" mode="algorithmic"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="text()" mode="algorithmic" >
   <xsl:value-of select="translate(.,' ','&#xA0;')"/>
</xsl:template>

   <xsl:template match="text:h[
     (@text:style-name='printthebibliography-dt')
     and
     (normalize-space()='')
]" >
</xsl:template>

         <xsl:template match="*|@*|text()|comment()" >
        <xsl:copy>
          <xsl:apply-templates select="*|@*|text()|comment()" />
        </xsl:copy>
      </xsl:template>
   </xsl:stylesheet>
]]>

</set>
<xslt name="." xml="." xsl="headers" />

      </script>
   </sax>
</xtpipes>

该文件用于ODT文件的一些验证问题。

我还必须修复你的 TeX 文件,因为你使用了错误版本的\printindex命令。你需要使用\printindex[film]而不是\printindex{film}

% https://tex.stackexchange.com/q/529524/2891
\documentclass[b5paper,polish]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{splitidx}
\makeindex
\newindex[Alphabetical index]{idx}
\newindex[Films index]{film}
\begin{document}
This is the first entry\sindex[idx]{The first entry}. And this is the 
second one\sindex[film]{The second entry}.
\printindex[idx]
\printindex[film]
\end{document}

要执行索引命令,请使用以下构建文件mybuild.lua

Make:htlatex {}
Make:xindex {}
Make:htlatex {}

使用以下方式编译文档

make4ht -f odt -e mybuild.lua filename.tex

这是在 LibreOffice 中的结果:

在此处输入图片描述

相关内容