summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/easyfig/easyfig.dtx
blob: 7581db0de4a480cb5282f1e68f927b91d72e2bbe (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
% \iffalse meta-comment
%<=*COPYRIGHT>
%% Copyright (C) 2011-2018 by Martin Scharrer <martin@scharrer-online.de>
%% ----------------------------------------------------------------------
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is Martin Scharrer.
%%
%% This work consists of the files easyfig.dtx and easyfig.ins
%% and the derived filebase easyfig.sty.
%%
%<=/COPYRIGHT>
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{easyfig.dtx}[%
%<=*DATE>
    2018/03/30
%<=/DATE>
%<=*VERSION>
    v1.2a
%<=/VERSION>
    Easily include images as figures]
\documentclass{ydoc}
\GetFileInfo{easyfig.dtx}
\usepackage{easyfig}[\filedate]
\usepackage{flafter}
%\usepackage{mwe}
\graphicspath{{example-}}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\OnlyDescription
\optionaloff
\def\textfraction{0.2}
\def\bottomfraction{0.8}
\begin{document}
  \DocInput{\jobname.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{207}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v1.0}{2011/10/31}{Initial release.}
% \changes{v1.1}{2012/05/06}{Fix of "placement" key and non-e-TeX fallback code.}
% \changes{v1.2}{2012/05/15}{Added "here" key.}
% \changes{v1.2a}{2018/03/30}{Updated manual to mentioned \pkg{adjustbox} as alternative.}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \GetFileInfo{easyfig.dtx}
% \author{Martin Scharrer}
% \email{martin@scharrer-online.de}
% \ifdefined\repository
%   \repository{https://bitbucket.org/martin_scharrer/easyfig}
% \fi
%
% \maketitle
%
% \begin{abstract}\noindent
% This package aims to simplify the most common usage of \env{figure} environments
% to include a single image with a caption and label.
%
% Current versions of the \pkg{adjustbox} package now provide keys for turning an image or other content into a
% float or non-float inclusive a caption. This makes this package quite redundant.
% \end{abstract}
%
% \section{Introduction}
% A very high percentage of all \LaTeX\ figures aim to simply display a simple image with a caption and label.
% A common way to code this is:
% \begin{lstlisting}[gobble=4,numbers=none,language={[latex]tex}]
%   \begin{figure}
%      \centering
%      \includegraphics[<options>]{<filename>}
%      \caption{<caption text>}
%      \label{<label>}
%   \end{figure}
% \end{lstlisting}
%
% This package provides an easy and short way to include figures like this. At the moment only single image figures
% are supported but it is planned to also support common 1x2 and 2x1 figures in future releases.
%
% \iffalse
% \section{Dependecies}
% This package uses the following packages: \pkg{xkeyval}, \pkg{adjustbox} (which uses \pkg{graphicx}) and \pkg{ifetex}.
% \fi
%
% \section{Usage}
% \subsection*{Macros}
% \DescribeMacro\Figure[<key=value pairs>]{<image filename>}
% \DescribeMacro\Figure*[<key=value pairs>]{<image filename>}
% The macro creates a \env{figure} environment where the given image file is included using \Macro\includegraphics wrapped in an \env{adjustbox} environment.
% By default the image is centered and all keys which are not known by \Macro\Figure are passed to the internal \env{adjustbox} environment. This accepts all normal
% \Macro\includegraphics keys as well as many more. The starred version uses the \env{figure*} environment instead which will insert the figure over both columns in \opt{twocolumn} mode.
%
% \noindent
% Usually this macro is used as follows:\nopagebreak
% \begingroup
% \leftmargin=0pt
% \rightmargin=0pt
% \begin{macroquote}
%   \Figure['caption='{My caption},label=fig:mypic]{myimage}\\
%   \Figure['caption='{My caption}]{myimage}\\
%   \Figure['caption='{My caption},label=fig:mypic,placement=ht]{myimage}
% \end{macroquote}
% \endgroup
%
% \DescribeMacro\easyfigdefault{<key=value pairs (before)>}
% \DescribeMacro\easyfigdefault*{<key=value pairs (after)>}
% Defines a list of key/value pairs which should be used by default. The normal version adds the pairs before the one
% given with the optional argument of \Macro\Figure, while the starred version adds them after these.
% If these macros are used more then once the old default settings are overwritten.
% This feature is similar to \Macro\adjustboxset from the used \pkg{adjustbox} package, but the keys are only affecting \Macro\Figure macros and do not stack.
% By default the \emph{before} key list is empty and the \emph{after} list is set to \Key{center} which centers the image around the \Macro\linewidth, after any other
% transformation is applied. The default centering can be deleted using \Macro\easyfigdefault*{}.
%
%
% \subsection*{Keys}
% The following keys exist to control the elements of the figure. All other keys are passed to the inner \env{adjustbox} environment from the \pkg{adjustbox} package.
%
% \DescribeKey{caption}'='<caption>
% Sets the \Macro\caption of the figure.
%
% \DescribeKey{captionabove}'='<caption>
% Sets the \Macro\caption of the figure which is placed above the figure instead of the default placement below it.
%
% \DescribeKey{shortcaption}'='<short caption>
% Sets the short version of the caption, i.e. the optional argument of \Macro\caption, used for the table-of-figures.
% This key only has an effect if either \Key{caption} or \Key{captionabove} is used as well. However, the order of keys is not meaningful.
%
% \DescribeKey{label}'='<label>
% Sets the \Macro\label of the figure. The order of this key and \Key{caption} or \Key{captionabove} is not meaningful.
% If the figure has a caption but this key is not used, a default label `\MacroArgs'fig:'<image filename>\relax' is used instead.
%
% \DescribeKey{placement}'='<placement characters>
% This key defines the requested placement of the figure. The value is then passed to the optional argument of \env{figure} or \env{figure*}.
% Valid positioning characters are `|p|' (page), `|t|' (top), `|b|' (bottom), `|h|' (here) and `|!|' (force) and any additional characters defined by packages (like `|H|').
%
% \DescribeKey{here}
% Using this key the figure is placed at the current position and not using a \env{figure} float. Any \Key{placement}
% key will be ignored. Note that this key is overwritten by any following \Key{twocolumn} key.
%
% \DescribeKey{twocolumn}
% \DescribeKey{twocolumn}'=true|false'
% This boolean key will use the \env{figure*} environment which make the figure span over both columns in a \opt{twocolumn} document. It has the same effect as using \Macro\Figure*, but allows
% to switch between this two modes for all figures using \Macro\easyfigdefault. When used without a value `|true|' is assumed.
% Any other value as `|true|' switches to one-column mode, even if \Macro\Figure* was used.
%
% \subsection*{Notable \pkg{adjustbox} Keys}
% The following keys are mentioned here because they are might be often used for images inside figures. Please see the \pkg{adjustbox} manual for further details and keys.
% In addition all \Macro\includegraphics keys as mentioned in the \href{http://www.ctan.org/pkg/grfguide}{grfguide} can of course be used.
%
% \DescribeKey{center}'='<length>
% \DescribeKey{left}'='<length>
% \DescribeKey{right}'='<length>
% Aligns the image at the center or to the left or right. The \emph{optional} length defines the final width in with the image is aligned. By default the current \Macro\linewidth is used, which
% inside figures is normally identical to the \Macro\textwidth. If the image is wider than the given width it will overlap accordantly, i.e. on both sites, on the right or on the left, respectively.
% As mentioned above by default \Key{center} is applied to all figures. This replaces the normally used \Macro\centering.
%
% \DescribeKey{width}'='<length>
% \DescribeKey{height}'='<length>
% \DescribeKey{totalheight}'='<length>
% \DescribeKey{min width}'='<length>
% \DescribeKey{min height}'='<length>
% \DescribeKey{min size}'='{<length>}{<length>}
% \DescribeKey{max width}'='<length>
% \DescribeKey{max height}'='<length>
% \DescribeKey{max size}'='{<length>}{<length>}
% Can be used to set the nominal/minimal/maximum width and height of the image.
%
% \DescribeKey{frame}
% \DescribeKey{frame}'='<length>
% Adds a tight frame around the image.
%
% \DescribeKey{fbox}
% \DescribeKey{fbox}'='<length>
% \DescribeKey{fbox}'='<length>~<sep>
% Adds a frame around the image.
%
% \DescribeKey{margin}'='<all sites>
% \DescribeKey{margin}'='<left/right>~<top/bottom>
% \DescribeKey{margin}'='<llx>~<lly>~<urx>~<ury>
% This key can be used to add a margin (white space) around the image.
%
%
% \clearpage
% \section{Examples}
%
% \subsection{Simple figure with an image and caption, automated label}
% A default label `|fig:image|' is used here.
% \par\smallskip\noindent
% \Macro\Figure['width=10cm,caption={An image}']{image}
% \Figure[width=10cm,caption={An image}]{image}
%
% \clearpage
% \subsection{Simple figure with an image and caption, manual label}
% Normal usage with a manual label.
% \par\smallskip\noindent
% \Macro\Figure['width=10cm,caption={Second image},label=fig:myimage']{image}
% \Figure[width=10cm,caption={Second image},label=fig:myimage]{image}
%
% \clearpage
% \subsection{Usage of other keys}
% Adds a frame around the image which is rotated a little.
% The placement is fixed to top or bottom.
% \par\smallskip\noindent
% \Macro\Figure['width=10cm,angle=10,position=tb,caption={Another image},frame']{image}
% \Figure[width=10cm,angle=10,placement=tb,caption={Another image},frame]{image}
%
% \StopEventually{}
% \clearpage
% \section{Implementation}
%
% \iffalse
%<*easyfig.sty>
% \fi
%    \begin{macrocode}
%<!COPYRIGHT>
\ProvidesPackage{easyfig}[%
%<!DATE>
%<!VERSION>
%<*DRIVER>
    2099/01/01 develop
%</DRIVER>
    Easy way to include figures]
%    \end{macrocode}
%
%    \begin{macrocode}
\RequirePackage{adjustbox}
\RequirePackage{xkeyval}
\RequirePackage{ifetex}
%    \end{macrocode}
%
%    \begin{macrocode}
\let\easyfig@caption\@gobble
\let\easyfig@captionabove\@gobble
\let\easyfig@placement\@empty
\let\easyfig@default\@empty
\let\easyfig@default@end\@empty
\let\easyfig@shortcaption\@empty
\def\easyfig@env{{figure}}
%    \end{macrocode}
%
% \begin{macro}{\easyfigdefault}
%    \begin{macrocode}
\newcommand*{\easyfigdefault}{%
    \@ifstar
        {\def\easyfig@default@end}%
        {\def\easyfig@default}%
}
\easyfigdefault*{center}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\easyfig@label}
%    \begin{macrocode}
\def\easyfig@label#1{%
    \@ifundefined {easyfig@r@fig:#1}{%
        \label{fig:#1}%
        \global\@namedef{easyfig@r@fig:#1}{}%
    }{}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{key}{easyfig}{caption}
%    \begin{macrocode}
\define@key{easyfig}{caption}{%
    \def\easyfig@caption##1{\expandafter\caption\easyfig@shortcaption{#1}\easyfig@label{##1}}%
    \let\easyfig@captionabove\@gobble
}
%    \end{macrocode}
% \end{key}
%
%
% \begin{key}{easyfig}{shortcaption}
%    \begin{macrocode}
\define@key{easyfig}{shortcaption}{%
    \def\easyfig@shortcaption{[{#1}]}%
}
%    \end{macrocode}
% \end{key}
%
%
% \begin{key}{easyfig}{captionabove}
%    \begin{macrocode}
\define@key{easyfig}{captionabove}{%
    \let\easyfig@caption\@gobble
    \def\easyfig@captionabove##1{{%
        \@tempdima=\abovecaptionskip
        \abovecaptionskip=\belowcaptionskip
        \belowcaptionskip=\@tempdima
        \expandafter\caption\easyfig@shortcaption{#1}%
        \easyfig@label{##1}%
    }}%
}
%    \end{macrocode}
% \end{key}
%
%
% \begin{key}{easyfig}{label}
%    \begin{macrocode}
\define@key{easyfig}{label}{%
    \def\easyfig@label##1{\label{#1}}%
}
%    \end{macrocode}
% \end{key}
%
%
% \begin{key}{easyfig}{placement}
%    \begin{macrocode}
\define@key{easyfig}{placement}{%
    \def\easyfig@placement{[#1]}%
}
%    \end{macrocode}
% \end{key}
%
% \begin{key}{easyfig}{here}
%    \begin{macrocode}
\define@key{easyfig}{here}[]{%
    \def\easyfig@env{{easyfighere}}%
}
%    \end{macrocode}
% \end{key}
%
%
% \begin{environment}{easyfighere}
%    \begin{macrocode}
\newenvironment{easyfighere}{%
    \trivlist
    \def\@captype{figure}%
    \item\relax
}{%
    \endtrivlist
}
%    \end{macrocode}
% \end{environment}
%
%
% \begin{key}{easyfig}{twocolumn}
%    \begin{macrocode}
\define@key{easyfig}{twocolumn}[true]{%
    \begingroup
    \def\@tempa{#1}%
    \def\@tempb{true}%
    \ifx\@tempa\@tempb
        \endgroup
        \def\easyfig@env{{figure*}}%
    \else
        \endgroup
        \def\easyfig@env{{figure}}%
    \fi
}
%    \end{macrocode}
% \end{key}
%
% \begin{macro}{\Figure}
%    \begin{macrocode}
\newcommand{\Figure}{%
    \begingroup
    \@ifstar
        {\def\easyfig@env{{figure*}}\@Figure}%
        {\@Figure}%
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\ifetex
%    \end{macrocode}
%
% Efficient $\epsilon$-\TeX\ definition:
%
% \begin{macro}{\easyfig@expandonce}
%    \begin{macrocode}
\def\easyfig@expandonce#1{%
    \expandafter\unexpanded\expandafter{#1}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@Figure}
%    \begin{macrocode}
\newcommand{\@Figure}[2][]{%
    \edef\@tempa{%
        \noexpand\setkeys*{easyfig}{%
            \easyfig@expandonce\easyfig@default,%
            \unexpanded{#1},%
            \easyfig@expandonce\easyfig@default@end
        }%
    }%
    \@tempa
    \edef\@tempa{%
        \noexpand\begin\easyfig@env%
        \easyfig@placement
    }%
    \@tempa
    \easyfig@captionabove{#2}%
    \edef\@tempa{\noexpand\begin{adjustbox}{\easyfig@expandonce\XKV@rm}}%
    \@tempa
        \includegraphics{#2}%
    \end{adjustbox}%
    \easyfig@caption{#2}%
    \expandafter\end\easyfig@env%
    \endgroup
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\else
%    \end{macrocode}
%
% Fall-back definition for distributions which do not support $\epsilon$-\TeX:
%
% \begin{macro}{\@Figure}
%    \begin{macrocode}
\newcommand{\@Figure}[2][]{%
    \def\@tempa{\setkeys*{easyfig}}%
    \expandafter\toks@\expandafter{\easyfig@default,#1}%
    \expandafter\expandafter\expandafter\toks@
    \expandafter\expandafter\expandafter{\expandafter\the\expandafter\toks@\expandafter,\easyfig@default@end}%
    \expandafter\@tempa\expandafter{\the\toks@}
    \edef\@tempa{\noexpand\begin\easyfig@env}%
    \expandafter\@tempa\easyfig@placement
    \easyfig@captionabove{#2}%
    \def\@tempa{\begin{adjustbox}}%
    \expandafter\def\expandafter\@tempb\expandafter{\expandafter{\XKV@rm}}%
    \expandafter\@tempa\@tempb
        \includegraphics{#2}%
    \end{adjustbox}%
    \easyfig@caption{#2}%
    \expandafter\end\easyfig@env%
    \endgroup
}
%    \end{macrocode}
% \end{macro}
%
%
%    \begin{macrocode}
\fi
%    \end{macrocode}
% \iffalse
%</easyfig.sty>
% \fi
%
% \Finale
\endinput