summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/lstaddons/lstlinebgrd.dtx
blob: 860e6b6e813aa3d7b29939191c96c426b1f07ec4 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
% \iffalse meta-comment
%<=*COPYRIGHT>
%% Copyright (C) 2011-2012 by Martin Scharrer <martin@scharrer-online.de>
%% ----------------------------------------------------------------------
%% 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 Martin Scharrer.
%%
%% This work consists of the files lstlinebgrd.dtx and lstladdons.ins
%% and the derived filebase lstlinebgrd.sty.
%%
%<=/COPYRIGHT>
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{lstlinebgrd.dtx}[%
%<=*DATE>
    2012/05/03
%<=/DATE>
%<=*VERSION>
    v0.1
%<=/VERSION>
    DTX file for 'lstlinebgrd']
\documentclass{ydoc}
\GetFileInfo{lstlinebgrd.dtx}
\usepackage{lstlinebgrd}[\filedate]
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\floatstyle{plaintop}
\restylefloat{example}
\belowcaptionskip=\medskipamount

\renewcommand\topfraction{.9}
\renewcommand\bottomfraction{.9}
\renewcommand\textfraction{.1}
\renewcommand\floatpagefraction{.7}
\renewcommand\dbltopfraction{.9}
\begin{document}
  \DocInput{\jobname.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%

% \changes{v0.1}{2012/05/03}{Initial release.}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \GetFileInfo{lstlinebgrd.dtx}
% \author{Martin Scharrer}
% \email{martin@scharrer-online.de}
% \ifdefined\repository
%   \repository{https://bitbucket.org/martin_scharrer/lstlinebgrd}
% \fi
%
% \maketitle
%
% \begin{abstract}\noindent
% This add-on package to \pkg{listings} provides the option to add background colors to lines of listings.
% Every line can be given a different color by using a conditional which
% depends on the line number.
%
% This package was created as response to the question
% \href{http://tex.stackexchange.com/q/18969/2975}{``Creating a zebra effect using listings''}
% on \href{http://tex.stackexchange.com/}{\TeX\ Stack Exchange}.
% \end{abstract}
%
% \section{Usage}
% The following settings are added by \pkg{lstlinebgrd} for \pkg{listings} and can be used in \Macro\lstset or any
% \pkg{listings} macro which accepts options.
%
% \DescribeKey{linebackgroundcolor}'='<color commands>
% This options allows the user to provide some \LaTeX\ code which sets the background
% color. The code must use \Macro\color{<color>} or a different color macro.
% The use of conditionals is permitted and can be used to color lines depending on
% the line number (|\value{lstnumber}|) etc., as shown in the example section.
% The initial setting of this option is |{}|, i.e.\ no coloring is done.
%
% \DescribeKey{linebackgroundsep}'='<length>
% Sets the distance of the begin of the background color bar and the line.
% A horizontal skip of this amount to the left from the start of the line is done
% before drawing the background line. This means that positive amounts will move
% the background color bar to the left and negative amounts to the right.
% This does not effect the total width of the color bar.
%
% \DescribeKey{linebackgroundwidth}'='<length>
% Sets the width of the background color bar. The initial default value is |\linewidth|.
% Note that the background width does not take the actual width of the text into account,
% because \pkg{listing} does not provide this information.
%
% \DescribeKey{linebackgroundheight}'='<length>
% Sets the height of the background color bar, i.e.\ the amount above the baseline.
% The initial value is |\ht\strutbox|, i.e.\ the full height of the line.
%
% \DescribeKey{linebackgrounddepth}'='<length>
% Sets the depth of the background color bar, i.e.\ the amount below the baseline.
% The initial value is |\dp\strutbox|, i.e.\ the full depth of the line.
%
% \DescribeKey{linebackgroundcmd}'='<macro>
% This advanced option sets the macro used to draw the background color bar. It is called at the correct
% starting position and receives the width, height and depth as three arguments in this order.
% The initial value is |\color@block| which is an internal macro of the \pkg{xcolor} package.
% This option can be used by advanced users to fine tune the background coloring.
%
%
% \clearpage
% \section{Examples}
%   \def\PrintExample{%
%       \begingroup
%       \BoxExample
%       \sffamily
%       \par\medskip
%       \centerline{Code\strut}%
%       \smallskip
%       \hrule
%       \usebox\examplecodebox
%       \par\hrule\bigskip
%       \centerline{Result\strut}%
%       \smallskip
%       \hrule
%       \usebox\exampleresultbox
%       \par\hrule\medskip
%       \endgroup
%   }
%
% \begin{example}
%   \caption{Basic example: zebra effect.}
%   \begin{examplecode}
%   \begin{lstlisting}[language=C,basicstyle=\ttfamily,linebackgroundcolor={\ifodd\value{lstnumber}\color{green}\fi}]
%   /**
%   * Prints Hello World.
%   **/
%   #include <stdio.h>

%   int main(void) {
%   printf("Hello World!");
%   return 0;
%   }
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
% \begin{example}
%   \caption{Two-colored zebra effect with extra adjustments.}
%   \begin{examplecode}
%   \begin{lstlisting}[language=C,basicstyle=\ttfamily\Large,linebackgroundcolor={\ifodd\value{lstnumber}\color{green}\else\color{yellow}\fi},numbers=left,linebackgroundsep=1em,linebackgroundwidth=18em]
%   /**
%   * Prints Hello World.
%   **/
%   #include <stdio.h>
%
%   int main(void) {
%   printf("Hello World!");
%   return 0;
%   }
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
%
% \begin{example}
%   \caption{Gradient effect with extra adjustments.}
%   \begin{examplecode}
%   \begin{lstlisting}[language=C,basicstyle=\ttfamily\tiny,linebackgroundcolor={\color{blue!\the\numexpr 5*\value{lstnumber}\relax}},linebackgroundheight=1.7ex,linebackgrounddepth=.4ex]
%   /**
%   * Prints Hello World.
%   **/
%   #include <stdio.h>
%
%   int main(void) {
%   printf("Hello World!");
%   return 0;
%   }
%   /**
%   * Prints Hello World.
%   **/
%   #include <stdio.h>
%
%   int main(void) {
%   printf("Hello World!");
%   return 0;
%   }
%   \end{lstlisting}
%   \end{examplecode}
% \end{example}
%
% \StopEventually{}
% \clearpage
% \section{Implementation}
%
% \iffalse
%<*lstlinebgrd.sty>
% \fi
%    \begin{macrocode}
%<!COPYRIGHT>
\ProvidesPackage{lstlinebgrd}[%
%<!DATE>
%<!VERSION>
%<*DRIVER>
    2099/01/01 develop
%</DRIVER>
    Provides 'listings' options to color the background of lines]
%    \end{macrocode}
%    \begin{macrocode}
%
\RequirePackage{listings}
\RequirePackage{xcolor}

% Patch line number key to call line background macro
\lst@Key{numbers}{none}{%
    \def\lst@PlaceNumber{\lst@linebgrd}%
    \lstKV@SwitchCases{#1}%
    {none&\\%
     left&\def\lst@PlaceNumber{\llap{\normalfont
                \lst@numberstyle{\thelstnumber}\kern\lst@numbersep}\lst@linebgrd}\\%
     right&\def\lst@PlaceNumber{\rlap{\normalfont
                \kern\linewidth \kern\lst@numbersep
                \lst@numberstyle{\thelstnumber}}\lst@linebgrd}%
    }{\PackageError{Listings}{Numbers #1 unknown}\@ehc}}

% New keys
\lst@Key{linebackgroundcolor}{}{%
    \def\lst@linebgrdcolor{#1}%
}
\lst@Key{linebackgroundsep}{0pt}{%
    \def\lst@linebgrdsep{#1}%
}
\lst@Key{linebackgroundwidth}{\linewidth}{%
    \def\lst@linebgrdwidth{#1}%
}
\lst@Key{linebackgroundheight}{\ht\strutbox}{%
    \def\lst@linebgrdheight{#1}%
}
\lst@Key{linebackgrounddepth}{\dp\strutbox}{%
    \def\lst@linebgrddepth{#1}%
}
\lst@Key{linebackgroundcmd}{\color@block}{%
    \def\lst@linebgrdcmd{#1}%
}


% Line Background macro
\newcommand{\lst@linebgrd}{%
    \ifx\lst@linebgrdcolor\empty\else
    \rlap{%
        \lst@basicstyle
        \color{-.}% By default use the opposite (`-`) of the current color (`.`) as background
        \lst@linebgrdcolor{%
        \kern-\dimexpr\lst@linebgrdsep\relax%
        \lst@linebgrdcmd{\lst@linebgrdwidth}{\lst@linebgrdheight}{\lst@linebgrddepth}%
        }%
    }%
    \fi
}
%    \end{macrocode}
% \iffalse
%</lstlinebgrd.sty>
% \fi
%
% \Finale
\endinput