4

My Problem

I want to create some sort of deeply customized table of content (abbreviated ToC). I already have some experience doing such things but the level of complexity that is here desired goes largely beyond my capacities.

The idea is to create something that would look like the following picture:

ToC Draft (This image was realized using Gimp)

Some More Details

This ToC will be part of a class that I am now writing (and willing to release under an open license as soon as it is finished). Those documents will be compiled using LuaLaTeX. I have some knowledge of tikz and titletoc and thought I would use them to create this design but I don't really know where to start. The idea behind this is to have true randomness (hence, the table of content will change at every compilation!)

Following features are desired:

  1. Left hand side of the page should be white
  2. Right and side of the page should be in Color (either Black, a dark grey or the main color used throughout the document) (use tikz remember picture and overlay options for example)
  3. The bars should have random length (using lua math.random?)
  4. The layout should adapt itself to negative vs. positive values
  5. The ToC should only display chapter and maybe part entries (using \setcounter{tocdepth}{0} for example; my point being that only the designs for chapter and part are required)
  6. For each chapter entry, the chapter number, the chapter title and the chapter page should appear

Important is the global idea. The little details are not and will probably change over time. For example, I am not certain that the chapter number will appear where they do now, I still need to figure out how chapter names that span more that the space available on one line will be displayed, etc.

  • For the black/white background: maybe have a look at http://tex.stackexchange.com/a/279318/36296 – samcarter_is_at_topanswers.xyz Feb 16 '17 at 17:18
  • And have a look at http://tex.stackexchange.com/questions/35825/pretty-table-of-contents/163707#163707 – samcarter_is_at_topanswers.xyz Feb 16 '17 at 17:22
  • Although this is probably subjective, I would be interested in knowing the rationale behind this level of customisation. A ToC is usually a functional element in a book, for which randomisation would not be considered optimal by most typographers. Then, if it is a design book (or other piece of art), maybe you want to hand tweak the ToC yourself and, as such, create a handmade page and add the titles + page numbers by using a \label and \ref system (such as cleveref or hyperref). So, why combine a fully automated yet fully custom layout? – ienissei Feb 16 '17 at 17:24
  • I am writing various stories, all related to a same world. All should be typeset using the same design. This is why an automated ToC design is desired. Indeed, the ToC is not really practical, but that's not really the idea here. I am more interested in some "Waouh" design, a beautiful one (which would however still give the users some practical informations). And I found quite funny the idea of true randomness (for once in typography, some stuffs would be made totally randomly, what a change!!) – HarveyShepp Feb 16 '17 at 17:33
  • This is far too broad, I think, for a single question. And the design request is really off-topic. You don't even provide an example for people to play around with, even though you clearly know enough about what you are doing to show us what you've tried. For what it is worth, that design would make me say, 'Shit! What went wrong here?' I'd simply assume the author had screwed up and not even bothered to check the first few pages of the document before posting. But presumably members here are not your target audience ... ;). – cfr Feb 17 '17 at 02:29
  • @cfr I do agree, my question is broad! But how do you propose I split it or simplify it? If tikz and titletoc were to be use, the \titlecontents{chapter}... command would be a really tricky one, and almost all of the wanted features would have to be coded there. As I don't know how to create such design and don't even know whether or not tikz and titletoc are the good packages to use, I did not post any MWE. You might not like the design, but this is not the question here. Furthermore, it might be different from the average design, but this does not make it a bad one. – HarveyShepp Feb 17 '17 at 08:59
  • @HarveyShepp Perhaps you could try to provide an MWE with some basic stuff already (i.e. the design elements you can do with basic tikz and titletoc), and focus your question on the specific randomness issue – then you should also change the title. – ienissei Feb 18 '17 at 00:42
  • @HarveyShepp Regarding your design choice, as I already mentioned, I still think it might be overall more efficient to create the page with labels. You have to take into account the programming costs of the result you want to reach, and the "Waouh" effect (which I do not share) typically enters the “don't bother automating” category. Say you were using \include to print your chapters, there would be ways to redefine it (or \chapter) so as to grab a label and page number automatically, and add it to a special file containing your code. Or any other quick fix along those lines. – ienissei Feb 18 '17 at 00:52

2 Answers2

1

Use another language like Python or Pearl to parse the toc file generated in a 1st LaTeX run. Add the features you need with that language, then have it call LaTeX to generate a completed document. Within your Editor, for instance in Kile, setup a custom compile script that will run LaTeX 1st to generate the basic toc data, then call up your parsing language script and generate your fancy ToC, and then finally compile with LaTeX again to generate the final pdf. This is a "global idea" answer, as requested, and not a specific solution for any particular type of fancy ToC.

user12711
  • 2,753
1

Finally, after some researches and struggling, I managed to program a version of it:

\documentclass{report}

\usepackage{tikz}
\RequirePackage{interfaces-base}
\LoadInterface{pgfkeys}
\usetikzlibrary{calc}

\usepackage{xparse}
\usepackage{calc}
\usepackage{ifthen}

\usepackage{titletoc}

\usepackage{ragged2e}

\usepackage{xcolor}
\definecolor{mainColor}{RGB}{211, 47, 47}

