summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/typogrid/typogrid.dtx
blob: 4ea0c77c650d9aa65082f9e7881ac4d8340fa38d (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
% \iffalse meta comment
% File: typogrid.dtx Copyright (C) 2003, 2011 Harald Harders, Rolf Niepraschk
% \fi
%
% \iffalse
%<*driver>
\listfiles
\documentclass[a4paper]{ltxdoc}
\usepackage[T1]{fontenc}
\IfFileExists{typogrid.sty}{%
  \usepackage[draft,columns=4,margin=false,headings=true]{typogrid}
}{%
  \GenericWarning{typogrid.dtx}{Package file typogrid.sty not found
    (Documentation will be messed up!^^J^^A 
    Generate typogrid.sty by (La)TeXing typogrid.ins, process
    typogrid.dtx again)^^J}\stop
}
%%
\GetFileInfo{typogrid.sty}
\title{The \texttt{typogrid} package}
\author{%
  Harald Harders (\texttt{h.harders@tu-bs.de}),\\
  Rolf Niepraschk (\texttt{rolf.niepraschk@ptb.de})}
\date{File Version \fileversion, \filedate, printed \today}
%%
\EnableCrossrefs
\CodelineIndex
\DoNotIndex{\def,\edef,\let,\newcommand,\newenvironment,\newcounter}
\DoNotIndex{\setcounter,\space,\if,\else,\fi,\empty,\@empty,\ifx,\fi}
\DoNotIndex{\ifnum,\fi,\expandafter,\csname,\endcsname,\the}
\DoNotIndex{\MessageBreak,\message,\newlength,\newif,\xdef,\newcount}
\DoNotIndex{\begingroup,\endgroup,\,,\@tempcnta,\@tempdima,\advance}
\DoNotIndex{\ensuremath,\filedate,\fileversion,\docdate}
\DoNotIndex{\mathit,\mathrm,\mathsf,\nprt@tmp,\nprt@tmpnum,\relax}
\DoNotIndex{\protect,\renewcommand,\setlength,\settowidth,\stepcounter}
\DoNotIndex{\string,\put,\multiput}
\CodelineNumbered
\RecordChanges
\CheckSum{219}
\begin{document}
 \DocInput{typogrid.dtx}
\end{document}
%</driver>
% \fi
%
% \changes{0.21}{2011/12/31}{Update Makefile}%
% \changes{0.21}{2011/12/31}{Set date and version explicitly in
%   \cs{ProvidesPackage}}%
% \changes{0.20}{2003/11/09}{Reimplementation with respect to
%   \texttt{showframe.sty} by Rolf Niepraschk}%
% \changes{0.10}{2003/10/30}{First version}%
%
% \maketitle
% \begin{abstract}
% \noindent
% This package produces a typographic grid on every page of the
% document.
% That grid consists of vertical lines that devide the text block into
% columns.
% This may be useful to get the horizontal measures (distances etc.)
% into good values.
% \end{abstract}
%
% \tableofcontents
%
% \section*{Copyright}
% Copyright 2003, 2011 Harald Harders, Rolf Niepraschk.
%
% This program can be redistributed and/or modified under the terms
% of the LaTeX Project Public License Distributed from CTAN
% archives in directory macros/latex/base/lppl.txt; either
% version 1 of the License, or any later version.
%
% \section{Usage}
%
% Load the package using the
% \begin{quote}
% \cs{usepackage}\oarg{options}|{typogrid}|
% \end{quote}
% command.
% Valid options are |final|, |draft|,
% |columns=|\meta{value}, |headings=|\meta{true,false}, and
% |margin=|\meta{true,false}.
% The options |final| and |draft| may be given explicitely or
% implicitely from the document-class options.
%
% When |draft| is given the typographic grid is switched on; if
% |final| or none of the above is given it is switched off.
%
% The |columns| option determines the number of columns printed.
% If it is not given, a default of 12 is used.
%
% If |headings=true| is given, a grid is printed for the head line and
% the foot line, too (this is the default, you may also omit it).
% If |headings=false| is given, no grid is printed for the headings.
%
% With |margin=false|, you can switch off printing a frame around the
% area for margin notes (default is to print the frame).
%
% \DescribeMacro{\typogridsetup}
% There is a second possibility to change the typographic grid.
% You may use the \cs{typogridsetup} command that takes a keyval list
% as argument.
% The same keywords are allowed as in the \cs{usepackage} line, e.g.,
% \begin{quote}
% |\typogridsetup{columns=4}|
% \end{quote}
% You may use this command within the text in order to change the grid
% at arbitrary position of the document.
% The change takes effect on the next page.
%
% \DescribeMacro{\typogrid}
% If you have switched off printing of the grid, you may get a grid
% for single pages using
% \begin{quote}
% |\AddToShipoutPicture*{\typogrid}|
% \end{quote}
%
% The package defines the length \cs{gridwidth} which is as long as
% the space between too grid lines.
% You may use it to scale pictures to fit to the grid, for instance.
%
%
% \section{Shortcomings}
%
% \begin{itemize}
% \item Calculation of position and size of the headings and the
%   margin works for standard cases, only.
% \item If the text width changes from one page to the next, the grid
%   width is wrong on the first page after the change.
% \end{itemize}
%
% 
% \StopEventually{\PrintChanges \PrintIndex}
%
%
% \section{The implementation}
% \iffalse
%<*package>
% \fi
% Heading of the package:
%    \begin{macrocode}
\ProvidesPackage{typogrid}
  [2011/12/31  v0.21  Typographic grid] 
\RequirePackage{calc}
\RequirePackage{keyval}
%    \end{macrocode}
% Introduce the used lengths.
%    \begin{macrocode}
\newlength{\gridwidth}%
%    \end{macrocode}
% Introduce the used counters and set the default number of columns.
%    \begin{macrocode}
\newcount\tpg@blocks%
\newcounter{tpg@blocks@new}%
\setcounter{tpg@blocks@new}{12}%
%    \end{macrocode}
% \begin{macro}{\typogridsetup}
% Declare a command that can be used to change the appearence of the
% typographic grid.
% The argument takes a list of keyval options.
%    \begin{macrocode}
\newcommand*\typogridsetup[1]{%
  \expandafter\setkeys\expandafter{typogrid}{#1}}
%    \end{macrocode}
% \end{macro}
% Define the key |columns| that takes the number of columns.
%    \begin{macrocode}
\define@key{typogrid}{columns}{%
  \setcounter{tpg@blocks@new}{#1}%
  \ifnum\the\c@tpg@blocks@new<1\relax
    \PackageError{typogrid}{Less than 1 column given}{You have to
      declare at least 1 column.}%
    \setcounter{tpg@blocks@new}{1}%
  \fi
}
%    \end{macrocode}
% Define the key |headings| to switch on or off a frame around the
% headings.
%    \begin{macrocode}
\newif\iftpg@headings
\newif\iftpg@headings@new
\tpg@headings@newtrue
\define@key{typogrid}{headings}[true]{%
  \csname tpg@headings@new#1\endcsname
}
%    \end{macrocode}
% Define the key |margin| to switch on or off a frame around the
% margin.
%    \begin{macrocode}
\newif\iftpg@margin
\newif\iftpg@margin@new
\tpg@margin@newtrue
\define@key{typogrid}{margin}[true]{%
  \csname tpg@margin@new#1\endcsname
}
%    \end{macrocode}
% Package options:
%    \begin{macrocode}
\newif\iftpg@draft
\DeclareOption{draft}{\tpg@drafttrue}
\DeclareOption{final}{\tpg@draftfalse}
\DeclareOption{colorgrid}{\PassOptionsToPackage{\CurrentOption}{eso-pic}}
\DeclareOption{grid}{\PassOptionsToPackage{\CurrentOption}{eso-pic}}
\DeclareOption*{\expandafter\typogridsetup\expandafter{\CurrentOption}}
%    \end{macrocode}
% Default is no grid.
%    \begin{macrocode}
\ExecuteOptions{final}
\ProcessOptions\relax
%    \end{macrocode}
% Load this package after processing the options.
%    \begin{macrocode}
\RequirePackage{eso-pic}[2002/11/16]
%    \end{macrocode}
% \begin{macro}{\typogrid}
% Define the command that produces the grid.
%    \begin{macrocode}
\newcommand*\typogrid{%
%    \end{macrocode}
% Switch to black and thin lines.
%    \begin{macrocode}
  \begingroup
    \normalcolor
    \thinlines
%    \end{macrocode}
% Calculate the number of lines to be printed.
%    \begin{macrocode}
    \@tempcnta=\tpg@blocks
    \advance\@tempcnta by -1%
%    \end{macrocode}
% Print a frame around the text block.
%    \begin{macrocode}
    \AtTextLowerLeft{%
      \put(0,0){%
        \framebox(\LenToUnit{\textwidth},\LenToUnit{\textheight}){}}%
%    \end{macrocode}
% Print the vertical lines for the grid.
%    \begin{macrocode}
      \multiput(\LenToUnit{\gridwidth},0)%
               (\LenToUnit{\gridwidth},0){\the\@tempcnta}{%
        \line(0,1){\LenToUnit{\textheight}}}%
    }%
%    \end{macrocode}
% Print a frame around the head line if wanted.
%    \begin{macrocode}
    \iftpg@headings
      \AtTextUpperLeft{%
        \put(0,\LenToUnit{\headsep}){%
          \framebox(\LenToUnit{\textwidth},\LenToUnit{\headheight}){}}%
%    \end{macrocode}
% Print the grid.
%    \begin{macrocode}
        \multiput(\LenToUnit{\gridwidth},\LenToUnit{\headsep})%
                 (\LenToUnit{\gridwidth},0){\the\@tempcnta}{%
          \line(0,1){\LenToUnit{\headheight}}}%
      }%  
%    \end{macrocode}
% Print a line below the foot line if wanted (the height of the foot
% line is not available).
%    \begin{macrocode}
      \AtTextLowerLeft{%
        \put(0,\LenToUnit{-\footskip}){%
          \line(1,0){\LenToUnit{\textwidth}}}%
%    \end{macrocode}
% Print the grid.
%    \begin{macrocode}
        \put(0,\LenToUnit{-\footskip}){%
          \line(0,1){\LenToUnit{\baselineskip}}}%
        \put(\LenToUnit{\textwidth},\LenToUnit{-\footskip}){%
          \line(0,1){\LenToUnit{\baselineskip}}}%
        \multiput(\LenToUnit{\gridwidth},\LenToUnit{-\footskip})%
        (\LenToUnit{\gridwidth},0){\the\@tempcnta}{%
          \line(0,1){\LenToUnit{\baselineskip}}}%
      }%
    \fi
%    \end{macrocode}
% Print a frame around the margin if wanted.
%    \begin{macrocode}
    \iftpg@margin
      \AtTextLowerLeft{%
        \@tempdima=\textwidth\advance\@tempdima\marginparsep%
        \if@twoside%
          \ifodd\c@page
          \else
            \@tempdima=-\marginparsep\advance\@tempdima-\marginparwidth% 
          \fi% 
        \fi%
        \put(\LenToUnit{\@tempdima},0){%
          \framebox(\LenToUnit{\marginparwidth},%
            \LenToUnit{\textheight}){}}%
      }% 
    \fi
  \endgroup
%    \end{macrocode}
% Calculate the width of each grid.
% Store it globally to be able to use it inside the document.
%    \begin{macrocode}
  \setlength\gridwidth{\textwidth/\arabic{tpg@blocks@new}}%
  \global\gridwidth=\gridwidth
  \global\tpg@blocks=\arabic{tpg@blocks@new}%
  \iftpg@headings@new
    \global\tpg@headingstrue
  \else
    \global\tpg@headingsfalse
  \fi
  \iftpg@margin@new
    \global\tpg@margintrue
  \else
    \global\tpg@marginfalse
  \fi
}
%    \end{macrocode}
% \end{macro}
% Start the grid at \cs{begin\{document\}}.
% Do it that late to enable the author to switch between |draft| and
% |final| before that position.
%    \begin{macrocode}
\AtBeginDocument{%
%    \end{macrocode}
% Print the grid on any page if wanted.
%    \begin{macrocode}
  \iftpg@draft
    \typeout{Typographic grid switched on}%
    \AddToShipoutPicture{\typogrid}%
  \else
    \typeout{Typographic grid switched off}%
  \fi
%    \end{macrocode}
% Declare width of grid for first page of document.
%    \begin{macrocode}
  \setlength\gridwidth{\textwidth/\arabic{tpg@blocks@new}}%
  \global\tpg@blocks=\arabic{tpg@blocks@new}%
  \iftpg@headings@new
    \tpg@headingstrue
  \else
    \tpg@headingsfalse
  \fi
  \iftpg@margin@new
    \tpg@margintrue
  \else
    \tpg@marginfalse
  \fi
}
%    \end{macrocode}
%
% \iffalse
%</package>
% \fi
% \Finale