forked from pkgw/tex-stuff
-
Notifications
You must be signed in to change notification settings - Fork 4
/
scgraphic.sty
165 lines (149 loc) · 5.44 KB
/
scgraphic.sty
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
% Copyright 2014-2015 Peter Williams
%
% 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
% Peter Williams. This work consists of the file scgraphic.sty.
% scgraphic.sty, inspired by sidecap.sty. Quick way to create a figure float
% containing an image and a caption placed to its right side.
%
% \scgraphic[frac=0.7,label=f.foo]{image-path}{Caption text.}
%
% is approximately equivalent to:
%
% \begin{figure*}
% \includegraphics{image-path}
% \caption{Caption text.}
% \label{f.foo}
% \end{figure*}
%
% except that special things are done to place the image and caption
% side-by-side, in this case giving the image 0.7 of the available width.
%
% The key parameter is 'frac', which determines how much of the horizontal
% space is allocated to the image. Usually you want to adjust it until the
% image and caption are about the same height.
%
% If the document is being set in a multi-column mode (specifically,
% \columnwidth is less than \textwidth), a regular 'figure' will be created
% with no special effects. This allows you to switch between one-column and
% two-column layouts relatively painlessly, assuming 'frac' is about 0.5.
%
% Keywords to \scgraphic are:
%
% altcap - Passed as the optional arg to \caption: \caption[altcap]{main cap...}
% frac - Fraction of the available width to be allocated to the image
% gutter - Size of the gutter between image and caption. Defaults to \marginparsep
% label - \label{} of the figure.
% pos - Postioning of the resulting float. Defaults to "tb".
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{scgraphic}[2014/11/03 v0.1 scgraphic (PKGW)]
\RequirePackage{keyval}[1997/11/10]
\RequirePackage{graphicx}[1999/02/16]
\RequirePackage[NewParameters]{ragged2e}
\newdimen\scg@gutter
\newdimen\scg@figwidth
\newdimen\scg@capwidth
\newdimen\scg@figheight
\def\scgDefaultAltCap{}
\def\scgDefaultPos{tb}
\def\scgDefaultFrac{0.5}
\def\scgDefaultGutter{\marginparsep}
\newcommand*{\scg@debuglen}[1]{} % noop; activated by 'debug' option
\newcommand{\scg@debugbox}[1]{#1} % passthrough; activated by 'debug' option
% Option processing
\DeclareOption{debug}{%
\renewcommand*{\scg@debuglen}[1]{%
\PackageInfo{scgraphic}{\string#1 \the#1}%
}%
\renewcommand{\scg@debugbox}[1]{%
{\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{1pt}%
\fbox{#1}%
}%
}%
}%
\ExecuteOptions{}
\ProcessOptions\relax
% The rest
\def\scgraphic{\@ifnextchar[\scgraphic@@a{\scgraphic@@a[]}}
\def\scgraphic@@a[#1]#2#3{%
% #1 - keyword args
% #2 - image path
% #3 - caption text
%
% Default the keywords
\edef\scg@kw@altcap{\scgDefaultAltCap}%
\edef\scg@kw@frac{\scgDefaultFrac}%
\setlength{\scg@gutter}{\scgDefaultGutter}%
\let\scg@kw@label=\@undefined%
\edef\scg@kw@pos{\scgDefaultPos}%
%
% Process keywords
\setkeys{scg}{#1}%
\scg@debuglen{\scg@gutter}%
%
\ifdim\columnwidth<\textwidth
% We're in multi-column mode! Just make a regular figure, ignoring most
% of our features.
\begin{figure}[htb]%
\includegraphics[width=\columnwidth]{#2}%
\caption[\scg@kw@altcap]{#3}%
\ifx\scg@kw@label\@undefined\else
\label{\scg@kw@label}%
\fi
\end{figure}%
\else
% We need to do a fancy sidecap. \@xdblfloat needs to see the value of
% \scg@kw@pos after expansion; I'm sure there's a non-ridiculous way to do
% this, but this is all I can manage right now.
\def\@tempa{{figure}[}%
\def\@tempb{]}%
\edef\@tempc{\noexpand\@xdblfloat\@tempa\scg@kw@pos\@tempb}%
\@tempc%
% Calculate the widths of the image and the caption
\setlength{\scg@capwidth}{\textwidth}% temporarily: full item width
\advance\scg@capwidth -\scg@gutter \relax% temporarily: full width minus gutter
\edef\@tempa{\scg@kw@frac\noexpand\scg@capwidth}%
\setlength{\scg@figwidth}\@tempa\relax%
\advance\scg@capwidth -\scg@figwidth% final value
\scg@debuglen{\textwidth}%
\scg@debuglen{\scg@figwidth}%
\scg@debuglen{\scg@capwidth}%
%
\makebox[\textwidth][c]{%
\settoheight{\scg@figheight}{%
\includegraphics[width=\scg@figwidth]{#2}%
}%
\scg@debuglen{\scg@figheight}%
\scg@debugbox{\includegraphics[width=\scg@figwidth]{#2}}%
\hspace{\scg@gutter}%
\scg@debugbox{\minipage[b][\scg@figheight][c]{\scg@capwidth}%
\hrule \@height\z@ \@depth\z@%
\let\@arrayparboxrestore\relax%
\let\usecaptionmargin\relax%
\abovecaptionskip\z@skip%
\belowcaptionskip\z@skip%
\caption[\scg@kw@altcap]{%
\RaggedRight%
\strut\ignorespaces#3\unskip\strut%
}%
\unskip%
\ifx\scg@kw@label\@undefined\else%
\label{\scg@kw@label}%
\fi%
\hrule \@height\z@ \@depth\z@%
\endminipage}%
}%
\end@dblfloat
\fi
}
\define@key{scg}{altcap}{\def\scg@kw@altcap{#1}}
\define@key{scg}{frac}{\def\scg@kw@frac{#1}}
\define@key{scg}{gutter}{\setlength{\scg@gutter}{#1}}
\define@key{scg}{label}{\def\scg@kw@label{#1}}
\define@key{scg}{pos}{\def\scg@kw@pos{#1}}
\endinput