# Setting up a counter in Tex4HT Configuration to use in html

I want to add a collapsible panel header to some text, to do this I need to define some IDs, I can't use \thesubsubsection because it comes with dots (2.0.1 for example). So I want to use a simple counter such as:

\newcounter{index}
\setcounter{index}{1}
\newcommand{\ppindex}{\arabic{index}\stepcounter{index}}


I want to use a similar behavior in my htlatex configfile so \Index expands to this counter:

\ConfigureEnv{lstlisting}{\HCode{\Hnewline
<div class="panel-group" id="accordion\Index">\Hnewline
<div class="panel panel-default">\Hnewline
<h4 class="panel-title">\Hnewline
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion\Index" href="#collapse\Index">\Hnewline
Click to expand\Hnewline
</a>\Hnewline
</h4>\Hnewline
</div>\Hnewline
<div id="collapse\Index" class="panel-collapse collapse">\Hnewline
<div class="panel-body">\Hnewline
<pre class="prettyprint"><p>\Hnewline}}
{\HCode{</p></pre>\Hnewline
</div>\Hnewline
</div>\Hnewline
</div>\Hnewline
</div>\Hnewline}}{}{}


I was thinking of using \thesection, or \thesubsubsection in my case but I get a wrong number in my generated html. Even after redefining it to \arabic{subsubsection} gives me zero for \thesubsubsection.

Thanks @michal.h21, I wanted to make my code snippets collapsable.

And expand when you click it

Thanks to you and the \CurSecHaddr macro, I was able to change my Objects ids to the following:

id="accordionx1-80001", so now laTeX generates valid ids for each piece of lstlisting I add.

This is environment configuration for my lstlisting:

\ConfigureEnv{lstlisting}{\HCode{
<div class="panel panel-default">\Hnewline
<h4 class="panel-title">\Hnewline
<FONT style="font-size:15px" COLOR="\#FE2E2E">View Code</FONT> <span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-up"></i></span>\Hnewline
</h4>\Hnewline
</div>\Hnewline
</a>\Hnewline
<div id="collapse\CurSecHaddr" class="panel-collapse collapse">\Hnewline
<div class="panel-body">\Hnewline
<pre class="prettyprint"><p>\Hnewline}}
{\HCode{</p></pre>\Hnewline
</div>\Hnewline
</div>\Hnewline
</div>\Hnewline
</div>\Hnewline}}{}{}


It stills need some work done, but I am thing of doing that with post compilation scripting since I need some generated parts erased.

Of course there is more than this code, you need to add some JS code just for the glyphicon to work.

The site is http://claudiordgz.github.io/GoodrichTamassiaGoldwasser/ and the code is in https://github.com/claudiordgz/GoodrichTamassiaGoldwasser/tree/gh-pages in case anyone wants to see it. It is still in a very ugly alpha state but at least I can start pushing content like its hot.

• Hi Claudio, could you please add complete example? I don't really understand what you are trying to do. If you want to get id of current sectioning command, try \CurSecHaddr – michal.h21 Oct 31 '14 at 23:51
• @michal.h21 Really sorry for not answering before, I was out of town, please see edit. – Claudiordgz Nov 2 '14 at 15:38
• @michal.h21 Btw your suggestion works wonders, please put it as an answer. – Claudiordgz Nov 2 '14 at 15:46
• it's OK, I provided little bit different solution, but it still needs some clean up – michal.h21 Nov 2 '14 at 19:43

id of current sectioning command is stored in \CurSecHaddr macro, it may look in a following way:

x1-130006.7


this looks cryptic, but in fact x is just prefix, 1 is number of current file (because multiple files may be produced in tex4ht run), 13000 means that this is 13th sectioning command and 6.7 is subsection number.

I don't think you should use current section id to construct a new one, custom counter updated every listings usage would be better, I think:

\newcount\ppindex
\def\theppindex{\the\ppindex}


For stuff dealing with linking and id's it is better to use \Link command instead of direct use of <a> elements. In your case it doesn't matter, because you are linking to the parent element, but I will show it anyway.

This command is to be used in this way:

\Link[baseurl attributes]{target id}{current id}link text\EndLink


baseurl is useful only for linking to external pages, it is created automatically for pages from the same document.

it is possible to create new command which will use cross referencing mechanism but which will produce different element than <a>:

\LinkCommand\Accordion{div,href,id}


now we can use

\Accordion[ class="panel-group"]{}{accordion\theppindex}...\EndAccordion


and

 <div
id="accordion1" class="panel-group"  >...</div>


is produced. Similarly, for the second link, use:

\Link[ class="accordion-toggle" data-toggle="collapse" data-parent="\#accordion\theppindex"]{collapse\theppindex}{}


and

<a
href="#collapse1" class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" >


is produced.

Full example:

\Preamble{xhtml,info}
\newcount\ppindex
\def\theppindex{\the\ppindex}
\ConfigureEnv{lstlisting}{%
\ifvmode\IgnorePar\fi\EndP%
\steppindex%
\Accordion[ class="panel-group"]{}{accordion\theppindex}%
\HCode{%
<div class="panel panel-default">\Hnewline}
%<a class="accordion-toggle" data-toggle="collapse" data-parent="\#accordion\the\ppindex" href="\#collapse\CurSecHaddr">\Hnewline}
\Link[ class="accordion-toggle" data-toggle="collapse" data-parent="\#accordion\theppindex"]{collapse\theppindex}{}\HCode{\Hnewline
<h4 class="panel-title">\Hnewline%
<FONT style="font-size:15px" COLOR="\#FE2E2E">View Code</FONT> <span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-up"></i></span>%
</h4>\Hnewline%
</div>\Hnewline}%
<div id="collapse\CurSecHaddr" class="panel-collapse collapse">\Hnewline
<div class="panel-body">%
%<pre class="prettyprint"><p>}}%
}}
{\IgnorePar\HCode{%
</div>\Hnewline
</div>\Hnewline
</div>\Hnewline}
\EndAccordion%
%</div>
}{}{}
\begin{document}
\EndPreamble


note that linking commands must be used outside of \HCode. I also played little bit with paragraphs and removed redundant <pre> element and some \Hnewlines, in order to reduce empty vertical space.