3
$\begingroup$

Is it possible to create for each headline a tag with the same content (i.e. for all Title, Chapter, Subchapter, Section, Subsection, Subsunsection) automatically?

When using text processing tools like LibreOffice or Word, I always use a stylesheet and I also genreate a table of content (TOC) automatically very often, when a document exceeds a few pages. LibreOffice and Word automatically equip the TOC with hyperlinks from the TOC lines to the corresponding headlines. That is very nice to quickly navigate in the document. Fortunately, such hyperlinks within the document are also inherited to pdf files created from them and the hyperlinks also work in the derived pdf file.

I am looking foreward to generate such tags automatically and also I am looking for a tool to generate a TOC to be put at the beginning of a notebook. But unlike Generating a table of contents I would prefer hyperlinks in the TOC to the headlines instead of page numbers. The hyperlink mechanism in Mma seems to work without reference to a page numnber, so my idea should be feasible.

Has anybody already done something of this kind? (Of course, as in the model of the text processing programs, a TOC with page numbers and hyperlinks to all headlines would even be better).

N.B.: Is there a way to automatically format keywords with a hyperlink "behind " them such that they show up in blue and underliend such that they can easily be recognized in the text?

$\endgroup$
1
$\begingroup$

I wrote some code to do something very similar, though I only wanted to include Sections and Subsections in the table of contents.

The function finds all Sections/Subsections in the notebooks and then appends cell tags to those sections (note, if you have other cell tags this will overwrite them).

It then creates styled hyperlinks pointing to the tags. When clicked they will jump the notebook to the first tag appearance. There are two potential issues with this:

  1. If there are multiple headers with the same name, the link will always jump to the first cell

  2. The hyperlinks point to a fixed file path, so if the notebook is downloaded and placed somewhere else, this won't work properly.

    tableOfContents[notebook_]:=Module[{headerTypes,nb,header,headerCells,tagHeaders},
    (* Cell types to include in ToC *)
    headerTypes = "Subsubsection"|"Section";
    
    (* Find cells of the desired type and tag with their names *)
    nb=Import[notebook];
    headerCells = Cases[nb,Cell[_,headerTypes,___],Infinity];
    tagHeaders=Map[Append[#,CellTags->#[[1]]]&,headerCells];
    
    (* Export tagged cells *)
    Export[notebook,ReplaceAll[nb,Normal[AssociationThread[headerCells,tagHeaders]]]];
    
    (* Generate styled hyperlinks for ToC *)
    Map[
        If[MatchQ[#[[2]],"Section"],
            Hyperlink[Style[#[[1]],RGBColor@{211/256, 15/64, 0},FontFamily -> "Arial", FontTracking -> "Plain", FontSize -> 18],{notebook,#[[1]]}],
            Hyperlink[Style["\t"<>#[[1]],RGBColor@{53/64, 51/128, 1/256},FontFamily -> "Arial", FontTracking -> "Plain", FontSize -> 14],{notebook,#[[1]]}]
        ]&,
        headerCells
    ]//TableForm]
    
$\endgroup$
  • $\begingroup$ Buttons can solve the fixed filed path problem - Button[sectionName, NotebookLocate[tag], BaseStyle -> {"Hyperlink"}, Appearance -> None]. This command just needs to be executed in the notebook that will hold the table of contents. $\endgroup$ – hbuchman Feb 14 '18 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.