2

I've been trying to follow a tutorial here: github.com/michal-h21/helpers4ht/wiki/tex4ht-tutorial

My MWE files below produce a valid output with the text from the command, but I was expecting the class (i.e. the document style for this text) to be assigned in accordance with the command used, similar to the way this (Customizing ODT output from mk4ht oolatex) example used environments to change the class which corresponded to a style name in the associated word file (referenced in the build instruction odttemplate). I can't, however, tell if the code is unable to interrupt the body text style or whether the style is not being assigned because of an error in the code.

Main.tex

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} 
\usepackage{testpkg}
\begin{document} 

    Test

    Some text in English, 

    \SectionTitleTest{Test}

    More text

\end{document}

testpkg.sty

\ProvidesPackage{testpkg} 

    \RequirePackage{xparse,expl3}
    \DeclareDocumentCommand{\SectionTitleTest}{m}{#1}

\endinput

testpkg.cfg

\Preamble{xhtml}

% to simplify things
\def\myendpar{\ifvmode\IgnorePar\fi\EndP}

\Configure{SectionTitleTest}{\HCode{<span class="SectionTitleTest">}\NoFonts}{\EndNoFonts\HCode{</span>}}% tried without NoFonts code to no avail either
\Css{.SectionTitleTest{font-style:SectionTitleTest;}}

\begin{document}

\EndPreamble

testpkg.4ht

% provide configure for new command. we can choose any name
% but most convenient is to name hooks after redefined command
% we declare two hooks, to be inserted before and after the command

\NewConfigure{SectionTitleTest}{2}

% now we need to redefine \SectionTitleTest. save it to tmp command
% note that `:` can be part of command name in `.4ht` files.
\let\tmp:SectionTitleTest\SectionTitleTest

% now insert the hooks. they are named as \a:hook, \b:hook, ..., \h:hook
% depending on how many hooks were declared

\renewcommand\SectionTitleTest{\a:SectionTitleTest\tmp:SectionTitleTest\b:SectionTitleTest} 
EngBIRD
  • 3,985

1 Answers1

2

There are several issues with your configuration. Fist of all, in testpkg.4ht:

\renewcommand\SectionTitleTest{\a:SectionTitleTest\tmp:SectionTitleTest\b:SectionTitleTest} 

The original \SectionTitleTest takes one argument, but you redefine it as argument free. Because of this, all content of the configuration is inserted before the text you pass as argument to \SectionTitleTest. The correct redefinition would look like this:

% provide configure for new command. we can choose any name
% but most convenient is to name hooks after redefined command
% we declare two hooks, to be inserted before and after the command

\NewConfigure{SectionTitleTest}{2}

% now we need to redefine \SectionTitleTest. save it to tmp command
% note that `:` can be part of command name in `.4ht` files.
\let\tmp:SectionTitleTest\SectionTitleTest

% now insert the hooks. they are named as \a:hook, \b:hook, ..., \h:hook
% depending on how many hooks were declared

\renewcommand\SectionTitleTest[1]{\a:SectionTitleTest\tmp:SectionTitleTest{#1}\b:SectionTitleTest}

The second issue is that you use HTML tags. In the ODT output, you must use OpenDocument tags. Also, ODT doesn't support CSS, it uses special XML instead.

The updated .cfg file:

\Preamble{xhtml}

% to simplify things
\def\myendpar{\ifvmode\IgnorePar\fi\EndP}

\Configure{SectionTitleTest}{\myendpar\HCode{<text:p text:style-name="section-title">}}{\HCode{</text:p>}}
\NewConfigureOO{section-title}
\ConfigureOO{section-title}{<style:style style:name="section-title" style:family="paragraph" style:class="text">
    <style:text-properties style:text-underline-style="solid"
                           style:text-underline-width="auto"
                           style:text-underline-color="font-color"
    />
</style:style>}

\begin{document}

\EndPreamble

The <text:p> element is used for paragraphs. It needs to use the text:style-name attribute to select the paragraph style.

The paragraph style is defined using \NewConfigureOO and configured using \ConfigureOO. I've used the same name as in text:style-name, but it is not necessary. The correct style name must be used <style:style style:name="section-title"> though.

The provided style configures the section title to produce underlined text. Use <style:text-properties> attributes to change the formatting according to your needs.

This is the result in LO:

enter image description here

michal.h21
  • 50,697
  • Are the redefinitions in the 4ht file, and the NewConfigureOO & ConfigureOO in the config file, necessary if the command was a basic command like \section{} which I just want to remap to a style in an odt file that would be identified through the compile command --format odt+odttemplate + odttemplate=template.odt? For example, I may have \section{introduction} in latex, but my organization's document template may not expect me to use Heading-2 but CustomSectionHeading -2. I'm hoping not to have to create 4ht and sty files except for custom commands separately packaged. Thanks! – EngBIRD Jul 06 '20 at 02:14
  • @EngBIRD sorry for the late reply. you can then configure just the section in the .cfg file. Look for \Configure{section} in ooffice.4ht, copy it and change the text:style-name attribute to a new name. – michal.h21 Jul 13 '20 at 13:54
  • Thanks, I've found the file and the command, it's definitely more fulsome than my first attempt. I had tried \def\myendpar{\ifvmode\IgnorePar\fi\EndP} \Configure{section}{\myendpar\HCode{<text:p text:style-name="Illustration">}}{\HCode{</text:p>}} which didn't work. There are two text:style blocks so I'll play around with which I substitute before confirming my success. – EngBIRD Jul 13 '20 at 15:26
  • @EngBIRD I think you should pass four parameters to \Configure{section}, not just two. – michal.h21 Jul 13 '20 at 15:45
  • I am a bit lost in the comments, sorry. Maybe it would be best to post it as a new question? – michal.h21 Aug 17 '20 at 08:01
  • Thanks for looking, I've cleaned up the comments and posted a new question: https://tex.stackexchange.com/questions/559032/customizing-existing-single-line-classes-in-odt-output-from-mk4ht-oolatex . Thanks for your help. – EngBIRD Aug 17 '20 at 18:17