[l2h] Looking for section/navigation info

Rhonda Hyslop 0402tug at write-on.org
Fri Feb 13 20:37:18 CET 2004

* Anthony Stone <ajs1 at cam.ac.uk> [13 Feb 2004 02:05]:
> At 15:48 on 12 February, Rhonda Hyslop wrote:
>  > And now, with a bit of processing on those two variables, I have exactly
>  > the sidebar menu I wanted. It even expands and shows all the subsections
>  > that are "siblings" of the current subsection, while leaving all the
>  > other sections collapsed but listed for easy navigation.
> This sounds excellent. Is it in a form that would be generally usable?
> I for one would find it very helpful if you would be willing to make
> it available.

Certainly. Below is what I used (minus the site logo, and plus

This code only makes a menu to 2 levels; section and subsection. This is
because that's as far as I split my document (subsubsections were
included in the same file as their parent subsection), but it should be
pretty easy to extend it to further levels as needed using the same
logic. It expands the current section only, displaying its subsections;
all other sections are collapsed and show only the top level.

It's a lot easier to jump around to different parts of the document now,
because from any section or subsection, you can jump directly to the top
level, any section, or any sibling subsection, instead of the one level
up/next/previous navigation that's the default.

I also used $HTML_VERSION = 4.0 so I could apply styles to the
navigation bar (class="navigation").

I hope somebody finds this useful.



sub top_navigation_panel {

    local $sec_info = "";
    local $in_subsec = 0;
    local (@curr_sec_levels, @seckey_levels, $this_sec_id, $level, $secfile, $title);
    # $key is the key for the file currently being processed
    @curr_sec_levels = split (" ", $key); # we want it split so we can 
                                          # easily get at a specific part of it

    foreach my $seckey (sort numerically keys(%section_info)) {
         @seckey_levels = split (" ", $seckey); # split for same reason as above
         $this_sec_id = ($key eq $seckey) ? " id=\"current\"" : "";
                        # mark the current section so it can be styled differently
         ($level, $secfile, $title) = split("%:%",$section_info{$seckey});

         if ($level == 0) { # main document, top level page
             $sec_info .= qq@<P><A href="$secfile"$this_sec_id>Your document title home</A></P><UL>@;
             $sec_info .= qq@\n@; # no idea why this is needed, but without it 
                        # the <UL> from the line above doesn't show up in the 
                        # HTML output.
         } elsif ($level == 3 && $seckey_levels[3] == $curr_sec_levels[3]) {
                        # the section heading we are either on or in a
                        # subsection of; has to open the subsection list
             $sec_info .= qq@<LI><A href="$secfile"$this_sec_id>$title</A></LI>\n@;
             $sec_info .= "<UL>\n";
         } elsif ($level == 3 && $seckey_levels[3] == $curr_sec_levels[3]+1) {
                        # the section heading after the section we are
                        # either on or in a subsection of; has to close
                        # the subsection list
            $sec_info .= "</UL>\n" if $in_subsec; 
                        # has to check the flag otherwise this was
                        # triggered for the front page (level 0) when
                        # putting the first section down.
            $in_subsec = 0;
            $sec_info .= qq@<LI><A href="$secfile">$title</A></LI>\n@;
        } elsif ($level == 3) {
                        # all other section headings we're not in
            $sec_info .= qq@<LI><A href="$secfile">$title</A></LI>\n@;
        } elsif ($level == 4 && $seckey_levels[3] == $curr_sec_levels[3]) {
                        # subsection headings only if we're in the
                        # parent section
            $sec_info .= qq@<LI><A href="$secfile"$this_sec_id>$title</A></LI>\n@;
    local $out = $sec_info . "</UL>";
    $out .= "\n"; # as above, needed or the </UL> doesn't appear.

    return $out;


 www.write-on.indy || www.write-on.org   \/  http://history.ubcengineers.ca/
  Discuss the art and craft of writing   /\   UBC Engineers History Project
   That's the problem with world domination. Nobody is willing to wait for 
   it anymore, work slowly towards it, drink more and enjoy the ride more.

More information about the latex2html mailing list