Debian Codegraph @

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

raw
1 \documentclass{ltxdoc}
2 \newcommand\authormail[1]{\footnote{\textless\url{#1}\textgreater}}
3 \ifdefined\HCode
4 \renewcommand\authormail[1]{\space\textless\Link[#1]{}{}#1\EndLink\textgreater}
5 \fi
6
7 \ifdefined\gitdate\else
8 \def\gitdate{\today}
9 \def\version{devel}
10 \fi
11
12
13
14 % \usepackage[noautomatic]{responsive}
15 \usepackage[]{responsive}
16 \usepackage{linebreaker}
17 \usepackage{caption}
18 \usepackage{subcaption}
19 \usepackage{graphicx}
20 \usepackage{microtype}
21 \usepackage{longtable}
22 \usepackage{booktabs}
23 \usepackage[style=iso-authoryear]{biblatex}
24 \addbibresource{responsive-doc.bib}
25 \newenvironment{changelog}{\longtable{@{} l p{20em}}}{\endlongtable}
26 \newcommand\change[2]{#1 & #2\\}
27 \ifdefined\HCode
28 % I want to convert parts of the document to images,
29 % so I need to use Type 1 fonts for the correct conversion with Dvisvgm
30 \usepackage{ebgaramond}
31 \else
32 \usepackage{fontspec}
33 \setmainfont{EB Garamond}[Numbers=OldStyle]
34 \setmonofont{Inconsolatazi4-Regular.otf}[Scale=MatchLowercase]
35 \fi
36
37 \newcommand\StartDescribe[1]{%
38 \par\medskip\noindent\DescribeMacro#1
39 }
40 \usepackage{lipsum}
41 \title{The Responsive Package}
42 \author{Michal Hoftich\authormail{[email protected]}}
43 \date{Version \version, \gitdate}
44 \NewDocElement[macrolike = false ,
45 toplevel = false,
46 idxtype
47 = key,
48 idxgroup = Package keys,
49 % printtype = \textit{key}
50 ]{Key}{key}
51
52 \newcommand\printsize[1]{\csname #1\endcsname\par\noindent Sample\par}
53 \newcommand\showscale[2][.5\textwidth]{%
54 \noindent\fbox{%
55 \begin{minipage}{#1}
56 \ResponsiveSetup{#2}
57 \setsizes[34]{25}
58 \printsize{huge}
59 \printsize{LARGE}
60 \printsize{Large}
61 \printsize{large}
62 \hrule
63 \printsize{normalsize}
64 \hrule
65 \printsize{small}
66 \printsize{footnotesize}
67 \end{minipage}
68 }
69 \ifx\relax#2\relax Default parameters\else\texttt{#2}\fi\hfill\null
70 }
71 \makeindex
72
73 \begin{document}
74 \maketitle
75 \begin{abstract}
76 Responsive design aims to display text and other design elements well on
77 variety of outputs, including electronic devices or various paper sizes.
78 It originated on the Web, where it uses Cascading Style Sheets to change
79 design elements.
80
81 This package tries to achieve similar result with \LaTeX. The main aim is to
82 support output of multiple editions of one document with
83 readable font sizes regardless of the physical size of the produced PDF,
84 from smartphones or e-ink readers to printed A4 pages.
85 \end{abstract}
86
87 \clearpage
88
89 \tableofcontents
90
91 \section{Introduction}
92
93 This package aims to facilitate the creation of documents for multiple output
94 formats, such as various page sizes, e-book readers, or even mobile phones.
95
96 One of the issues that needs to be addressed is setting the correct font size
97 for readability. The default font size in LaTeX is 10 points, regardless of the
98 page size. This is an appropriate font size for A5 format pages. For A4 format,
99 the font size should be larger, while it may be smaller for smaller displays of
100 e-readers and mobile phones. Similarly, we can adjust the line spacing, which
101 also affects text readability depending on the font size and page size.
102
103 Web browsers face a similar problem as they must display text on both large PC
104 monitors and smaller displays of laptops, tablets, and mobile phones. The
105 solution they use is called \textit{responsive design}.
106
107 Responsive design is a method of web design that allows flexible and dynamic
108 adaptation of the appearance and layout of page content to different display
109 devices. One of the key elements of responsive design is a flexible structure
110 that enables adjusting the size of elements on the page to the display device.
111
112 Another important element is media queries. These allow defining rules based on
113 the properties of the display device, such as screen width and height or output
114 type (paper, display). Thanks to these rules, the same page code can be
115 well-displayed on both large monitors and mobile devices or when printing.
116
117 The Responsive package is inspired by these principles. Its main function
118 is to set the font size based on the page size and the approximate number of
119 characters that should fit on the page (see figure~\ref{fig:example}). It also adjusts the typographic scale
120 (influencing font size, for example, in headings or footnotes), line height,
121 and supports a simple version of media queries.
122
123
124 \section{Features}
125
126
127 \subsection{Fitting Font Size to the Page Size}
128
129 Regardless of the width of the page, there is a certain range of the number of
130 characters that should be displayed on average per line of text to ensure good
131 readability. With a larger number of characters, the reader may have difficulty
132 quickly moving to the next line, while shorter lines may lead to skipping
133 lines.
134
135 The Responsive package automatically sets the width of the text to a chosen number of characters.
136 Figure~\ref{fig:example} shows a difference in the font size in two
137 different text widths.
138
139
140 \begin{figure}[htbp]
141 \caption{Example of changing font size based on the width of the text block:}\label{fig:example}
142 \begin{subfigure}[t]{0.45\textwidth}
143 \centering
144 \fbox{%
145 \begin{minipage}{4cm}
146 \ResponsiveSetup{}
147 \setsizes{}
148
149 \lipsum[1]
150
151 \end{minipage}}
152 \caption{Text width 4\,cm}
153 \end{subfigure}
154 % \hfill
155 \begin{subfigure}[t]{0.45\textwidth}
156 \centering
157 \fbox{%
158 \begin{minipage}{6cm}
159 \ResponsiveSetup{}
160 \setsizes{}
161
162 % \lipsum[1]
163 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
164 vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
165 mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,
166 magna. Donec vehicula augue eu neque. Pellentesque habitant morbi
167 tristique senectus et netus et malesuada fames ac turpis egestas. Mauris
168 ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna
169 fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer
170 sapien est, iaculis in, pretium quis, viverra ac, nunc.
171
172 \end{minipage}}
173 \caption{Text width 6\,cm}
174 \end{subfigure}
175 \end{figure}
176
177
178 \subsection{Typographic Scale}
179
180 The typographic scale is a set of predetermined font sizes used to establish a
181 consistent visual style for a document or website. These sizes are typically
182 expressed in point units and progressively increase or decrease by a specific
183 interval along the scale.
184
185 A typographic scale may include sizes for headings, footnotes, and body text.
186 Proper use of a typographic scale helps create a visual hierarchy that enhances
187 the readability and aesthetic appeal of the text.
188
189 In \LaTeX, the typographic scale is accessible through commands such as \verb|\large|,
190 \verb|\huge|, or \verb|\scriptsize|. Each of these commands is one interval
191 away from the previous size.
192
193 I drew inspiration from Spencer Mortensen's article (\citeyear{mortensen}), which defines several
194 different typographic scales and provides a method for easily creating
195 additional scales.
196 The default scale in the Responsive package,
197 which closely aligns with the scale used in \LaTeX, is referred to as \verb|tetratonic|.
198 The package also offers additional scales described in the
199 article, such as the golden scale based on the golden ratio.
200 See Figure~\ref{fig:scale} for differences between the default scale and scale based on the
201 golden ratio.
202
203 The Responsive package includes several predefined typographic scales, but you
204 can also define your own. All scales are defined using the options
205 \verb|number| and \verb|ratio|, specifying how many steps (\verb|number|) the
206 font size increases by the size of \verb|ratio|. For example scale with \verb|number| of 2
207 and \verb|ratio| of 5, will double the font size over 5 steps.
208 Table~\ref{tbl:scales} lists all predefined scales.
209
210 \begin{table}[htbp]
211 \centering
212 \begin{tabular}{l l l}
213 \toprule
214 name & number & ratio\\
215 \midrule
216 heptatonic & 7 & 2\\
217 pentatonic & 5 & 2\\
218 tetratonic & 4 & 2\\
219 tritonic & 3 & 2\\
220 golden & 2 & 1.618\\\bottomrule
221 \end{tabular}
222 \caption{Predefined typographic scales}\label{tbl:scales}
223 \end{table}
224
225
226 \begin{figure}[htbp]
227 \caption{Sample of Typographic Scales (the default font size is highlighted with lines)}\label{fig:scale}
228 \begin{subfigure}[b]{0.45\textwidth}
229 \fbox{%
230 \begin{minipage}{5cm}
231 \setsizes{45}
232
233 \showscale{}
234
235 \end{minipage}}
236 \caption{Default scale}
237 \end{subfigure}
238 \begin{subfigure}[b]{0.45\textwidth}
239 % \begin{verbatim}
240 % \ResponsiveSetup{scale=golden}
241 % \end{verbatim}
242 \fbox{%
243 \begin{minipage}{5cm}
244 % \ResponsiveSetup{scale=golden}
245 \setsizes{45}
246
247 \showscale{scale=golden}
248
249 \end{minipage}}
250 \caption{Golden ratio scale}
251 \end{subfigure}
252 \end{figure}
253 % % https://spencermortensen.com/articles/typographic-scale/
254
255
256
257 % \showscale{}
258 % \showscale{scale=heptatonic}
259 % \showscale{scale=tritonic}
260 % \showscale{scale=golden}
261 % \showscale{scale=none,ratio=1.3,number=2}
262
263 \subsection{Line height}
264
265 In the default settings of \LaTeX, the line spacing is set to the font size
266 multiplied by a factor of 1.2. For different fonts and page sizes, a different
267 line spacing may be appropriate. Similarly, distinct values may be suitable for
268 the printed and electronic versions of the document.
269
270 The Responsive package provides two options which allows you to change the line spacing
271 (Figure~\ref{fig:lineratio}).
272
273 First is the \verb|lineratio| option, which bases the line height
274 on the ratio between x-height of the current font and the desired height.
275 I've found this method in article by Edoardo Cavazza in the \textit{Smashing Magazine}~(\citeyear{cavazza}).
276 The value of the \verb|lineratio| option determines what percentage of the total line
277 height is the \verb|x-height|. The higher this value, the lower the line will be. For
278 different fonts, the same ratio may lead to different line heights because each
279 font has a different \verb|x-height| value. The appropriate lineratio size needs to be
280 determined experimentally. The default value should roughly correspond to a
281 line height of 1.2, the default in \LaTeX.
282
283 Another option is \verb|lineheight|, which
284 is similar to the CSS property \verb|line-height|. Using it, we can choose the
285 line height as a multiple of the font size.
286
287
288
289
290 \begin{figure}[htbp]
291 \caption{Changing the line spacing by adjusting the value of \texttt{lineratio}.}\label{fig:lineratio}
292 \begin{subfigure}[b]{0.45\textwidth}
293 \fbox{%
294 \begin{minipage}{5cm}
295 \ResponsiveSetup{lineratio=38}
296 \setsizes{65}
297
298 \lipsum[1]
299
300 \end{minipage}}
301 \caption{lineratio=38}
302 \end{subfigure}
303 \begin{subfigure}[b]{0.45\textwidth}
304 \fbox{%
305 \begin{minipage}{5cm}
306 \ResponsiveSetup{lineratio=34}
307 \setsizes{65}
308
309 \lipsum[1]
310
311 \end{minipage}}
312 \caption{lineratio=34}
313 \end{subfigure}
314 \end{figure}
315
316 % https://www.smashingmagazine.com/2020/07/css-techniques-legibility/
317
318 \subsection{Media Queries}
319
320 Media queries are a technique that allows web developers to dynamically adapt
321 the appearance and behavior of websites based on various device properties,
322 such as screen width and height, device orientation, color support, and more.
323 Thanks to these conditions, responsive and flexible websites can be created
324 that automatically adjust to different types and sizes of devices on which they
325 are displayed.
326
327 How can this technique be useful for \LaTeX\ package authors? They could, for
328 example, set the font size, line spacing, and other elements for specific page
329 dimensions. Once the user chooses the page size based on the device size for
330 which they want to compile the document, these elements are automatically
331 adjusted. For instance, a package author could define that if the width of the
332 text block is smaller than a certain dimension, fewer characters will be
333 displayed on each line compared to longer lines (Figure~\ref{fig:mediaquery}).
334
335 The media query used for this figure looks like this:
336
337 \begin{verbatim}
338 \mediaquery{max-textwidth=4cm}
339 {\ResponsiveSetup{lineratio=38}\setsizes{45}}
340 \ResponsiveSetup{lineratio=34}\setsizes{60}}
341 \end{verbatim}
342
343 This media query defines that if the width of the text is smaller than or equal to 4 cm, a
344 smaller number of characters will be displayed per line to prevent the font
345 from being too small and difficult to read. Additionally, a smaller line
346 spacing will be applied.
347
348
349
350 \newcommand\mdtest{%
351 \mediaquery{max-textwidth=4cm}
352 {\ResponsiveSetup{lineratio=38}\setsizes{45}}
353 {\ResponsiveSetup{lineratio=34}\setsizes{60}}
354 \lipsum[1]
355 }
356
357 \begin{figure}[htbp]
358 % \centering
359 % \begin{subcaptionblock}{0.25\textwidth}
360 \caption{Change number of characters and line spacing depending on the text width using Media queries}\label{fig:mediaquery}
361 % \begin{subcaptiongroup}
362 \begin{subfigure}[t]{0.40\textwidth}
363 \centering
364 \begin{minipage}{4cm}
365 { \mdtest
366
367 }
368 \end{minipage}
369 \caption{Text width of 4\,cm with 45 characters}
370 \end{subfigure}
371 \hfill
372 % \hspace{2em}
373 \begin{subfigure}[t]{0.55\textwidth}
374 \centering
375 \begin{minipage}{6cm}
376 { \mdtest
377
378 }
379 \end{minipage}
380 \caption{Text width of 6\,cm with 60 characters}
381 \end{subfigure}
382
383 \end{figure}
384
385 The tests available in media queries are listed in Table~\ref{tbl:mediaqueries}.
386 Those expecting dimensional units also have variants using \texttt{min-} and \texttt{max-}, which
387 do not test exact values but rather whether the provided value is less than,
388 greater than, or equal to the tested parameter. Tests without these variants evaluate for an exact match.
389 The test for a two-column layout is special in that it does not expect any
390 testing values. It evaluates to true if the document uses the \texttt{twocolumn} option.
391
392
393
394 \begin{table}[htbp]
395 \centering
396 \begin{tabular}{ll}
397 \toprule
398 test & possible values \\
399 \midrule
400 textwidth & dimensional unit \\
401 textheight & dimensional unit \\
402 paperwidth & dimensional unit\\
403 paperheight & dimensional unit\\
404 orientation & landscape, portrait\\
405 twocolumn & none \\
406 \bottomrule
407 \end{tabular}
408 \caption{Tests available in media queries}\label{tbl:mediaqueries}
409 \end{table}
410
411
412
413
414 \section{Package Options}
415
416 The following options can be passed as package options used in \cmd{\usepackage},
417 or later in the document using the \cmd{\ResponsiveSetup} command.
418
419 \medskip
420
421 \noindent \DescribeKey{characters} Approximate number of characters that should fit to a text line. Default value is 66.
422
423 \noindent \DescribeKey{noautomatic} Disable automatic setting of font sizes, line spacing and typographic scale in the document. You can set them
424 later using the \cmd{\setsizes} and \cmd{\ResponsiveSetup} commands.
425
426 \noindent \DescribeKey{scale} Select named typographic scale. Default scale is
427 \texttt{tetratonic}, which is the most similar to the default scale used by
428 \LaTeX.
429
430 \noindent \DescribeKey{number} The number of steps during which the text size is multiplied by the amount specified in the \texttt{ratio} option.
431
432 \noindent \DescribeKey{ratio} The amount by which the font size will increase over a given number of steps.
433
434 \noindent \DescribeKey{lineratio} The ratio used in calculating the line spacing. Default value is 33.3333, which should produce similar line spacing as the default \LaTeX\ setting.
435
436 \noindent \DescribeKey{lineheight} Calculate line spacing by a fixed ratio, similarly to the CSS \texttt{lineheight} property.
437
438 \noindent \DescribeKey{boxwidth} Width of line that is used for font size calculations in the \cmd{\setsizes} command. Default value is \cmd{\textwidth}.
439
440
441
442
443 \section{Commands}
444
445 \StartDescribe\ResponsiveSetup
446 \cmd\ResponsiveSetup\marg{options}
447
448 \noindent It allows you to reset the package options later in the document. You
449 need to call the \cmd{\setsizes} command to reset font sizes explicitly if you use this command
450 outside of the document preamble, or when using the \texttt{noautomatic} option.
451
452 \StartDescribe\setsizes
453 \cmd\setsizes\oarg{line height ratio}\marg{number of characters per line}
454
455 \noindent It sets the text size so that the specified average number of
456 characters fit on a line. If you do not provide the number of characters, the
457 package option \texttt{characters} will be used.
458
459
460 \StartDescribe\fonttobox
461 \cmd\fonttobox\oarg{line height ratio}\marg{text width}\marg{text}
462
463 \noindent Set the text size so that the specified text in the second parameter has
464 the specified size in the first parameter. Optionally, you can set the line
465 height ratio.
466
467 \StartDescribe\mediaquery
468 \cmd\mediaquery\marg{media query test}\marg{true code}\marg{false code}
469
470 \noindent
471 It tests the document's properties using media queries and executes code based on the test results.
472 The code can typically use \cmd\ResponsiveSetup, but you can also execute any other commands.
473
474 \StartDescribe\DeclareMediaQueryMatcher
475 \cmd\DeclareMediaQueryMatcher\marg{test name}\marg{\LaTeX\ 3 boolean query test}
476
477 \noindent Declares a new test for a media query. The tests can use LaTeX 3 boolean tests,
478 and in case of a positive result, it must execute the command
479 \cmd\mediaquerytrue.
480
481 \StartDescribe\fixtextheight
482 \cmd\fixtextheight
483
484 \noindent It adjusts the page height, header, and footer to prevent underflow
485 \cmd{\vbox} warnings in the output routine.
486
487
488
489 \section{Troubleshooting}
490
491 Occasionally, you can run into issues caused by the change of font sizes. \LaTeX\ sets lot of parameters
492 depending on the base font size. We try to recalculate them according to the new font size, but you can still
493 experience some issues, described in following subsections.
494
495 \subsection{Underfull \texttt{\textbackslash vbox} warning}
496
497 Occasionally, you can get the following warning:
498
499 \begin{verbatim}
500 Underfull \vbox (badness 6412) has occurred while \output is active []
501 \end{verbatim}
502
503 We calculate the page height that suits for the font size, but often,
504 we would get error when a bigger font is used on the page,
505 for example in chapters. When you set font sizes in the document preamble,
506 text height should be changed automatically. If you reset font sizes later in the
507 document, you need to use the \cmd{\fixtextheight} command. It should help in
508 most cases. If not, you can try the following hack.
509 This trick should fix \cmd{\vbox} errors in the output routine, but it can lead
510 to an irregular number of lines on pages.
511
512
513 \begin{verbatim}
514 \ExplSyntaxOn
515 \def\@textbottom{\vskip \z@ \@plus \resp_font_size \@minus \resp_font_size}
516 \ExplSyntaxOff
517 \end{verbatim}
518
519 \section{License}
520
521 Permission is granted to copy, distribute and/or modify this software
522 under the terms of the \LaTeX\ Project Public License, version 1.3.
523
524
525 \section{Changes}
526
527 \begin{changelog}
528 \change{2024-07-14}{Version 0.1a released}
529 \change{2024-03-16}{Set the \texttt{topmargin} dimension}
530 \change{}{Call Geometry's \texttt{\textbackslash Gm@process} command to fix page dimensions, if Geometry is loaded}
531 \change{2023-12-13}{Version 0.1 released.}
532 \change{}{Finished documentation.}
533 \change{2023-12-07}{Added the \texttt{lineheight} property.}
534 \change{}{Use floating point numbers for the lineratio.}
535 \change{2023-12-06}{ChangeLog started.}
536 \end{changelog}
537 % \section{Examples}
538 % \PrintIndex
539
540 \printbibliography[heading=bibintoc]
541
542
543
544
545
546 \end{document}