\usepackage{geometry}


% Comment this block if you don't have the Fira Sans font installed!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{fontspec}
\setmainfont{Fira Sans Light}[
    BoldFont=Fira Sans Medium,
    ItalicFont=Fira Sans Light Italic,
    BoldItalicFont=Fira Sans Medium Italic
]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareDocumentCommand{\myTableOfContent}{}{%
    \begingroup
    % remove hyphenation in titles
    \hyphenpenalty10000\exhyphenpenalty10000\righthyphenmin62\lefthyphenmin62
    % Allow to draw on the table of content page, other wise blank pages are added between the ToC and the big vertical line
    \let\clearpage\relax
    % Drawing the big vertical line
    \begin{tikzpicture}[remember picture, overlay, inner sep=0mm]
        \fill[fill=mainColor] (current page.north) rectangle (current page.south east);
    \end{tikzpicture}
    % Only show chapter (and part) in the toc
    \setcounter{tocdepth}{0}
    % Change ToC title
    \renewcommand{\contentsname}{Table of Content}
    % Print the ToC
    \tableofcontents
    \endgroup
}

% This length contains temporarily the width of the current chapter's bar
\newlength{\chapterBarTempLength}
% This length stores the height of a chapter entry for proper spacing between them
\newlength{\chapterEntryHeight}
\newsavebox{\chapterEntryBox}

% Configuration Options
\pgfkeys{
    /customized toc/space between entries/.store as dimen,
    /customized toc/space between entries = 3mm,
}
\pgfkeys{
    /customized toc/bar/height/.store as dimen,
    /customized toc/bar/height = 1.2\baselineskip,
}
\pgfkeys{
    /customized toc/bar/xoffset/.store as dimen,
    /customized toc/bar/xoffset = 6mm,
}
\pgfkeys{
    /customized toc/bar/style/.style = {rounded corners=1pt,},
}
\pgfkeys{
    /customized toc/bar/left style/.style = {},
}
\pgfkeys{
    /customized toc/bar/right style/.style = {},
}
\pgfkeys{
    /customized toc/text/xoffset/.store as dimen,
    /customized toc/text/xoffset = \pgfkeysvalueof{/customized toc/bar/xoffset},
}
\pgfkeys{
    /customized toc/text/yoffset/.store as dimen,
    /customized toc/text/yoffset=7mm,
}
\pgfkeys{
    /customized toc/text/format/.store in=\customizedTocChapterTextFont,
    /customized toc/text/format = \fontsize{1.7\baselineskip}{2\baselineskip}\selectfont,
}
\pgfkeys{
    /customized toc/text/format/.store in=\customizedTocChapterTextRightFont,
    /customized toc/text/format = \color{white},
}
\pgfkeys{
    /customized toc/text/format/.store in=\customizedTocChapterTextLeftFont,
    /customized toc/text/format = ,
}
\pgfkeys{
    /customized toc/text/color/.store as color,
    /customized toc/text/color = mainColor,
}
\pgfkeys{
    /customized toc/text/height/.store as dimen,
}
\pgfkeys{
    /customized toc/page number/width/.store as dimen,
    /customized toc/page number/width = 4mm,
}
\pgfkeys{
    /customized toc/page number/style/.style = {
        font=\bfseries,
    },
}
\pgfkeys{
    /customized toc/page number/left style/.style={},
}
\pgfkeys{
    /customized toc/page number/right style/.style={},
}
\pgfkeys{
    /customized toc/chapter number/style/.style={
        font=\fontsize{4\baselineskip}{4\baselineskip}\selectfont\itshape\bfseries\color{mainColor}
    },
}

% Saveboxes don't take in account the fontsize, to compensate, some more calculations are needed
\newlength{\normalsizeXHeight}\setlength{\normalsizeXHeight}{\heightof{\normalsize x}}
\newlength{\chapterTocEntryXHeight}\setlength{\chapterTocEntryXHeight}{\heightof{\customizedTocChapterTextFont x}}
\newlength{\scaledChapterEntryHeight}

