1 | % framed.sty v 0.8a 21-Jul-2003 |
---|
2 | % Copyright (C) 1992-2003 by Donald Arseneau |
---|
3 | % These macros may be freely transmitted, reproduced, or modified |
---|
4 | % provided that this notice is left intact. |
---|
5 | % |
---|
6 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
7 | % Create framed or shaded regions that can break across pages using |
---|
8 | % \begin{framed} ... \end{framed} -- ordinary frame box (box at margin) |
---|
9 | % \begin{shaded} ... \end{shaded} -- shaded background (into margin) |
---|
10 | % ... leftbar ... -- line on left side |
---|
11 | % \begin{MakeFramed}{settings} ... \end{MakeFramed} |
---|
12 | % -- generic frame (for new environments) |
---|
13 | % |
---|
14 | % The "framed" environment puts the text into "\fbox" with the |
---|
15 | % settings "\fboxrule=\FrameRule" and "\fboxsep=\FrameSep". |
---|
16 | % You can change these lengths (using "\setlength") and you |
---|
17 | % can even change the definition of "\FrameCommand" to use |
---|
18 | % much fancier boxes. |
---|
19 | % |
---|
20 | % In fact, the "shaded" environment just redefines "\FrameCommand" |
---|
21 | % to use "\colorbox{shadecolor}" (and you have to define the |
---|
22 | % color "shadecolor": \newcolor{shadecolor}...). |
---|
23 | % |
---|
24 | % A page break is allowed, and even encouraged, before the framed |
---|
25 | % environment. If you want to attach some text (a box title) to the |
---|
26 | % frame, then the text should be inserted by \FrameCommand |
---|
27 | % |
---|
28 | % The contents of the framed regions are restricted: |
---|
29 | % Floats, footnotes, marginpars and head-line entries will be lost. |
---|
30 | % (Some of these may be handled in a later version.) |
---|
31 | % This package will not work with the page breaking of multicol.sty, |
---|
32 | % or other systems that perform column-balancing. |
---|
33 | % |
---|
34 | % The MakeFramed environment does the work. Its "settings" argument |
---|
35 | % should contain any adjustments to the text width (applied to \hsize, |
---|
36 | % and using the "\width" of the frame itself) as well as a `restore' |
---|
37 | % command -- \@parboxrestore or \FrameRestore or something similar. |
---|
38 | % |
---|
39 | % Expert commands: |
---|
40 | % \MakeFramed, \endMakeFramed: the "MakeFramed" environment |
---|
41 | % \FrameCommand: command to draw the frame around its argument |
---|
42 | % \FrameRestore: restore some text settings, but fewer than \@parboxrestore |
---|
43 | % \FrameRule: length register; \fboxrule for default "framed". |
---|
44 | % \FrameSep: length register; \fboxsep for default "framed". |
---|
45 | % \FrameHeightAdjust: macro; height of frame above baseline at top of page |
---|
46 | % |
---|
47 | % This is still a `pre-production' version because I can think of many |
---|
48 | % features/improvements that should be made. Nevertheless, starting |
---|
49 | % with version 0.5 it should be bug-free. |
---|
50 | % |
---|
51 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
52 | |
---|
53 | \ProvidesPackage{framed}[2003/07/21 v 0.8a: |
---|
54 | framed or shaded text with page breaks] |
---|
55 | |
---|
56 | \newenvironment{framed}% using default \FrameCommand |
---|
57 | {\MakeFramed {\advance\hsize-\width \FrameRestore}}% |
---|
58 | {\endMakeFramed} |
---|
59 | |
---|
60 | \newenvironment{shaded}{% |
---|
61 | \def\FrameCommand{\colorbox{shadecolor}}% |
---|
62 | \MakeFramed {\FrameRestore}}% |
---|
63 | {\endMakeFramed} |
---|
64 | |
---|
65 | \newenvironment{leftbar}{% |
---|
66 | \def\FrameCommand{\vrule width 3pt \hspace{10pt}}% |
---|
67 | \MakeFramed {\advance\hsize-\width \FrameRestore}}% |
---|
68 | {\endMakeFramed} |
---|
69 | |
---|
70 | \chardef\FrameRestore=\catcode`\| % for debug |
---|
71 | \catcode`\|=\catcode`\% % (debug: insert space after backslash) |
---|
72 | |
---|
73 | \def\MakeFramed#1{\par |
---|
74 | % measure added width and height; call result \width and \height |
---|
75 | \setbox\z@\vbox{\vskip-1in \hbox{\hskip-1in |
---|
76 | \FrameCommand{\hbox{\vrule \@height .7in \@depth.3in \@width 1in}}}% |
---|
77 | \vskip\z@skip}% |
---|
78 | \def\width{\wd\z@}\def\height{\ht\z@}% |
---|
79 | \edef\fb@frw{\the\width}\edef\fb@frh{\the\height}% |
---|
80 | % insert pre-penalties and skips |
---|
81 | \begingroup |
---|
82 | \skip@\lastskip |
---|
83 | \if@nobreak\else |
---|
84 | \penalty9999 % updates \page parameters |
---|
85 | \ifdim\pagefilstretch=\z@ \ifdim\pagefillstretch=\z@ |
---|
86 | \edef\@tempa{\the\skip@}% |
---|
87 | \ifx\@tempa\zero@glue \penalty-30 |
---|
88 | \else \vskip-\skip@ \penalty-30 \vskip\skip@ |
---|
89 | \fi\fi\fi |
---|
90 | \penalty\z@ |
---|
91 | % Give a stretchy breakpoint that will always be taken in preference |
---|
92 | % to the \penalty 9999 used to update page parameters. The cube root |
---|
93 | % of 10000/100 indicates a multiplier of 0.21545, but the maximum |
---|
94 | % calculated badness is really 8192, not 10000, so the multiplier |
---|
95 | % is 0.2301. |
---|
96 | \advance\skip@ \z@ plus-.5\baselineskip |
---|
97 | \advance\skip@ \z@ plus-.231\height |
---|
98 | \advance\skip@ \z@ plus-.231\skip@ |
---|
99 | \advance\skip@ \z@ plus-.231\topsep |
---|
100 | \vskip-\skip@ \penalty 1800 \vskip\skip@ |
---|
101 | \fi |
---|
102 | \addvspace{\topsep}% |
---|
103 | \endgroup |
---|
104 | % clear out pending page break |
---|
105 | \penalty\@M \vskip 2\baselineskip \vskip\height |
---|
106 | \penalty9999 \vskip -2\baselineskip \vskip-\height |
---|
107 | \penalty9999 % updates \pagetotal |
---|
108 | |\message{After clearout, \pagetotal=\the\pagetotal, \pagegoal=\the\pagegoal. }% |
---|
109 | \fb@adjheight |
---|
110 | \setbox\@tempboxa\vbox\bgroup |
---|
111 | #1% Modifications to \hsize (can use \width and \height) |
---|
112 | \textwidth\hsize \columnwidth\hsize |
---|
113 | } |
---|
114 | |
---|
115 | \def\endMakeFramed{\par |
---|
116 | \kern\z@ \penalty-100 % put depth into height |
---|
117 | \egroup |
---|
118 | \begingroup \put@frame \endgroup |
---|
119 | } |
---|
120 | |
---|
121 | % \put@frame takes the contents of \@tempboxa and puts all, or a piece, |
---|
122 | % of it on the page with a frame (\FrameCommand). It recurses until |
---|
123 | % all of \@tempboxa has been used up. (\@tempboxa must have zero depth.) |
---|
124 | |
---|
125 | \def\put@frame{\relax |
---|
126 | \ifdim\pagegoal=\maxdimen \pagegoal\vsize \fi |
---|
127 | | \message{=============== Entering putframe ====================^^J |
---|
128 | | \pagegoal=\the\pagegoal, \pagetotal=\the\pagetotal. }% |
---|
129 | \ifinner \else |
---|
130 | \dimen@\pagegoal \advance\dimen@-\pagetotal % natural space left on page |
---|
131 | \ifdim\dimen@<2\baselineskip |
---|
132 | | \message{Page has only \the\dimen@\space room left; eject. }% |
---|
133 | \eject \fb@adjheight \put@frame |
---|
134 | \else % there's appreciable room left on the page |
---|
135 | | \message{\string\pagetotal=\the\pagetotal, |
---|
136 | | \string\pagegoal=\the\pagegoal, |
---|
137 | | \string\pagestretch=\the\pagestretch, |
---|
138 | | \string\pageshrink=\the\pageshrink, |
---|
139 | | \string\fb@frh=\fb@frh. \space} |
---|
140 | | \message{Box of size \the\ht\@tempboxa\space + \fb@frh}% |
---|
141 | \begingroup % temporarily set \dimen@ to be... |
---|
142 | \advance\dimen@.8\pageshrink % maximum space available on page |
---|
143 | \advance\dimen@-\fb@frh\relax % space available for frame's contents |
---|
144 | \expandafter\endgroup |
---|
145 | % restore \dimen@ to real room left on page |
---|
146 | \ifdim\dimen@>\ht\@tempboxa % whole box does fit |
---|
147 | | \message{fits in \the\dimen@. }% |
---|
148 | \else % box must be split |
---|
149 | | \message{must be split to fit in \the\dimen@. }% |
---|
150 | \setbox\@tempboxa\vbox{% simulate frame and flexiblity of the page: |
---|
151 | \vskip \fb@frh \@plus\pagestretch \@minus.8\pageshrink |
---|
152 | \kern137sp\kern-137sp\penalty-30 |
---|
153 | \unvbox\@tempboxa}% |
---|
154 | \edef\fb@resto@set{\boxmaxdepth\the\boxmaxdepth \splittopskip\the\splittopskip}% |
---|
155 | \boxmaxdepth\z@ \splittopskip\z@ |
---|
156 | \setbox\tw@\vsplit\@tempboxa to\dimen@ |
---|
157 | \setbox\tw@\vbox{\unvbox\tw@}% natural-sized |
---|
158 | | \message{Box of size \the\ht\@tempboxa\space split to \the\dimen@. |
---|
159 | | Natural height of split box is \the\ht\tw@. }% |
---|
160 | % If the split-to size > (\vsize-\topskip), then set box to full size |
---|
161 | \begingroup |
---|
162 | \advance\dimen@\topskip |
---|
163 | \expandafter\endgroup |
---|
164 | \ifdim\dimen@>\pagegoal |
---|
165 | | \message{Frame is big -- Use up the full column. }% |
---|
166 | \dimen@ii\pagegoal |
---|
167 | \advance\dimen@ii -\topskip |
---|
168 | \advance\dimen@ii \FrameHeightAdjust\relax |
---|
169 | \else % suspect this is wrong: |
---|
170 | % If the split-to size > feasible room_on_page, rebox it smaller. |
---|
171 | \advance\dimen@.8\pageshrink |
---|
172 | \ifdim\ht\tw@>\dimen@ |
---|
173 | | \message{Box too tall; rebox it to \the\dimen@. }% |
---|
174 | \dimen@ii\dimen@ |
---|
175 | \else % use natural size |
---|
176 | \dimen@ii\ht\tw@ |
---|
177 | \fi |
---|
178 | \fi |
---|
179 | % Re-box contents to desired size \dimen@ii |
---|
180 | \advance\dimen@ii -\fb@frh |
---|
181 | \setbox\tw@\vbox to\dimen@ii \bgroup |
---|
182 | % remove simulated frame and page flexibility: |
---|
183 | \vskip -\fb@frh \@plus-\pagestretch \@minus-.8\pageshrink |
---|
184 | \unvbox\tw@ \unpenalty\unpenalty |
---|
185 | \ifdim\lastkern=-137sp % whole box went to next page |
---|
186 | | \message{box split at beginning! }% |
---|
187 | \egroup \fb@resto@set \eject % (\vskip for frame size was discarded) |
---|
188 | \fb@adjheight |
---|
189 | \else % |
---|
190 | \egroup \fb@resto@set |
---|
191 | \ifvoid\@tempboxa % it all fit after all |
---|
192 | | \message{box split at end! }% |
---|
193 | \setbox\@tempboxa\box\tw@ |
---|
194 | \else % it really did split |
---|
195 | | \message{box split as expected. Its reboxed height is \the\ht\tw@. }% |
---|
196 | \ifdim\wd\tw@>\z@ |
---|
197 | \centerline{\FrameCommand{\box\tw@}}% ??? \centerline bad idea |
---|
198 | \else |
---|
199 | | \message{Zero width means likely blank. Don't frame it (guess)}% |
---|
200 | \box\tw@ |
---|
201 | \fi |
---|
202 | \hrule \@height\z@ |
---|
203 | \eject |
---|
204 | \fb@adjheight |
---|
205 | \put@frame |
---|
206 | \fi\fi\fi\fi\fi |
---|
207 | \ifvoid\@tempboxa\else |
---|
208 | \centerline{\FrameCommand{\box\@tempboxa}}% |
---|
209 | \nointerlineskip \null %{\showoutput \showlists} |
---|
210 | \penalty-30 \vskip\topsep |
---|
211 | \fi} |
---|
212 | |
---|
213 | \def\fb@adjheight{% |
---|
214 | \vbox to\FrameHeightAdjust{}% get proper baseline skip from above. |
---|
215 | \penalty\@M \nointerlineskip |
---|
216 | \vskip-\FrameHeightAdjust |
---|
217 | \penalty\@M} % useful for tops of pages |
---|
218 | |
---|
219 | \edef\zero@glue{\the\z@skip} |
---|
220 | |
---|
221 | \catcode`\|=\FrameRestore |
---|
222 | |
---|
223 | % Provide configuration commands: |
---|
224 | \providecommand\FrameCommand{\fboxrule=\FrameRule \fboxsep=\FrameSep \fbox} |
---|
225 | \@ifundefined{FrameRule}{\newdimen\FrameRule \FrameRule=\fboxrule}{} |
---|
226 | \@ifundefined{FrameSep} {\newdimen\FrameSep \FrameSep =3\fboxsep}{} |
---|
227 | |
---|
228 | % Height of frame above first baseline when frame starts a page: |
---|
229 | \providecommand\FrameHeightAdjust{6pt} |
---|
230 | |
---|
231 | % \FrameRestore has parts of \@parboxrestore. See how it is used in the |
---|
232 | % "settings" argument of \MakeFrame. Previous behavior can be restored by |
---|
233 | % using \@parboxrestore there, or redefining: |
---|
234 | % \makeatletter \renewcommand\FrameRestore{\@parboxrestore} \makeatother |
---|
235 | \def\FrameRestore{% |
---|
236 | \let\if@nobreak\iffalse |
---|
237 | \let\if@noskipsec\iffalse |
---|
238 | % \let\par\@@par ?? |
---|
239 | \let\-\@dischyph |
---|
240 | \let\'\@acci\let\`\@accii\let\=\@acciii |
---|
241 | % \parindent\z@ \parskip\z@skip Definitely omit! |
---|
242 | % \everypar{}% ?? |
---|
243 | \linewidth\hsize |
---|
244 | % \@totalleftmargin\z@ |
---|
245 | % \leftskip\z@skip \rightskip\z@skip \@rightskip\z@skip |
---|
246 | % \parfillskip\@flushglue \lineskip\normallineskip |
---|
247 | % \baselineskip\normalbaselineskip |
---|
248 | \sloppy |
---|
249 | % \let\\\@normalcr |
---|
250 | } |
---|
251 | |
---|
252 | % Compatibility with previous versions (temporary!): |
---|
253 | \let\fram@d=\MakeFramed \let\endfram@d=\endMakeFramed |
---|
254 | |
---|
255 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|