summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/thumby/thumby.sty
blob: 418bc9eccb792e1bab4be0ee9b4a6275169f0c06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
%% Thumby - LaTeX package for creating thumb indexes.
%
%  Version: 0.1
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Requirements:
%
%   - memoir document class
%
%   - Packages:
%
%       - PerlTeX
%       - tikz
%       - bophook
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Instructions:
%
% Put something like the following in your document (we'll call it "foo.tex"):
%
%     \documentclass[twoside]{memoir}
%
%     \usepackage{thumby}
%
%     \thumbySides{two}
%     \thumbyTotalChapters{10}
%     \thumbyPageHeight{792}
%     \thumbyThumbWidth{42.5}
%     \thumbyForeground{white}
%     \thumbyBackground{black}
%     \thumbyNumberFormat{\Huge\textbf{$chapter_number}}
%
%     \thumbySetup
%
% Then, run twice:
%
%     perltex --latex=pdflatex foo.tex
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Please change the above to reflect your document and preferences.
% In particular, you should change the \thumbyTotalChapters from 10 to the number
% of chapters in your document, and the \thumbyPageHeight from 792 to the height
% of your document (in points), and \thumbySides to reflect whether your document
% uses one- or two-sided pages.
%
% Note: Units for page height, width, etc, are in points.
%
% Note: Only normal, numbered chapters will have a thumb index.
%       For example, the of contents and \frontmatter chapters will not have a thumb index.
%
% Note: When \thumbyNumberFormat contains the literal $chapter_number
%       Thumby will substitute the actual chapter number for this literal.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   Thumby - LaTeX package for creating thumb indexes.
%
%   Copyright (C) 2010  Sergey Goldgaber
%
%   This program is free software: you can redistribute it and/or modify
%   it under the terms of version 3 of the GNU General Public License
%   as published by the Free Software Foundation.
%
%   This program is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{thumby}[2010/01/14 v0.1 Thumby]

\typeout{}
\typeout{***********************************************}
\typeout{*********** Loading Thumby Package ************}
\typeout{***********************************************}
\typeout{}

% We use the tikz library to draw and position the thumb indexes.
\RequirePackage{tikz}

% We use the perltex package to run perl within LaTeX.
% This is used to do the calculations of the dimensions
% of the thumb boxes and their locations, which are fed
% to tikz.
\RequirePackage{perltex}

%% We use the bophook package to place a thumb box on each page.
\RequirePackage{bophook}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Configuration commands.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\perlnewcommand{\thumbyNumberFormat}[1]{
    $thumby_number_format = $_[0] ;
    # Slashes have to be doubled, as otherwise perl will interpret them as indicating
    # that a special character follows.
    $thumby_number_format =~ s{/}{//} ;
    return "\\typeout{Thumby number format: $thumby_number_format}" ;
}

% Place a thumb index on each page.
\perlnewcommand{\thumbySides}[1]{
    $thumby_sides = $_[0] ;

    if ( $thumby_sides eq "one" ) {
        $output = <<END;
        % Using command from the bophook package to place a thumb index on each page.
        \\AtBeginPage{%
            \\thumbyprintthumbright{\\thumbychapnum}
        }
END
    } elsif ( $thumby_sides eq "two" ) {
        $output = <<END;
        % Using command from the bophook package to place a thumb index on each page.
        \\AtBeginPage{%
            % Odd pages should have the thumb index on the right,
            % while even ones on the left.
            \\checkoddpage
            \\ifoddpage
               \\thumbyprintthumbright{\\thumbychapnum}
            \\else
               \\thumbyprintthumbleft{\\thumbychapnum}
            \\fi
        }
END
    } else {
        $output = <<END;
        \\PackageError{thumby}{\\protect\\thumbySides \\space must be "one" or "two"}{}
END
    }
    return $output ;
}

\perlnewcommand{\thumbyBackground}[1]{
    $thumby_background = $_[0] ;
    return "" ;
}

\perlnewcommand{\thumbyForeground}[1]{
    $thumby_foreground = $_[0] ;
    return "" ;
}

\perlnewcommand{\thumbyPageHeight}[1]{
    $thumby_page_height = $_[0] ;

    # Sanity checks
    if ( $thumby_page_height =~ m{^\d+$} ) {
        # An integer.
        # Looks good.  Doing nothing.
        $output = "" ;
    } elsif ( $thumby_page_height =~ m{^\d*\.\d+$} ) {
        # A floating point number.
        # Looks good.  Doing nothing.
        $output = "" ;
    } else {
        $output = "\\PackageError{thumby}{\\protect\\thumbyPageHeight \\space must a number.}{}" ;
    }

    return $output ;
}

\perlnewcommand{\thumbyThumbWidth}[1]{
    $thumby_thumb_width = $_[0] ;

    # Sanity checks
    if ( $thumby_thumb_width =~ m{^\d+$} ) {
        # An integer.
        # Looks good.  Doing nothing.
        $output = "" ;
    } elsif ( $thumby_thumb_width =~ m{^\d*\.\d+$} ) {
        # A floating point number.
        # Looks good.  Doing nothing.
        $output = "" ;
    } else {
        $output = "\\PackageError{thumby}{\\protect\\thumbyThumbWidth \\space must a number.}{}" ;
    }

    return $output ;
}

\perlnewcommand{\thumbySetup}{
    $thumby_thumb_height = $thumby_page_height / $thumby_total_chapters ;
    $half_thumb_width = $thumby_thumb_width / 2 ;
    $output = <<END;
\\typeout{}
\\typeout{** Thumby setup **}
\\typeout{Sides: $thumby_sides}
\\typeout{Total chapters: $thumby_total_chapters}
\\typeout{Page height: $thumby_page_height points}
\\typeout{Thumb height: $thumby_thumb_height points}
\\typeout{Thumb width: $thumby_thumb_width points}
\\typeout{Foreground: $thumby_foreground}
\\typeout{Background: $thumby_background}
\\typeout{Chapter number format: $thumby_number_format}
\\typeout{}
END
    return $output ;
}

\perlnewcommand{\thumbyTotalChapters}[1]{
    $thumby_total_chapters = $_[0] ;
    my $output = "" ;
    if ( $thumby_total_chapters =~ m{^\d+$} ) {
        if ( $thumby_total_chapters < 1 ) {
            $output = <<END;
            \\PackageError{thumby}{\\protect\\thumbyTotalChapters \\space must be greater than 0.}{}
END
        } else {
            # Looks good.  Doing nothing.
            $output = "" ;
        }
    } else {
        $output = <<END;
        \\PackageError{thumby}{\\protect\\thumbyTotalChapters \\space must be an integer}{}
END
    }

    return $output ;
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Miscellaneous commands
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Definition of \thumbychapnum, which returns the current chapter number,
% but only for chapters, not for parts.
\newcommand{\thumbychapnum}{}

\renewcommand{\mempartinfo}[4]{%
\renewcommand{\thumbychapnum}{}
}

\renewcommand{\memchapinfo}[4]{%
\renewcommand{\thumbychapnum}{\thechapter}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Thumb index printing commands.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% This command will print a thumb box on the left or right of the page,
% depending on the value of the second argument given to it.
%
% Some sanity checks are also run on the chapter number, to make sure it's valid.
% NOTE, chapter numbers less than 1 are not printed.
\perlnewcommand{\thumbyprintthumb}[2]{
    $thumby_chapter_number = shift ;
    $left_or_right = shift ;

    # Run some sanity checks on $thumby_chapter_number
    if ( $thumby_chapter_number eq "" ) {
        # Doing nothing
        return ""
    } elsif ( not ( $thumby_chapter_number =~ m{^\d+$} ) ) {
        # $thumby_chapter_number is not a number
        return 'ERROR: chapternumber "' . $thumby_chapter_number . '" is not a number' ;
    } elsif ( $thumby_chapter_number < 1 ) {
        # Doing nothing
        return "" ;
    }

    # Determine the position of the thumb index
    if ( $left_or_right eq "left" ) {
        $horizontal_thumb_position = $half_thumb_width ;
        $origin = "north west" ;
    } elsif ( $left_or_right eq "right" ) {
        $horizontal_thumb_position = -$half_thumb_width ;
        $origin = "north east" ;
    }

    $vertical_thumb_position = ( $thumby_chapter_number * $thumby_thumb_height ) - $thumby_thumb_height ;

    $thumby_formatted_chapter_number = $thumby_number_format ;
    $thumby_formatted_chapter_number =~ s{\$chapter_number}{$thumby_chapter_number} ;

# Tikz code to draw the chapter number surrounded by a box:
$output = <<END;
\\pgfrememberpicturepositiononpagetrue
\\begin{pgfpicture}
\\pgfusepath{use as bounding box}
\\pgftransformshift{\\pgfpointanchor{current page}{$origin}}
\\pgftransformshift{\\pgfpoint{$horizontal_thumb_position}{-$vertical_thumb_position}}
\\pgfset{minimum width=$thumby_thumb_width}
\\pgfset{minimum height=$thumby_thumb_height}
\\pgfsetfillcolor{$thumby_background}
\\pgfnode{rectangle}{north}{}{thumb}{\\pgfusepath{fill}}
\\pgfsetfillcolor{$thumby_foreground}
\\pgfnode{rectangle}{north}{$thumby_formatted_chapter_number}{thumb}{\\pgfusepath{stroke}}
\\end{pgfpicture}
END
    return $output ;
}

\perlnewcommand{\thumbyprintthumbleft}[1]{
    $thumby_chapter_number = $_[0] ;
    return latex_thumbyprintthumb( $thumby_chapter_number, "left" ) ;
}

\perlnewcommand{\thumbyprintthumbright}[1]{
    $thumby_chapter_number = $_[0] ;
    return latex_thumbyprintthumb( $thumby_chapter_number, "right" ) ;
}

\endinput