% Arguments: chapter title, coefficient (should be 1 or -1)
\DeclareDocumentCommand{\chapterEntryGraphicalElementPrint}{m m}{%
    \ifthenelse{#2>0}{
        \DeclareDocumentCommand{\tempChapterEntryAnchor}{}{east}
        \DeclareDocumentCommand{\tempChapterEntryTikzAlign}{}{left}
        \DeclareDocumentCommand{\tempChapterEntryParboxAlign}{}{\FlushRight}
        \DeclareDocumentCommand{\tempChapterEntrySideFont}{}{\customizedTocChapterTextLeftFont}
        \pgfkeys{/customized toc/bar/temp style/.style={/customized toc/bar/style, /customized toc/bar/right style}}
        \pgfkeys{/customized toc/page number/temp style/.style={/customized toc/page number/style, /customized toc/page number/right style}}
    }{
        \DeclareDocumentCommand{\tempChapterEntryAnchor}{}{west}
        \DeclareDocumentCommand{\tempChapterEntryTikzAlign}{}{right}
        \DeclareDocumentCommand{\tempChapterEntryParboxAlign}{}{\FlushLeft}
        \DeclareDocumentCommand{\tempChapterEntrySideFont}{}{\customizedTocChapterTextRightFont}
        \pgfkeys{/customized toc/bar/temp style/.style={/customized toc/bar/style, /customized toc/bar/left style}}
    \pgf

keys{/customized toc/page number/temp style/.style={/customized toc/page number/style, /customized toc/page number/left style}}
    }
    % Bar
        % Coordinates
    \coordinate[xshift=#2\pgfkeysvalueof{/customized toc/bar/xoffset}, yshift=.5*\pgfkeysvalueof{/customized toc/bar/height}] (chapterBarBegin) at (currentCenter);
    \coordinate[xshift=\chapterBarTempLength, yshift=-\pgfkeysvalueof{/customized toc/bar/height}] (chapterBarEnd) at (chapterBarBegin);
        % Drawing the bar
    \fill[/customized toc/bar/temp style] (chapterBarBegin) rectangle (chapterBarEnd);

    % Page Number
    \coordinate[yshift=.5*\pgfkeysvalueof{/customized toc/bar/height}] (pageNumberAnchor) at (chapterBarEnd);
    \node [anchor=\tempChapterEntryAnchor, text=white, text width=\pgfkeysvalueof{/customized toc/page number/width}, align=\tempChapterEntryTikzAlign, /customized toc/page number/temp style] at (pageNumberAnchor) {\thecontentspage};

    %Text
    \coordinate[xshift=#2*-\pgfkeysvalueof{/customized toc/text/xoffset}] (chapterTextAnchor) at (currentCenter);
    \node[yshift=\pgfkeysvalueof{/customized toc/text/yoffset}, font=\customizedTocChapterTextFont\tempChapterEntrySideFont, anchor=mid \tempChapterEntryAnchor, text=\pgfkeysvalueof{/customized toc/text/color}] (chapterText) at (chapterTextAnchor) {\mbox{}\parbox[t]{.5\textwidth-\pgfkeysvalueof{/customized toc/text/xoffset}}{\tempChapterEntryParboxAlign#1}};

    % Chapter Number
    \node[xshift=-1mm, anchor=center, /customized toc/chapter number/style] at (currentCenter) {\thecontentslabel};
}

% Command that format each chapter entry in the ToC
\DeclareDocumentCommand{\formatChapterEntry}{m}{%
    % Random bar length (between (-1, 1) )
    \setlength{\chapterBarTempLength}{\directlua{tex.print(tostring(math.random()*2-1))}\textwidth/2}
    \begin{tikzpicture}[remember picture, overlay, inner sep=0mm]
        % Calculate the current entry's center
        \path let
            \p1 = (current page.center)
        in
            coordinate (currentCenter) at (\x1, 0);
        % Draw the graphical elements
        \ifthenelse{\lengthtest{\chapterBarTempLength>0pt}}{%
            % Bar has positive value
            % Bar must have the minimal size needed to display the page number
            \ifthenelse{\lengthtest{\chapterBarTempLength<\pgfkeysvalueof{/customized toc/page number/width}}}{\setlength{\chapterBarTempLength}{\pgfkeysvalueof{/customized toc/page number/width}+2mm}}{}
            \chapterEntryGraphicalElementPrint{#1}{1}
        }{%
            % Bar has negative value
            % Bar must have the minimal size needed to display the page number
            \ifthenelse{\lengthtest{-1\chapterBarTempLength<\pgfkeysvalueof{/customized toc/page number/width}}}{\setlength{\chapterBarTempLength}{-\pgfkeysvalueof{/customized toc/page number/width}-2mm}}{}
            \chapterEntryGraphicalElementPrint{#1}{-1}
        }%
    \end{tikzpicture}
    % Calculating the height of the chapter's entry
    \savebox{\chapterEntryBox}{\parbox{.5\textwidth-\pgfkeysvalueof{/customized toc/text/xoffset}}{\customizedTocChapterTextFont#1}}%
    \setlength{\chapterEntryHeight}{\ht\chapterEntryBox}%
    \setlength{\scaledChapterEntryHeight}{\chapterEntryHeight*\ratio{\chapterTocEntryXHeight}{\normalsizeXHeight}}%
    \vspace*{\scaledChapterEntryHeight}%
    \vspace*{\pgfkeysvalueof{/customized toc/space between entries}}%
}

% Redefine the styling of the chapter entries in the ToC
\titlecontents{chapter}%
[0pt] % left
{} % Above code
{\formatChapterEntry} % numbered entry format (the last command can take as argument the chapter's text)
{} % number-less entry format
{} % filler code
[] % Below code



\begin{document}
    \myTableOfContent

    \chapter{This is chapter one}
    \section{This is a section}
    \section{This is a section}

    \chapter{This is chapter two with a very long title}
    \section{This is a section}
    \section{This is a section}

    \chapter{This is chapter three}
    \section{This is a section}
    \section{This is a section}

    \chapter{This is chapter four}
    \section{This is a section}
    \section{This is a section}
\end{document}

Which results in Result in: Deeply Customized ToC, Results