summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/colorframed/colorframed.sty
blob: 6b6de479779893e9bf2a6cdb30e2fda7c7e79678 (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
% colorframed.sty    v0.9b    2022/10/01
% Copyright (C) 2022 Jean-François Burnol
% Distributed under the terms of the LPPL 1.3c, see README.md
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{colorframed}
    [2022/10/01 v0.9b fix `framed' breakable boxes colour leaks (JFB)]
% Apart from code comments here, documentation is in colorframed-doc.pdf
\RequirePackage{framed}
\RequirePackage{color}
% First we must make \MakeFramed/\endMakeFramed colour safe.
\expandafter\def\expandafter\MakeFramed\expandafter#\expandafter1\expandafter
  {\MakeFramed{#1}\color@begingroup}
\expandafter\def\expandafter\endMakeFramed\expandafter
  {\expandafter\color@endgroup\endMakeFramed}
%
% `framed'
\def\FrameCommand{%
    \setlength\fboxrule{\FrameRule}\setlength\fboxsep{\FrameSep}%
    \CustomFBox{}{}\fboxrule\fboxrule\fboxrule\fboxrule % replaces \fbox
}
% The frame colour can be set via modifying this macro.
\def\colorframedbordercolorcommand{\normalcolor}
%
% `shaded', `shaded*', `snugshade', `snugshade*'
% The \colorbox in \FrameCommand is replaced by \colorframedcolorbox
\renewenvironment{shaded}{%
  \def\FrameCommand{\fboxsep=\FrameSep 
                    \colorframedcolorbox{shadecolor}}%<<<< MODIFIED
  \MakeFramed {\FrameRestore}}%
 {\endMakeFramed}
\renewenvironment{shaded*}{%
  \def\FrameCommand{\fboxsep=\FrameSep
                    \colorframedcolorbox{shadecolor}}%<<<< MODIFIED
  \MakeFramed {\advance\hsize-\width \FrameRestore}}%
 {\endMakeFramed}
\renewenvironment{snugshade}{%
  \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
  \colorframedcolorbox{shadecolor}{##1}%<<<< MODIFIED
      \hskip-\fboxsep
      % There is no \@totalrightmargin, so:
      \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
  \MakeFramed {\advance\hsize-\width 
    \@totalleftmargin\z@ \linewidth\hsize
    \@setminipage}%
 }{\par\unskip\@minipagefalse\endMakeFramed}
\renewenvironment{snugshade*}{%
  \def\FrameCommand##1{\hskip\@totalleftmargin 
  \colorframedcolorbox{shadecolor}{##1}%<<<< MODIFIED
      % There is no \@totalrightmargin, so:
      \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
  \MakeFramed {\advance\hsize-\width 
    \@totalleftmargin\z@ \linewidth\hsize
    \advance\labelsep\fboxsep
    \@setminipage}%
 }{\par\unskip\@minipagefalse\endMakeFramed}
% The \colorframedcolorbox was obtained via a process of trimming from some
% far more capable box, hence some possibly strange looking or sub-optimal
% code; but we did not want to keep extra capabilities and have to provide
% interface for that...
\def\colorframedcolorbox#1#2{%
  % #1 is in our usage always "shadecolor" (which has to be defined by user)
  % #2 is in our usage always a box or rule
  \leavevmode% this is not needed in our usage
  \hbox{%
    \setbox\@tempboxa\hbox{\kern\fboxsep{#2}\kern\fboxsep}%
    \ht\@tempboxa
       \dimexpr\ht\@tempboxa+\fboxsep\relax
    \dp\@tempboxa
       \dimexpr\dp\@tempboxa+\fboxsep\relax
    % BACKGROUND
    % draw background and move back to reference point
    {\color{#1}%
     \vrule\@height\ht\@tempboxa
           \@depth\dp\@tempboxa
           \@width\wd\@tempboxa
     \kern-\wd\@tempboxa
    }%
    % CONTENTS
    \box\@tempboxa
  }%
}
%
% `oframed' and `titled-frame'
% The \CustomFBox redefinition.
% Refer to framed.sty for significance of the parameters.
% We have not wrapped #3, #4, #5, #6 thicknesses in \dimexpr.
% Generally speaking, no attempt here to make this macro more
% versatile than is the original.
% Our version tries to let #1 and #2 have same effect as in original
% (where they are only used for titled-frame environment, and
%  it thus seems to be sort of a convention that #2 has no height.)
\long\def\CustomFBox#1#2#3#4#5#6#7{%
  \leavevmode\begingroup
  \setbox\@tempboxa\hbox{%
    \color@begingroup
      \kern\fboxsep{#7}\kern\fboxsep
    \color@endgroup}%
  \hbox{%
    \ht\@tempboxa
       \dimexpr\ht\@tempboxa+\fboxsep+#3\relax % cf "overlap" remark below
    \dp\@tempboxa
       \dimexpr\dp\@tempboxa+\fboxsep+#4\relax
        \setbox\z@\hbox
                  {\vrule\@width#5\relax
                   \kern\wd\@tempboxa
                   \vrule\@width#6\relax
                  }%
            \ht\z@\ht\@tempboxa
            \dp\z@\dp\@tempboxa
    \vbox{%
        \colorframedbordercolorcommand % defaults to \normalcolor
        \hrule\@height#3\relax
        #1%
        \kern-#3\relax % see discussion of issue #2
        \copy\z@ % deliberate overlap hrules+vrules to avoid anti-aliasing artefacts
        \kern-#4\relax
        #2%
        \hrule\@height#4\relax
        \kern-\dp\@tempboxa
       }%
    \kern-\wd\z@
    \kern#5\relax
    \box\@tempboxa
    \kern#6\relax
  }%
  \endgroup
}
% The titled-frame environment depends in part on this macro,
% whose original in framed.sty has various colour problems.
% We have indicated the modifications, additionally to using
% our modified \CustomFBox.
% Original let the continuation label use same colour as frame.
% Let make this customizable.
\let\colorframedTFconlabcolorcommand\empty
\renewcommand\TitleBarFrame[3][]{\begingroup
  \ifx\delimiter#1\delimiter
    \let\TF@conlab\@empty
  \else
    \def\TF@conlab{%
     \nointerlineskip
     \hbox{%<<<< ADDED (needed due to \smash doing \leavevmode nowadays)
           \colorframedTFconlabcolorcommand%<<<< ADDED
     \smash{\rlap{\kern\wd\@tempboxa\kern\fboxrule\kern\fboxsep #1}}}%
          }%<<<< ADDED
  \fi
  %\let\TF@savecolor\current@color%<<<< REMOVED
  %\textcolor{TFFrameColor}{%      <<<< REMOVED
  \def\colorframedbordercolorcommand{\color{TFFrameColor}}%<<<< ADDED
  \CustomFBox
    {\TF@Title{#2}}{\TF@conlab}%
    \fboxrule\fboxrule\fboxrule\fboxrule
    {%\let\current@color\TF@savecolor\set@color%<<<< REMOVED
    #3}%
  %}%                              <<<< REMOVED
  \endgroup
}
% There are various issues with \TitleBarFrame/\TF@Title/TF@conlab
% we provide an alternative
\def\colorframedTFconlabsep{3pt}
\newcommand\colorframedTitleBarFrame[3][]{\begingroup
  \ifx\delimiter#1\delimiter
    \let\TF@conlab\@empty
  \else
    \def\TF@conlab{%
     \nointerlineskip
     \hbox{\colorframedTFconlabcolorcommand
     \smash{\rlap{\kern\wd\@tempboxa
                  % 2\fboxrule, not \fboxrule\fboxsep!
                  \kern2\fboxrule
                  \kern\dimexpr\colorframedTFconlabsep\relax #1}}}%
          }%
  \fi
  \def\colorframedbordercolorcommand{\color{TFFrameColor}}%
  \CustomFBox
    {\colorframed@TF@Title{#2}}{\TF@conlab}%
    \fboxrule\fboxrule\fboxrule\fboxrule
    {#3}%
  \endgroup
}
% The title bar for \colorframedTitleBarFrame, it will wrap its argument
% as paragraphs in case it is too long, the horizontal placement is improved
% and it will be suitably vertically centered. Nota Bene: for #1 to be
% allowed to contain \par, the \textbf in titled-frame must be replaced by
% some \bfseries.
% Macro to customize the horizontal placement. For example
% \def\colorframedTFtitleshift{0pt}
% if \fboxrule is set by the environment to a high value.
% Don't use negative value, some parts of the title may then be hidden
% behind the left border.
\def\colorframedTFtitlesep{\fboxsep}
\newcommand\colorframed@TF@Title[1]{%
  \ifx\delimiter#1\delimiter\else
  %\kern-0.04pt\relax
  \kern-.5\fboxrule
  \begingroup
  \setbox\@tempboxa\vbox{%
   \kern0.8ex
   \hbox{\kern\fboxrule
         \kern\dimexpr\colorframedTFtitlesep\relax
         \vbox{\hsize\dimexpr\linewidth-2\fboxrule-
                             2\dimexpr\colorframedTFtitlesep\relax\relax
               \noindent\color{TFTitleColor}%
               #1\par}}%
   \kern0.8ex}%
  \hrule\@height\ht\@tempboxa
  \kern-\ht\@tempboxa
  \box\@tempboxa
  \endgroup
  %\nointerlineskip 
  %\kern-0.04pt\relax
  \hrule\@height.5\fboxrule
  \fi
}
% maybe provide a titled-frame variant using the above and acting like
% snugshade respective to being used in a list?
\endinput