summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/tkz-doc/tkzexample.sty
blob: 8dde2148bde6292fb55a164c991313257c427910 (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
% Copyright 2009 by Alain Matthes
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% The author of the original code 
% of the environment 'codeexample' is Till Tantau
% from the file pgfmanual-en-macros.tex
%  (Copyright 2006 by Till Tantau)
% See the file doc/generic/pgf/licenses/LICENSE for more details.
% 
% Diff between tkexample and codeexample
% I removed :
% graphicbackground 
% I replaced  \xdef\code@temp{#1}% by \protected@xdef\code@temp{#1}}%
% now the use of accented letters with utf8 is possible.
% I changed  \def\code@mid{\hskip6pt}
%  \def\code@width{\linewidth-12pt\tkzexamplewidth}%
% into
%      \def\code@mid{\hskip6pt\hskip2em}%
%      \def\code@width{\linewidth-12pt-2em-\tkzexamplewidth}
% this is  to add room for the numbers
% I insert the next code to add numders in front of each line of code
% \ifnum@show%
%   \noindent%
%   \llap{\colorbox{green!25!black}{%
%         \textcolor{white}{%
%         \number\value{code@cnt}}}\enspace}%
%   \stepcounter{code@cnt}%
% \fi
% I changed some keys :
% I added
%	show num/.is if=num@show,
%	num/.code= {\num@showtrue\setcounter{code@cnt}{1}},
%	global	num/.code= {\num@showtrue},
% and   latex/.code etc ...
% I removed 	graphic/.code=	{\colorlet{graphicbackground}{#1}},
% I removed   \raggedright
%<----------------------------------------------------------------------------->
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tkzexample}[2009/02/14 v1 beta  code and graphic  ]
\definecolor{graphicbackground}{rgb}{0.96,0.96,0.8}%
\definecolor{codebackground}{rgb}{0.8,0.8,1}%
%\colorlet{codebackground}{Peach!20}%
\colorlet{numbackground}{Peach!60!black}%
\definecolor{numcolor}{rgb}{1,1,1}%
\makeatletter
\newcounter{code@cnt}%

\ifx\scantokens\@undefined
  \PackageError{tkzexample}{You need to use extended latex
    (elatex) or (pdfelatex) to use this package}{}
\fi

% Define \find@example such that it doesn't destroy catcodes:
\begingroup
\catcode`|=0
\catcode`[= 1
\catcode`]=2
\catcode`\{=12
\catcode `\}=12
\catcode`\\=12 |gdef|find@example#1\end{tkzexample}[|endoftkzexample[#1]]
|endgroup

% define \returntospace.
%
% It should define NEWLINE as {}, spaces and tabs as \space.
\begingroup
\catcode`\^=7
\catcode`\^^M=13
\catcode`\^^I=13
\catcode`\ =13%
\gdef\returntospace{\catcode`\ =13\def {\space}\catcode`\^^I=13\def^^I{\space}\catcode`\^^M=13\def^^M{}}%
\endgroup

\begingroup
\catcode`\%=13
\catcode`\^^M=13
\gdef\commenthandler{\catcode`\%=13\def%{\@gobble@till@return}}
\gdef\@gobble@till@return#1^^M{}
\gdef\@gobble@till@return@ignore#1^^M{\ignorespaces}
\gdef\typesetcomment{\catcode`\%=13\def%{\@typeset@till@return}}
\gdef\@typeset@till@return#1^^M{{\def%{\char`\%}\textsl{\char`\%#1}}\par}
\endgroup



\pgfqkeys{/tkzexample}{%
   width/.code=  {\setlength\tkzexamplewidth{#1}},
  graphic/.code=  {\colorlet{graphicbackground}{#1}},
  code/.code= {\colorlet{codebackground}{#1}},
	execute code/.is if=code@execute,
	code only/.code=	{\code@executefalse},
	show num/.is if=num@show,
	num/.code= {\num@showtrue\setcounter{code@cnt}{1}},
	global	num/.code= {\num@showtrue},
	normal/.code=	{\def\code@size{}},
	small/.code=	{\def\code@size{\small}},
	very	small/.code=	{\def\code@size{\footnotesize}},
	pre/.code=	{\def\code@pre{#1}},
	post/.code=	{\def\code@post{#1}},
  latex/.code= {\setlength\tkzexamplewidth{#1}%
                 \def\code@pre{\begin{minipage}{#1}}%
                 \def\code@post{\end{minipage}}},
	vbox/.code=	{\def\code@pre{\vbox\bgroup\setlength{\hsize}{\linewidth-6pt}}\def\code@post{\egroup}},
	ignorespaces/.code=	{\let\@gobble@till@return=\@gobble@till@return@ignore},
	leave comments/.code=	{\def\code@catcode@hook{\catcode`\%=12}\let\commenthandler=\relax\let\typesetcomment=\relax},
	every tkzexample/.style={width=8cm+7pt},
}
\def\code@size{}
\def\code@pre{}
\def\code@post{}
\def\code@catcode@hook{}

\newdimen\tkzexamplewidth
\newif\ifcode@execute
\newif\ifnum@show
\newbox\tkzexamplebox
\def\tkzexample[#1]{%
  %\setcounter{code@cnt}{1}%
  \begingroup%
  \code@executetrue
  \pgfqkeys{/tkzexample}{every tkzexample,#1}%
  \parindent0pt
  \begingroup%
  \par%
  \medskip%
  \let\do\@makeother%
  \dospecials%
  \obeylines%
  \@vobeyspaces%
  \catcode`\%=13%
  \catcode`\^^M=13%
  \code@catcode@hook%
  \relax%
  \find@example}
\def\endoftkzexample#1{%
  \endgroup%
  \ifcode@execute%
    \setbox\tkzexamplebox=\hbox{%
      {%
        {%
          \returntospace%
          \commenthandler%
          \protected@xdef\code@temp{#1}%added  by AM
         }%
       \colorbox{graphicbackground}{\color{black}\ignorespaces%
        \code@pre\expandafter\scantokens\expandafter{\code@temp\ignorespaces}\code@post\ignorespaces}%
    }%
    }%
    \ifdim\wd\tkzexamplebox>\tkzexamplewidth%
      \def\code@start{\par}%
      \def\code@flushstart{}%
      \def\code@flushend{}%
      \def\code@mid{\parskip2pt\par\noindent}%
      \def\code@width{\linewidth-6pt}%
      \def\code@end{}%
    \else%
      \def\code@start{%
        \linewidth=\textwidth%
        \parshape \@ne 0pt \linewidth
        \leavevmode%
        \hbox\bgroup}%
      \def\code@flushstart{\hfill}%
      \def\code@flushend{\hbox{}}%
      \def\code@mid{\hskip6pt\hskip2em}%added \hskip2em by AM
      \def\code@width{\linewidth-12pt-2em-\tkzexamplewidth}%modified by AM -2em
      \def\code@end{\egroup}%
    \fi%
    \code@start%
    \noindent%
    \begin{minipage}[t]{\tkzexamplewidth}\raggedright
      \hrule width0pt%
      \code@size\vskip-1em% 
      \code@flushstart\box\tkzexamplebox\code@flushend%
      \vskip-1ex%
      \leavevmode%
    \end{minipage}%
  \else%
    \def\code@mid{\par}
    \def\code@width{\linewidth-6pt}
    \def\code@end{}
  \fi%
  \code@mid%  
  \colorbox{codebackground}{%
    \begin{minipage}[t]{\code@width}%
      {%
        \let\do\@makeother
        \dospecials
        \frenchspacing\@vobeyspaces
        \normalfont\ttfamily\code@size%
        \typesetcomment%
        \@tempswafalse
        \def\par{%
          \if@tempswa
            \leavevmode\null\@@par\penalty\interlinepenalty%
            \ifnum@show%
              \noindent\parskip=0pt%
              \llap{\colorbox{numbackground}{%%added by AM
                    \textcolor{numcolor}{%%added by AM
                    \number\value{code@cnt}}}\enspace}%%added by AM
              \stepcounter{code@cnt}%%added by AM
            \fi
          \else
          \@tempswatrue%
          \ifhmode\@@par\penalty\interlinepenalty\fi%
            \ifnum@show%
              \noindent\parskip=0pt%
              \llap{\colorbox{numbackground}{%%added by AM
                    \textcolor{numcolor}{%%added by AM
                    \number\value{code@cnt}}}\enspace}%%added by AM
              \stepcounter{code@cnt}%%added by AM
            \fi
          \fi}%
        \obeylines
        \everypar \expandafter{\the\everypar \unpenalty}%
        #1}%
    \end{minipage}}%
  \code@end%
  \par%
  \medskip
  \end{tkzexample}
}

\def\endtkzexample{\endgroup}

\makeatother