Debian Codegraph @

texmf-dist/doc/latex/easyfloats/content/x1_motivation.tex

raw
1 % !TeX root = ../easyfloats.tex
2
3 \section{Motivation}
4 \label{motivation}
5
6 In this section I will explain how to insert figures and tables in standard \LaTeX\ without this package and how this package can improve that.
7 If you are only interested in how to use this package not why, see \cref{examples} for examples and \cref{documentation} for an explanation of the commands, environments and options defined by this package.
8
9 \subsection{Graphics}
10 \label{graphics}
11
12 Inserting a graphic without using this package requires 6 lines of code (\pkg{graphicx} or \pkg{graphbox} must be loaded for `\includegraphics`):
13 \begin{examplecode\starred}{\ExamplecodeNoBox\ExamplecodeLinenumbers}
14 \begin{figure}
15 \centering
16 \includegraphics[graphic width=.8\linewidth]{ctan_lion}
17 \caption{CTAN lion drawing by Duane Bibby}
18 \label{ctan_lion}
19 \end{figure}
20 \end{examplecode\starred}
21
22 \begin{description}
23 \item[Lines 1 and 6]
24 open\slash close a floating environment.
25 The content of this environment can float around so that it won't cause a bad page break.
26 You don't need this if you really just want to insert a graphic exactly here (like a logo in a header)
27 but a graphic cannot break across pages so if it is too large for the end of the current page it will move to the next page leaving the end of this page empty.
28 This is a waste of paper and may confuse a reader by suggesting this might be the end of a chapter.
29 A floating environment can help you by putting the figure where it fits best.
30
31 The placement determines where a float is allowed to be placed.
32 Initially that's the top or bottom of a text page or a separate page just for floats.
33 The placement can be specified for a single floating object by passing an optional argument to the floating environment
34 or for all floating objects using the `\floatplacement` command defined by the \pkg{float} package.
35 (The floating environments `figure` and \env{table} are standard \LaTeX\ and do not require the \pkg{float} package.)
36 The allowed values for the placement are described in the description of the \env{object} environment's `placement` key.
37
38 There are people who are concerned that a figure not sitting at the exact position might confuse a reader.
39 However, a graphic naturally attracts the reader's attention.
40 Therefore it does not matter where it is located on the double page.
41 The reader will see it.
42
43 Of course the author must ensure that the figure does not float too far away.
44 If that is the case changing the size of this or another graphic,
45 `\usepackage[section]{placeins}`,
46 `\FloatBarrier` (defined by the \pkg{placeins} package),
47 moving this block of lines in the code,
48 changing the placement or
49 tweaking the parameters which govern the placing of floats \autocite[page~28]{latex2e}
50 can help.
51
52 \item[Line 2]
53 centers the graphic horizontally on the line.
54
55 The `\centering` command is used instead of the `center` environment because the latter would insert additional vertical space.
56
57 \begin{examplecode\starred}{\ExamplecodeNoBox}
58 \begin{center}
59 ...
60 \end{center}
61 \end{examplecode\starred}
62 is in \LaTeX2\footnote{This will change in \LaTeX3~\autocite{ltx3env}.}
63 \makeatletter
64 (somewhat simplified\footnote{`\begin` checks that it's argument is defined, `\end` checks that it's argument matches that of `\begin` and deals with `\ignorespacesafterend` and `\@endparenv`. Since 2019/10/01 `\begin` and `\end` are robust. Since 2020/10/01 they include hooks. \autocite[section \sectionname{ltmiscen.dtx}]{source2e}})
65 \makeatother
66 equivalent to
67 \begin{examplecode\starred}{\ExamplecodeNoBox}
68 \begingroup
69 \center
70 ...
71 \endcenter
72 \endgroup
73 \end{examplecode\starred}
74
75 This means that if you accidentally try to use `\centering` as an environment instead of a command you will *not* get an error.
76 You might expect to get an error at least for `\endcentering` not being defined
77 but the \TeX\ primitive `\csname` which is used to produce the `\endcentering` token instead defines it to `\relax`, a no operation.
78
79 The output, however, will not be as desired: the group is closed before the end of the paragraph and `\centering` is forgotten before it can take effect.
80
81 \item[Line 3]
82 inserts the graphic.
83 This requires the \pkg{graphicx} or \pkg{graphbox} package.
84
85 If you want all graphics to have the same width you can set the `width` globally with `\setkeys{Gin}{width=<dimen>}`.
86 However, that does not work with all options.
87 Unfortunately the \pkg{graphicx} package documentation~\autocite[section~4.6]{graphicx} is not getting more specific than that this works with \enquote{Most of the keyval keys}.
88
89 \item[Line 4]
90 inserts the caption.
91
92 Captions for a figure should be placed *below* the figure.
93 Captions for a table should be placed *above* the table.
94 \autocite{texexchange_caption_position}
95
96 `\caption` can be used inside of a floating environment only.
97 If you need a caption for a non-floating object you can either use `\captionof{<type>}{<caption>}` defined by the \pkg{capt-of} or \pkg{caption} package
98 or use a floating environment with the placement `H` defined by the \pkg{float} package.
99
100 \item[Line 5]
101 defines a label.
102 This is not visible in the output but can be referenced using `\ref{<label>}` or `\pageref{<label>}`.
103 You might want to consider using the \pkg{cleveref} package for references.
104
105 The label must be set inside of or after the caption.
106 A label always refers to the last `\refstepcounter` inside of the current group. \autocite[section \sectionname{ltxref.dtx}]{source2e}
107 `\refstepcounter` is used for example by `\caption` and `\section`.
108 Therefore if you use `\label` after the caption it refers to the caption.
109 If you use it before the caption it refers to the current section\slash subsection\slash subsubsection.
110 \end{description}
111
112 There are many things that a beginner can do wrong without even getting a warning.
113 Three out of this six lines are always the same (lines 1, 2 and~6).
114 I don't want to always write them out.
115 There is no way to easily switch floating on or off globally.
116
117 \bigpar
118
119 This package reduces these six lines to a single command and loads \pkg{graphicx} automatically (unless this package is loaded with the `nographic` option).
120 \begin{examplecode\starred}{}
121 \includegraphicobject[%
122 caption = CTAN lion drawing by Duane Bibby,
123 graphic width = .8\linewidth,
124 ]{ctan_lion}
125 \end{examplecode\starred}
126
127 The floating environment is applied automatically.
128 It can be changed using the `type` key but I discourage doing so manually.
129 Instead I recommend to use the separate optional `<style>` argument if necessary.
130 If you do not want the object to float you can pass `placement=H`.
131 This works also globally with `\objectset`.
132
133 `\centering` is applied automatically.
134 It can be changed using the `align` key.
135
136 You can set any of the options passed to the `\includegraphics` command globally using:
137 \begin{examplecode\starred}{}
138 \objectset[figure]{graphic width=.8\linewidth}
139 \end{examplecode\starred}
140
141 Caption and label can be passed as options.
142 Which one is specified first makes no difference.
143 I recommend to stick with caption first in case you ever need to work without this package and to not confuse other people who are not familiar with this package.
144 If you omit one of them the file name is used.
145 See `auto label`, `auto caption`, `auto label strip path` and `auto caption strip path`.
146
147 Whether the caption is put above or below the object is specified by the `float style`.
148
149 \subsection{Tables}
150 \label{tables}
151
152 Inserting a table is similar to inserting a graphic except that you replace the `\includegraphics` command with an environment which creates a table, place the caption above the table not below it and use another floating environment, namely \env{table} instead of `figure`.
153
154 The following example (not using this package) requires the \pkg{booktabs} package for the horizontal rules and the \pkg{caption} package to have an appropriate space below the caption.
155
156 \begin{examplecode\starred}{\ExamplecodeNoBox\ExamplecodeLinenumbers}
157 \begin{table}
158 \centering
159 \caption{Some catcodes}
160 \label{catcodes}
161 \begin{tabular}{cl}
162 \toprule
163 Catcode & Meaning \\
164 \midrule
165 0 & Escape Character \\
166 1 & Begin Group \\
167 2 & End Group \\
168 \vdots & \quad \vdots \\
169 \bottomrule
170 \end{tabular}
171 \end{table}
172 \end{examplecode\starred}
173
174 What I have said about floating environments, `\centering`, `\caption` and `\label` in \cref{graphics} is also valid for tables.
175 New are lines 5--14.
176 We now have two environments nested inside of each other.
177 The outer environment (lines 1 and~15) is the floating environment.
178 The inner environment (lines 5--14) is the environment which creates the table.
179 The inner environment takes a column specification telling \LaTeX\ how many columns the table has and how they are supposed to be aligned.
180 In this case that is `cl`: Two columns, the first centered, the second left aligned.
181 For more information about column specifications see the \pkg{array} package documentation~\autocite[section~1]{array}.
182
183 `\toprule`, `\midrule` and `\bottomrule` (defined by the \pkg{booktabs} package) produce horizontal lines.
184 They differ in the width of the line and\slash or spacing around them.
185 In contrast to the standard \LaTeX\ `\hline` command they have proper spacing around them.
186
187 `&` separates columns, `\\` separates rows.
188 Indentation and spaces at the beginning and end of a cell are ignored.
189
190 \bigpar
191
192 Using this package we don't need two environments and we don't even need to type out the rule commands if we use `table head`.
193 The packages \pkg{caption}, \pkg{booktabs} and \pkg{array} are loaded automatically (unless you load this package with `nobooktabs` or `noarray`).
194 \begin{examplecode\starred}{}
195 \begin{tableobject}{%
196 caption = Some catcodes,
197 label = catcodes,
198 env = tabular,
199 arg = cl,
200 table head = Catcode & Meaning,
201 }
202 0 & Escape Character \\
203 1 & Begin Group \\
204 2 & End Group \\
205 \vdots & \quad \vdots \\
206 \end{tableobject}
207 \end{examplecode\starred}
208
209 Also we gain the possibility to easily switch between different tabular-like environments, see \cref{longtable} and the example given for the `(<env>) arg(s) +` key in \cref{object-environment}.
210
211 \subsection{Subobjects}
212 \label{subobjects}
213
214 There are several packages to combine several figures\slash tables into a single floating environment.
215 \mycite{l2tabu} recommends using \pkg{subcaption} over \pkg{subfig} and the long deprecated \pkg{subfigure}.
216
217 The \pkg{subcaption} package provides several ways to do this.
218 The first one is using the `\subcaptionbox` command.
219
220 \begin{examplecode\starred}{\ExamplecodeNoBox\ExamplecodeLinenumbers}
221 \begin{table}
222 \centering
223 \caption{Category and character codes}
224 \label{codes}
225 \subcaptionbox{Category codes\label{catcodes}}{%
226 \begin{tabular}{cl}
227 \toprule
228 Catcode & Category \\
229 \midrule
230 0 & Escape Character \\
231 1 & Begin Group \\
232 2 & End Group \\
233 \vdots & \quad \vdots \\
234 \bottomrule
235 \end{tabular}%
236 }%
237 \qquad
238 \subcaptionbox{Character codes\label{charcodes}}{%
239 \begin{tabular}{cr<{\hspace{1.3em}}}
240 \toprule
241 Character & \multicolumn1c{Charcode} \\
242 \midrule
243 \textbackslash & \number`\\ \\
244 \{ & \number`\{ \\
245 \} & \number`\} \\
246 \vdots & \vdots \phantom{0} \\
247 \bottomrule
248 \end{tabular}%
249 }%
250 \end{table}
251 \end{examplecode\starred}
252
253 As the subobjects are inside of an argument they cannot contain code which relies on changing catcodes e.g.\ `\verb`.
254 Aside from that it just doesn't seem elegant to put an environment inside of an argument.
255
256 If you accidentally put the label in the second argument of `\subcaptionbox` instead of in the first it refers to the parent object instead of the subobject and you won't get an error or a warning for that.
257
258 Note how I have commented out line breaks in order to avoid undesired spaces.
259
260 The second way is to use the `subfigure`\slash `subtable` environment.
261 Because the subobject is not inside of an argument it is possible to use `\verb`.
262
263 \begin{examplecode\starred}{\ExamplecodeNoBox\ExamplecodeLinenumbers}
264 \begin{table}
265 \caption{Category and character codes}
266 \label{codes}
267 \begin{subtable}{.5\linewidth}
268 \centering
269 \caption{Category codes}
270 \label{catcodes}
271 \begin{tabular}{cl}
272 \toprule
273 Catcode & Category \\
274 \midrule
275 0 & Escape Character \\
276 1 & Begin Group \\
277 2 & End Group \\
278 \vdots & \quad \vdots \\
279 \bottomrule
280 \end{tabular}%
281 \end{subtable}%
282 \begin{subtable}{.5\linewidth}
283 \centering
284 \caption{Character codes}
285 \label{charcodes}
286 \begin{tabular}{cr<{\hspace{1.3em}}}
287 \toprule
288 Character & \multicolumn1c{Charcode} \\
289 \midrule
290 \verb|\| & \number`\\ \\
291 \verb|{| & \number`\{ \\
292 \verb|}| & \number`\} \\
293 \vdots & \vdots \phantom{0} \\
294 \bottomrule
295 \end{tabular}%
296 \end{subtable}%
297 \end{table}
298 \end{examplecode\starred}
299
300 But why having different environments for subfigures and subtables?
301 The floating environment specifies the type already.
302
303 These environments are based on a minipage and require you to always explicitly specify the width of this minipage.
304 On the one hand I don't want to always type that out.
305 On the other hand I want to be able to change the width once for all subobjects for easier consistency.
306
307 Caption and label must be placed correctly, see \cref{graphics}.
308 Even if you restyle the floating environment to always put the caption at the top or bottom using the \pkg{float} package this does *not* apply to subobjects.
309
310 It is important to comment out line breaks because the widths of the two minipages add up to the line width, a space between them would cause an overfull hbox or a line break.
311
312 We need two `\centering`s, one for each subobject.
313 Remember what I said about `\centering` and `center` in \cref{graphics}.
314
315 \bigpar
316
317 This package defines an environment called `subobject` which is a unified wrapper around `\subcaptionbox` and `subfigure`\slash `subtable`.
318 Which of these two backends should be used can be specified with the `subcaptionbox` and `subpage` options.
319 `subpage` is used by default so that you can usually use `\verb` in the content.
320
321 `subobject` can be used inside of any `*object` environment.
322 If you define a new object environment with `\NewObjectStyle` it defines a corresponding subpage environment like `subfigure`\slash `subtable` if it does not exist already and if the \pkg{caption} package is new enough.
323 If the \pkg{caption} package is older than August~30, 2020 you need to define the subtype manually by putting the following line *before* loading this package \autocite{texexchange_subtype_workaround}:
324 \begin{examplecode\starred}{}
325 \AtBeginDocument{\DeclareCaptionSubType{<type>}}
326 \end{examplecode\starred}
327
328 You don't need to write out the width, `.5\linewidth` is used automatically. You can change this value for all subobjects using
329 \begin{examplecode\starred}{}
330 \objectset{subobject linewidth=<dimen>}
331 \end{examplecode\starred}
332
333 Caption and label are given as options like for `tableobject`.
334 Their order does not matter.
335 They are placed above or below the subobject based on the internal command `\caption@iftop` defined by the \pkg{caption} package.
336
337 Spaces after `\begin{subobject}` and before and after `\end{subobject}` are ignored so you don't need to comment out the line breaks.
338 \unskip\footnote{Actually, spaces after `\begin{subobject}` and before `\end{subobject}` are ignored only if `env` is empty. But if `env` is not empty I am expecting it to be a tabular-like environment where spaces are ignored at the beginning and end of a cell or a tikzpicture where spaces are ignored as well. Spaces after `\end{subobject}` are ignored regardless of `env`.}
339 Just make sure you don't have an empty line between the subobject environments.
340 That would *not* be ignored.
341
342 `\centering` is inserted automatically. It can be changed with `subpage align`.
343
344 \begin{examplecode\starred}{}
345 \begin{tableobject}{caption=Category and character codes, label=codes, env=tabular, sub}
346 \begin{subobject}{caption=Category codes, label=catcodes}{cl}
347 \toprule
348 Catcode & Category \\
349 \midrule
350 0 & Escape Character \\
351 1 & Begin Group \\
352 2 & End Group \\
353 \vdots & \quad \vdots \\
354 \bottomrule
355 \end{subobject}
356 \begin{subobject}{caption=Character codes, label=charcodes}{cr<{\hspace{1.3em}}}
357 \toprule
358 Character & \multicolumn1c{Charcode} \\
359 \midrule
360 \verb|\| & \number`\\ \\
361 \verb|{| & \number`\{ \\
362 \verb|}| & \number`\} \\
363 \vdots & \vdots \phantom{0} \\
364 \bottomrule
365 \end{subobject}
366 \end{tableobject}
367 \end{examplecode\starred}
368
369 A separator for the subobjects could be defined globally using `sep`, see also `hor` and `ver`.
370
371 For including a graphic from an external file this package defines a wrapper command around `subobject` and `\includegraphics` in order to reduce the typing effort:
372 \begin{examplecode\starred}{}
373 \begin{figureobject}{caption=Two lions, label=lions, sub}
374 \includegraphicsubobject[caption=A lion]{lion-1}
375 \includegraphicsubobject[caption=Another lion]{lion-2}
376 \end{figureobject}
377 \end{examplecode\starred}