2

I have a .tex file as shown

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tasks}
\usepackage{xcolor}
\usepackage{float}
\begin{document}
\begin{enumerate}

    \item Question 1
    \begin{tasks}(1)
        \task[\textbf{a.}]Option 1
        \task[\textbf{b.}]Option 2
        \task[\textbf{c.}]Option 3
        \task[\textbf{d.}]Option 4
        
    \end{tasks}

    \item Question 2
    \begin{tasks}(2)
    \task[\textbf{a.}]Option A
    \task[\textbf{b.}]Option B
    \task[\textbf{c.}]Option C
    \task[\textbf{d.}]Option D
    
\end{tasks}
    
\end{enumerate}


\end{document}

I'm attempting to convert a La-TeX document of mine to HTML using make4ht.The PDF output look like this,enter image description here but the corresponding HTML code has is producing something different. enter image description here

 <!DOCTYPE html>
    <html lang="en-US">

    
    <body>
    <ol class="enumerate1">
    <li class="enumerate" id="x1-3x1">Question 1 <a id="x1-4r1"></a> <!--l. 17-->
    <p class="noindent"><!--l. 17--></p>
    <p class="noindent">Option 1</p>
    <!--l. 17-->
    <p class="noindent"><span class="rm-lmbx-12">a.</span> <a id="x1-5r2"></a></p>
    <!--l. 17-->
    <p class="noindent"></p>
    <!--l. 17-->
    <p class="noindent">Option 2</p>
    <!--l. 17-->
    <p class="noindent"><span class="rm-lmbx-12">b.</span> <a id="x1-6r3"></a></p>
    <!--l. 17-->
    <p class="noindent"></p>
    <!--l. 17-->
    <p class="noindent">Option 3</p>
    <!--l. 17-->
    <p class="noindent"><span class="rm-lmbx-12">c.</span> <a id="x1-7r4"></a></p>
    <!--l. 17-->
    <p class="noindent"></p>
    <!--l. 17-->
    <p class="noindent">Option 4</p>
    <!--l. 17-->
    <p class="noindent"><span class="rm-lmbx-12">d.</span></p>
    </li>
    <li class="enumerate" id="x1-9x2">Question 2 <a id="x1-10r1"></a> <!--l. 26-->
    <p class="noindent"><!--l. 26--></p>
    <p class="noindent">Option A</p>
    <!--l. 26-->
    <p class="noindent"><span class="rm-lmbx-12">a.</span><a id="x1-11r2"></a></p>
    <!--l. 26-->
    <p class="noindent">Option B</p>
    <!--l. 26-->
    <p class="noindent"><span class="rm-lmbx-12">b.</span> <a id="x1-12r3"></a></p>
    <!--l. 26-->
    <p class="noindent"></p>
    <!--l. 26-->
    <p class="noindent">Option C</p>
    <!--l. 26-->
    <p class="noindent"><span class="rm-lmbx-12">c.</span><a id="x1-13r4"></a></p>
    <!--l. 26-->
    <p class="noindent">Option D</p>
    <!--l. 26-->
    <p class="noindent"><span class="rm-lmbx-12">d.</span></p>
    </li>
    </ol>
    </body>
    </html>

I don't mind if make4ht cannot reproduce the multi column behavior of task package but is there any way to align the option label and option.What config file can do the job ?

1 Answer 1

4

It seems that the tasks package uses Xcoffins to typeset particular items. Because all formatting is stripped, you get the items in the order in which they were sent. This is why task text goes before the label.

As tasks environment starts list, we can output particular tasks as \item. We can then use standard TeX4ht commands to configure output HTML for this list.

Here is the configuration file for the Tasks package, tasks.4ht. Place it to the same directory as is your document.

\ExplSyntaxOn
% generate normal \item for tasks instead of coffins
\cs_set_protected:Npn \__tasks_task_fourht:nnn #1#2#3
  {\item[#1]{\__tasks_setup: #2 {#3}}}
\cs_generate_variant:Nn \__tasks_task_fourht:nnn {VVV}

\HLet\__tasks_task:nnn\__tasks_task_fourht:nnn
\HLet\__tasks_task:VVV\__tasks_task_fourht:VVV

\ExplSyntaxOff
\Hinput{tasks}
\endinput

It redefines the command that typeset tasks to output \item. Now, you can configure the list in the config.cfg configuration file:

\Preamble{xhtml}
\begin{document}
\makeatletter
\ConfigureList{tasks}
{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="tasks">}\def\end@task{}}
{\ifvmode\IgnorePar\fi\EndP\end@task\HCode{</div>}}
{\end@task\HCode{<div class="task"><span class="label">}\def\end@task{\HCode{</div>}}}
{\HCode{</span>}\noindent}
\makeatother
\EndPreamble

The \ConfigureList command is used to configure list environments:

\ConfigureList.....................5

   #1   type of list (e.g., itemize, description, enumerate,
                            list, trivlist)
   #2   before list
   #3   after  list
   #4   before label
   #5   after label

   \DeleteMark   removes latex's label; to be placed at the end of #4
   \AnchorLabel  defines an anchor for \label in current item; to
                 be placed in #5

