Debian Codegraph @

texmf-dist/doc/latex/returntogrid/returntogrid.tex

raw
1 % !Mode:: "TeX:UTF-8:Main"
2 \makeatletter
3 \def\UlrikeFischer@package@version{0.2}
4 \def\UlrikeFischer@package@date{2018/08/21}
5 \makeatother
6 %\PassOptionsToPackage{enable-debug}{expl3}
7 \documentclass[twoside,parskip=half-,fontsize=12pt,egregdoesnotlikesansseriftitles,headings=normal]{scrartcl}
8 \usepackage[english]{babel}
9 \usepackage[autostyle]{csquotes}
10 \usepackage{microtype}
11 %\DisableLigatures{encoding = T1, family = tt* }
12 \usepackage{fourier}
13 \renewcommand\ttdefault{lmtt}
14 \usepackage{ydoc-desc}
15 \usepackage{needspace}
16 \usepackage{returntogrid}
17 \usepackage{geometry}
18
19 \usepackage{tikz}
20 \usepackage{enumitem}
21 \raggedbottom
22
23 \title{The returntogrid package, v\csname UlrikeFischer@package@version\endcsname}
24 \date{\csname UlrikeFischer@package@date\endcsname}
25 \author{Ulrike Fischer\thanks{[email protected]}}
26 \usepackage{listings}
27 \lstset{basicstyle=\ttfamily, columns=fullflexible,language=[LaTeX]TeX,aboveskip=0pt}
28
29 \makeatletter
30 \lst@AddToHook{PreInit}{\returntogrid} %works here but should be checked. A local key would be better.
31
32 \begin{document}
33 \maketitle
34
35
36 \section{Introduction}
37
38 This package offers a few commands to get something like an simple, semi-automatic grid typesetting. It does more or less what the existing \texttt{gridset} package does. The main differences to \texttt{gridset} are that \texttt{returntogrid} works also with lualatex, that it has also a command to do some horizontal movements, that it uses internally expl3 and that it does much less \enquote{security} checks: It actually started only as internal code, where I had good control about the use of the commands. Then I thought it would be useful to externalize it in case I needed it for more projects and added a thin wrapper of user commands.
39
40 It probably also has more bugs -- report these at the issue tracker at github \\ \textbf{https://github.com/u-fischer/returntogrid}.
41
42 \section{Vertical grid moves}
43
44 \subsection{Restrictions / Deficiencies: Grid typesetting is not easy}
45
46
47 The problem is at first that there are so many things that can destroy the grid:
48
49 \begin{itemize}[nosep]
50 \item different font sizes with their different leading,
51 \item all the various skips around and in lists, around math, around floats,
52 \item pictures and tabulars (e.g. because of lines) often don't fit the grid,
53 \item the way \TeX\ handles large boxes: People e.\,g. quite often believe that a tabular with three rows of normal height shouldn't disturb the grid but it does:
54
55 \smallskip
56
57 \tikz[overlay]\draw[red,dashed](0,0) --++ (3cm,0)
58 (0,-\baselineskip) --++ (3cm,0)
59 (0,-2\baselineskip) --++ (3cm,0)
60 (0,-3\baselineskip) --++ (3cm,0)
61 (0,-4\baselineskip) --++ (3cm,0);some text\\*
62 \begin{tabular}[t]{l}
63 tabular row 1\\
64 tabular row 2\\
65 tabular row 3
66 \end{tabular}\\*
67 some text not on the grid
68
69 \smallskip
70 \item and more
71 \end{itemize}
72
73
74 Getting back to the grid isn't trivial either. In many cases it can be done by inserting a suitable vertical space. But due to the way \TeX\ builds the page the needed amount can normally only be calculated in a second compilation. As the inserted space changes the following text probably new compilations are needed. So it can take a long time until everything is stable -- if a stable solution actually exists: stretchable space, floats that move around and problems at page breaks can actually prevent a stable state.
75
76
77 So you want to use this package be aware of the following restrictions:
78
79 \begin{enumerate}
80 \item It will only work with documents/pages which uses \cs{raggedbottom}.
81 \item Many \cs{returntogrid} commands means many compilations. So use the command only when really needed. Setup as many spaces of lists and sectioning commands so that \pkg{returntogrid} is not needed to keep the grid.
82 \item Objects at/after page breaks needs special care, see section \ref{sec:pagebreak}.
83 \item Top floats could be a real problem.
84 \item The code uses for comparision the y-coordinate of a point in the text upper corner of the first page. (Such coordinates are measured from the \emph{bottom} of the page.) You will get nonsensical results if you change the geometry in the middle of your document unless you setup a new reference point and tell \cs{returntogrid} to use it.
85 \item The skips to return to the grid should normally be inserted only on the main vertical list, not in boxes. But the code doesn't check for inner mode as there could be special cases where it works (e.g. in a tcolorbox). If you use \cs{returntogrid} in such a place think carefully if this can lead to a stable state.
86 \end{enumerate}
87
88 \Needspace{8\baselineskip}
89 \subsection{The main command}
90
91 The main command is called \cs{returntogrid} and it takes one \emph{optional} argument:
92
93 \DescribeMacro\returntogrid[<key-val-list>]
94
95 The command is used for both type of \enquote{moves}: vertical and horizontal. In this section I'm describing the vertical movements, for horizontal movements see section \ref{sec:nextgridtab}.
96
97 Without the optional argument the command should normally be used only at the begin of paragraphs. It will force a new paragraph by issuing a \cs{par}, start the paragraph with a \cs{leavevmode}, then store the current position of the baseline and at the next compilation insert after the \cs{par} a hopefully suitable vskip to move this position to the grid.
98
99 It will always insert a \emph{positive} vskip, even if the previous grid position is much nearer -- I thought about small negative spaces but I'm not sure that this leads to a stable state.
100
101
102 In the optional argument the following keys can be used:
103
104 \begin{description}
105 \item[\PrintKeyName{save}] Value is a label name. With this option \cs{returntogrid} only issues a \cs{leavevmode} and stores the position but doesn't insert any space and doesn't force a new paragraph. With this option a use inside boxes makes sense (see below).
106
107 \item[\PrintKeyName{use}] Value is a label name. With this option \cs{returntogrid} only inserts the vskip needed to move the position stored with the \PrintKeyName{save} option to the grid.
108
109 This can be e.g. used to move a section title or a line of a tabular to the grid
110
111 \begin{lstlisting}
112 \returntogrid[use=mysec]
113 \section[xxxabc]{\returntogrid[save=mysec,strut=1]xxxabc}
114
115 \returntogrid[use=mytab]
116 \begin{tabular}[t]{l}
117 \hline
118 \returntogrid[save=mytab]xxxabc
119 \end{tabular}
120 \end{lstlisting}
121
122
123 \item[\PrintKeyName{label}] Value is a label name. By default \cs{returntogrid} generates numbered label names. This means that if you insert a new \cs{returntogrid} command at the begin of the document all following label names have to be renumbered and all skip calculated anew. As this can need lots of compilations you can avoid this by giving the label a unique name manually. Internally a package specific prefix is always added to the label.
124
125
126 \item[\PrintKeyName{strut}] Value is an integer. This will insert a strut of height $ n \times \cs{baselineskip} + \cs{topskip} $. See section \ref{sec:pagebreak} why this could be needed.
127
128 \item[\PrintKeyName{debug-vgrid}] This adds the command \cs{showdebugpagegrid} to the background of the current page.
129
130 \end{description}
131
132 You should ensure that \cs{returntogrid} isn't use in arguments that wander to the toc or the header as this would give multiply defined labels or insert the vskip in places where you don't want it.
133
134 Also it is up to you to use the save-use system in a sensible way -- if the vskip doesn't move the position or if you use the vskip twice you probably will never get a stable position.
135
136 \subsection{The page break problem}\label{sec:pagebreak}
137
138 The space inserted to force an object onto the grid can push the object to a new page.
139
140 This is a problem if the object doesn't fall naturally on the grid there. In this case one would need a second (non-discardable) space that moves the object further down.
141
142 Currently the code doesn't do this. Instead it expects you to set the height of such problematic objects so that they keep the grid naturally.
143
144
145 \subsection{Top floats}
146
147 Floats at the top of the page can disturb the grid too. There is normally no sensible place to insert a vskip in the text running below such a float to return to the grid. This means that you should either avoid top floats, or give them a height that pushes the text below onto the grid without manual adjustments.
148
149 \Needspace{10\baselineskip}
150 \subsection{Debugging: Showing the grid}
151
152 \DescribeMacro\showdebugpagegrid
153
154 This command requires that you load the package \pkg{tikz}. You can then use it e.g. like this to see the vertical grid lines:
155
156 \begin{lstlisting}
157 \AddToShipoutPictureBG {\AtTextUpperLeft{\showdebugpagegrid}}
158 \end{lstlisting}
159
160
161 \section{Horizontal grid: going to the next tab position}\label{sec:nextgridtab}
162
163 The \cs{returntogridsetup} command described in the following section has a key to define lists of \enquote{tab positions}. It takes two argument: an (unique) name for the tab list and a list of dimension expressions\footnote{This means that e.\,g. sums of length are allowed.}.
164
165 \PrintKeyName{settabpositions}=\MacroArgs{<name>}\MacroArgs{<dim expression>','<dim expression>,\ldots}
166
167 The tab positions are set from the position you are issuing the command, and the dimensions describe the distance between two tabs. The command doesn't insert spaces but it uses a zero-width box, so use it at a places where a such a box doesn't affect your layout. The tabs must be set before the first use.
168
169
170 So
171
172 \begin{lstlisting}
173 abc%
174 \returntogridsetup
175 {
176 settabpositions={demo}{1cm,2\fboxsep +1cm, 2cm}
177 }%
178 text
179 \end{lstlisting}
180
181 sets the following tab positions for the list \enquote{demo}:
182
183 abc%
184 \returntogridsetup
185 {
186 debug-tab,
187 settabpositions={demo}{1cm,2\fboxsep +1cm, 2cm}
188 }%
189 text
190
191 \subsection{Moving to the next tab position}
192
193 You can move to the next tab position by using the \PrintKeyName{tab} key in the optional argument of \cs{returntogrid}:
194
195
196 \Macro\returntogrid[tab=\meta{name}]
197
198
199 The name refers to the name of the tab list from which you want to use tab positions. If you don't give a name, whatever is the currently active tab list name will be used e.g. the one set with the \PrintKeyName{tab-list} option described in section \ref{sec:setup}. If no list with the name exists an error will be raised.
200
201 The code is \emph{very} simple: It issues a \cs{leavevmode}, stores the current position in a label and at the next compilation calculates the distance to the next tab position and inserts a suitable \verb+\hspace*+. If there is no next tab position it inserts a \cs{hfill} unless the key \PrintKeyName{hfill} has been set to false. It doesn't try to avoid line breaks or overfull lines.
202
203 The current position is stored with a automatically generated numbered label. Like in the case of the vertical movements you can avoid that this names changes all the time you add a command by setting a manual name with the key \PrintKeyName{label}.
204
205 As with vertical movements \cs{returntogrid} has a number of restrictions:
206
207 \begin{itemize}
208 \item Pay attention to stretchable space! If you move something to the end of the line, if you have a positive \cs{parfillskip} value, if the movement gives an overfull hbox, \LaTeX\ will perhaps change the word spaces and then no stable hspace value can be found. Use \cs{raggedright} or end the lines with \verb+\\+.
209
210 I advise against setting an explizit tab position at the right margin, it is often unstable (and as the code inserts an \cs{hfill} after the last tab not needed anyway).
211
212
213 \item Don't misuse the command! It is not a replacement for a real tabular or a tabbing as many \cs{returntogrid} in one line or one paragraph can mean many compilations. But it is useful if you want to move e.g. graphics in a three column layout.
214 \end{itemize}
215
216 \subsection{The twoside problem}\label{sec:twoside}
217
218 Like with vertical grid movements changes of the page geometry can lead to nonsensical results: The tab positions are stored in coordinates which are measured from the left side of the page.
219
220 Unlike as the situation with vertical grids geometry changes are actual common: a twoside document has different margins on the odd and even page. Normally you want the tab position to be relative to the text margins, so an offset is needed.
221
222 It \pkg{returntogrid} detects the twoside mode, or you use the \PrintKeyName{twoside} key in the setup command, \pkg{returntogrid} will add the difference between oddsidemargin and evensidemargin as an offset when needed. The code assumes that the page geometry of odd and even pages don't change in the middle of the document!
223
224
225
226 \section{Setup and configuration}\label{sec:setup}
227
228 \DescribeMacro\returntogridsetup{<key-val-list>}
229
230 With this command you can change some defaults of the package.
231
232 \begin{description}
233 \item[\PrintKeyName{active}] Boolean, default is true. It deactivates following \cs{returntogrid} commands.
234
235 \item[\PrintKeyName{step}] Value is a length, the default is \cs{baselineskip}. It sets the distance of the vertical grid.
236
237 \item[\PrintKeyName{reference}] The name of the point used as reference for the vertical grid. The default is a point at the text upper corner stored like this:
238
239 \begin{lstlisting}
240 \AddToShipoutPictureBG*{%
241 \AtTextUpperLeft{%
242 \zsaveposy{ufgrid@vpointtextupperleft}}}
243 \end{lstlisting}
244
245 \item[\PrintKeyName{offset}] Value is a length, the default is \cs{topskip}. The reference point doesn't need to be on the grid, this key sets the offset.
246
247 \item[\PrintKeyName{settabpositions}] This stores tab positions that can be use with the \cs{nextgridtab} command See section \ref{sec:nextgridtab}.
248
249 \item[\PrintKeyName{tab-list}] Value a name. This sets the name of tab-list that will be used if you don't give a name explictely until the enclosing group end.
250
251 \item[\PrintKeyName{hfill}] Boolean. Default is true. When set to false no \cs{\hfill} is inserted after the last tab position.
252
253 \item[\PrintKeyName{twoside}] Boolean. This activates/deactivates the offset described in \ref{sec:twoside} in case the automatic detection doesn't do what you want.
254
255 \item[\PrintKeyName{debug-tab}] This helps to see horizontal tabs when defining them. It should be used before using \PrintKeyName{settabpositions}. It needs the package \pkg{tikz}.
256 \end{description}
257
258
259
260
261
262
263 \newpage
264 \section{Examples}
265
266 \returntogrid In the following text all parts with XXX are forced by the package to return to the grid.
267
268 First some horizontal movements:
269
270 \noindent
271 \returntogridsetup{
272 debug-tab,
273 settabpositions={main}{0.3\textwidth,0.3\textwidth+0.05\textwidth},
274 tab-list={main}
275 }
276
277
278 \noindent abc\returntogrid[tab]X
279
280 \noindent abc\returntogrid[tab,label=mine]X
281
282 \noindent more text more text more text \returntogrid[tab]X
283
284 \noindent more text more text more text more text more text\returntogrid[tab]X
285
286 \noindent more text more text more text more text more text more text more text\returntogrid[tab]X
287
288 \minisec{The code}
289
290 \begin{lstlisting}[breaklines]
291 \noindent
292 \returntogridsetup{
293 debug-tab,
294 settabpositions={main}{0.3\textwidth,0.3\textwidth+0.05\textwidth},
295 tab-list={main}
296 }
297
298
299 \noindent abc\returntogrid[tab]X
300
301 \noindent abc\returntogrid[tab,label=mine]X
302
303 \noindent more text more text more text \returntogrid[tab]X
304
305 \noindent more text more text more text more text more text\returntogrid[tab]X
306
307 \noindent more text more text more text more text more text more text more text\returntogrid[tab]X
308 \end{lstlisting}
309
310
311 \newpage
312 \AddToShipoutPictureBG {\AtTextUpperLeft{\showdebugpagegrid}}
313 moving something to the next page
314 \vspace*{29,1\baselineskip}
315 \returntogrid[use=mysec1]
316 \section[]{\returntogrid[save=mysec1,strut=1]XXX}
317 \rule{0.4pt}{3cm}
318
319
320
321 \minisec{The code}
322
323 \begin{lstlisting}
324 \vspace*{29,1\baselineskip}
325 \returntogrid[use=mysec1]
326 \section[]{\returntogrid[save=mysec1,strut=1]XXX}
327 \rule{0.4pt}{3cm}
328 \end{lstlisting}
329
330
331 \newpage
332
333 \begin{itemize}[itemsep=3pt]
334 \item abc
335
336 \item \returntogrid abc
337
338 \item \returntogrid abc
339 \end{itemize}
340
341 \minisec{The code}
342 \begin{lstlisting}
343 \begin{itemize}[itemsep=3pt]
344 \item abc
345
346 \item \returntogrid abc
347
348 \item \returntogrid abc
349 \end{itemize}
350 \end{lstlisting}
351
352 \newpage
353 a
354
355 \vspace{3pt}
356
357 \returntogrid
358 xxxa
359
360
361
362 \returntogrid
363 \LARGE xxxabc
364 \normalsize
365
366
367
368 \returntogrid[use=mytab]
369 \begin{tabular}[t]{l}
370 \returntogrid[save=mytab]xxxsome text\\
371 some text\\
372 some text\\
373 some text\\
374 some text\\
375 some text\\
376 some text \\some text\\some text
377 \end{tabular}
378
379 \returntogrid[use=mysec2]
380 \section[]{\returntogrid[save=mysec2,strut=1]xxxabc}
381
382 abc \\abc
383
384 \newpage
385 \ClearShipoutPictureBG
386 \minisec{The code}
387 \begin{lstlisting}
388 a
389
390 \vspace{3pt}
391
392 \returntogrid
393 xxxa
394
395
396
397 \returntogrid
398 \LARGE xxxabc
399 \normalsize
400
401
402
403 \returntogrid[use=mytab]
404 \begin{tabular}[t]{l}
405 \returntogrid[save=mytab]xxxsome text\\
406 some text\\
407 some text\\
408 some text\\
409 some text\\
410 some text\\
411 some text \\some text\\some text
412 \end{tabular}
413
414 \returntogrid[use=mysec2]
415 \section[]{\returntogrid[save=mysec2,strut=1]xxxabc}
416
417 abc \\abc
418 \end{lstlisting}
419 \end{document}