summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/colorframed/colorframed.sty
blob: 677cdef8a296c275f93236a65eabb4c720842ac1 (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
% colorframed.sty    v0.9a    2022/09/23
% Copyright (c) 2022 Jean-François Burnol
% Distributed under the terms of the LPPL 1.3c, see README.md
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{colorframed}
    [2022/09/23 v0.9a fix `framed' breakable boxes colour leaks (JFB)]
% This is actually same as 0.9 release of 2022/09/22 apart from these
% comments:
%
% The analysis and original workarounds for using framed.sty
% with colours were developed by me in some contributions I made
% to the Sphinx project (\url{https://github.com/sphinx-doc/sphinx})
% and I am transferring here the general idea.
%
% The key thing is that the boxes handled by framed.sty may
% contain isolated colour push or colour pop.  We must make
% sure an isolated colour push, if followed by colour changes,
% is always followed by paired ones, and never by a colour pop
% from a colour command originated "prior".
%
\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
        \kern-#3\relax
        #1%
        \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
}
\endinput