The \end@task command is used to close the current task, this is why it is defined as empty at the beginning of the environment and initialized only after the first task is opened.

Compile using:

 make4ht -c config.cfg filename.tex

I've expanded the MWE:

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tasks}
\usepackage{xcolor}
\usepackage{float}
\begin{document}
\begin{enumerate}

    \item Question 1
    \begin{tasks}(1)
        \task[\textbf{a.}]Option 1
        \task[\textbf{b.}]Option 2
        \task[\textbf{c.}]Option 3
        \item[\textbf{d.}]Option 4
        \task Without explicit label
        \task! Full width
        \task*(7) Starred task
    \end{tasks}

    \item Question 2
    \begin{tasks}(2)
    \task[\textbf{a.}]Option A
    \task[\textbf{b.}]Option B
    \task[\textbf{c.}]Option C
    \task[\textbf{d.}]Option D
    
\end{tasks}
    
\end{enumerate}


\end{document}

And this is the result:

enter image description here

And HTML code:

  <ol class='enumerate1'>
<li class='enumerate' id='x1-3x1'>
      <!-- l. 10 --><p class='noindent'>Question 1
      </p><div class='tasks'><div class='task'><span class='label'>
      </span>
      </div><div class='task'><span class='label'>
   <span class='cmbx-12'>a.</span> </span>Option 1
      </div><div class='task'><span class='label'>
  <span class='cmbx-12'>b.</span> </span>Option 2
      </div><div class='task'><span class='label'>
   <span class='cmbx-12'>c.</span> </span>Option 3
      </div><div class='task'><span class='label'>
  <span class='cmbx-12'>d.</span> </span>Option 4<a id='x1-4r1'></a>
      </div><div class='task'><span class='label'>
  a) </span>Without explicit label<a id='x1-5r2'></a>
      </div><div class='task'><span class='label'>
  b) </span>Full width<a id='x1-6r3'></a>
      </div><div class='task'><span class='label'>
   c) </span>Starred task</div></div>
      </li>
<li class='enumerate' id='x1-8x2'>
      <!-- l. 21 --><p class='noindent'>Question 2
      </p><div class='tasks'><div class='task'><span class='label'>
      </span>
      </div><div class='task'><span class='label'>
   <span class='cmbx-12'>a.</span> </span>Option A
      </div><div class='task'><span class='label'>
  <span class='cmbx-12'>b.</span> </span>Option B
      </div><div class='task'><span class='label'>
   <span class='cmbx-12'>c.</span> </span>Option C
      </div><div class='task'><span class='label'>
  <span class='cmbx-12'>d.</span> </span>Option D
      </div></div>
      </li></ol>

Edit:

To get the multicolumn support try this config file:

\Preamble{xhtml}
\begin{document}
\makeatletter
\newcounter{taskid}
\ExplSyntaxOn
\ConfigureList{tasks}
{\stepcounter{taskid}\ifvmode\IgnorePar\fi\EndP\HCode{<div~class="tasks"~id="tasks-\arabic{taskid}">}\def\end@task{}}
{\ifvmode\IgnorePar\fi\EndP\end@task\HCode{</div>}
\Css{\#tasks-\arabic{taskid}{%
display:grid;
grid-template-columns: repeat(\int_use:N \l__tasks_columns_int, 1fr);
}}
}
{\end@task\HCode{<div~class="task" ><span~class="label">}\def\end@task{\HCode{</div>}}}
{\HCode{</span>}\noindent}
\ExplSyntaxOff
\Css{.task:first-child{display: none;}}
\makeatother
\EndPreamble

It declares unique ID for each tasks environment, and using CSS grid, it declares number of columns.

This is the result:

enter image description here

4
  • 1
    I don't have enough reputation to up vote. Thanks a lot michal. It saved my day @michal.h21
    – hfactor
    Feb 2, 2022 at 15:54
  • @PhysicsFuturing I've updated TeX4ht sources, so you will not need this code soon, if you update your TeX distribution.
    – michal.h21
    Feb 2, 2022 at 15:56
  • BTW, Is there any way to keep the multi column behavior in tasks @michal.h21
    – hfactor
    Feb 12, 2022 at 11:00
  • @hfactor sure, see the edited answer
    – michal.h21
    Feb 13, 2022 at 18:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .