| 1 |
% \iffalse meta-comment |
| 2 |
% !TeX spellcheck = en-US |
| 3 |
% |
| 4 |
% File: codesection.sty |
| 5 |
% Version: 2014/06/27 v0.1 |
| 6 |
% Author: Matthias Pospiech |
| 7 |
% Email: <[email protected]> |
| 8 |
% |
| 9 |
% Copyright (C) 2014 by Matthias Pospiech <[email protected]> |
| 10 |
% --------------------------------------------------------------------------- |
| 11 |
% This work may be distributed and/or modified under the |
| 12 |
% conditions of the LaTeX Project Public License, either version 1.3 |
| 13 |
% of this license or (at your option) any later version. |
| 14 |
% The latest version of this license is in |
| 15 |
% http://www.latex-project.org/lppl.txt |
| 16 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 17 |
% version 2005/12/01 or later. |
| 18 |
% |
| 19 |
% This work has the LPPL maintenance status `maintained'. |
| 20 |
% |
| 21 |
% The Current Maintainer of this work is Matthias Pospiech. |
| 22 |
% |
| 23 |
% This work consists of the files codesection.dtx and codesection.ins |
| 24 |
% and the derived filebase codesection.sty. |
| 25 |
% |
| 26 |
% \fi |
| 27 |
% |
| 28 |
% \iffalse |
| 29 |
%<*driver> |
| 30 |
\ProvidesFile{codesection.dtx} |
| 31 |
%</driver> |
| 32 |
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01] |
| 33 |
%<package>\ProvidesPackage{codesection} |
| 34 |
%<*package> |
| 35 |
[2014/06/27 v0.1 disableable code sections] |
| 36 |
%</package> |
| 37 |
% |
| 38 |
%<*driver> |
| 39 |
\documentclass{ltxdoc} |
| 40 |
\usepackage{hypdoc} |
| 41 |
\RequirePackage[% |
| 42 |
loadHyperref=true, |
| 43 |
createIndexEntries=false, |
| 44 |
applyLayout=true] |
| 45 |
{doctools} % |
| 46 |
% |
| 47 |
\usepackage{lmodern} |
| 48 |
|
| 49 |
\usepackage{codesection}[2014/06/27] |
| 50 |
\EnableCrossrefs |
| 51 |
\CodelineIndex |
| 52 |
\RecordChanges |
| 53 |
|
| 54 |
\begin{document} |
| 55 |
\DocInput{codesection.dtx} |
| 56 |
\PrintChanges |
| 57 |
\PrintIndex |
| 58 |
\end{document} |
| 59 |
%</driver> |
| 60 |
% \fi |
| 61 |
% |
| 62 |
% \CheckSum{0} |
| 63 |
% |
| 64 |
% \CharacterTable |
| 65 |
% {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 |
| 66 |
% 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 |
| 67 |
% Digits \0\1\2\3\4\5\6\7\8\9 |
| 68 |
% Exclamation \! Double quote \" Hash (number) \# |
| 69 |
% Dollar \$ Percent \% Ampersand \& |
| 70 |
% Acute accent \' Left paren \( Right paren \) |
| 71 |
% Asterisk \* Plus \+ Comma \, |
| 72 |
% Minus \- Point \. Solidus \/ |
| 73 |
% Colon \: Semicolon \; Less than \< |
| 74 |
% Equals \= Greater than \> Question mark \? |
| 75 |
% Commercial at \@ Left bracket \[ Backslash \\ |
| 76 |
% Right bracket \] Circumflex \^ Underscore \_ |
| 77 |
% Grave accent \` Left brace \{ Vertical bar \| |
| 78 |
% Right brace \} Tilde \~} |
| 79 |
% |
| 80 |
% |
| 81 |
% \changes{v0.1}{2014/06/27}{Converted to DTX file} |
| 82 |
% |
| 83 |
% \DoNotIndex{\newcommand,\newenvironment} |
| 84 |
% |
| 85 |
% \providecommand*{\url}{\texttt} |
| 86 |
% \GetFileInfo{codesection.dtx} |
| 87 |
% \title{The \textsf{codesection} package} |
| 88 |
% \author{Matthias Pospiech \\ \url{[email protected]}} |
| 89 |
% \date{\fileversion~from \filedate} |
| 90 |
% |
| 91 |
% \maketitle |
| 92 |
% |
| 93 |
% \section{Introduction} |
| 94 |
% This packages provides an environment to switch a section of code |
| 95 |
% on or off. The code can be placed anywhere in the file and is |
| 96 |
% not limited to the document or the preamble. |
| 97 |
% The motivation for this package was to have commands which allow to preselect if |
| 98 |
% sections of code in a preamble of a template are executed or not. |
| 99 |
% |
| 100 |
% \section{Origin of the code} |
| 101 |
% The code is based on the \texttt{verbatim.sty} package and was |
| 102 |
% originally modified by Ulrich Diez to match the pure comment |
| 103 |
% functionality. Further modifications are contributed by |
| 104 |
% Matthias Pospiech. |
| 105 |
% During the development some discussion about the best approach took place |
| 106 |
% on de.comp.text.tex% |
| 107 |
% \footnote{\url{http://groups.google.com/group/de.comp.text.tex/% |
| 108 |
% browse_thread/thread/2c18f0c221ab167f/}}, which resulted in the current code. |
| 109 |
% |
| 110 |
% \section{Usage} |
| 111 |
% The idea of the following commands is to define a collection of code, here |
| 112 |
% notated as a \emph{section}, which can be executed as it would be without the |
| 113 |
% commands or which is not executed at all. To use that section it must be defined |
| 114 |
% with \emph{true} (execute code) or \emph{false} (skip code). |
| 115 |
% |
| 116 |
% \DescribeMacro{\DefineCodeSection} \oarg{true/false}\marg{name} |
| 117 |
% \AfterLastParam |
| 118 |
% Defines a code section with a \emph{name}. The default is \emph{true}, |
| 119 |
% thus the code will be executed. |
| 120 |
% |
| 121 |
% \DescribeMacro{\SetCodeSection} \marg{name}\marg{true/false} |
| 122 |
% \AfterLastParam |
| 123 |
% is like \cs{DefineCodeSection}, but with both arguments mandatory. |
| 124 |
% |
| 125 |
% \DescribeMacro{\BeginCodeSection} \marg{name} \AfterLastParam |
| 126 |
% starts the code section with the given name and |
| 127 |
% |
| 128 |
% \DescribeMacro{\EndCodeSection} \marg{name} \AfterLastParam |
| 129 |
% ends the code section with the given name. Note that both commands need to |
| 130 |
% be paired and not to be nested with other code sections. |
| 131 |
% |
| 132 |
% \cs{BeginCodeSection} and \cs{EndCodeSection} \AfterLastParam |
| 133 |
% mimic an environment. It would be preferable to define them as an |
| 134 |
% environment, but that opens a group in \TeX{}, which has many disadvantages. |
| 135 |
% For example this would make it impossible to load packages. Therefore this |
| 136 |
% package defines paired commands and consequently, has no such limitations. |
| 137 |
% |
| 138 |
% % |
| 139 |
% \section{Example} |
| 140 |
% In the following code the first section is going to be executed and the second |
| 141 |
% and the third are completely skipped. |
| 142 |
% \iffalse |
| 143 |
%<*example> |
| 144 |
% \fi |
| 145 |
\begin{lstlisting}[style=lstDemoStyleLaTeXCode] |
| 146 |
\DefineCodeSection[true]{ExecuteMe} |
| 147 |
\DefineCodeSection[false]{SkipMe} |
| 148 |
% |
| 149 |
\BeginCodeSection{ExecuteMe} |
| 150 |
This sentence has ... |
| 151 |
\EndCodeSection{ExecuteMe} |
| 152 |
% |
| 153 |
\BeginCodeSection{SkipMe} |
| 154 |
no end. |
| 155 |
\EndCodeSection{SkipMe} |
| 156 |
% |
| 157 |
\SetCodeSection{ExecuteMe}{false} |
| 158 |
% |
| 159 |
\BeginCodeSection{ExecuteMe} |
| 160 |
a different ending. |
| 161 |
\EndCodeSection{ExecuteMe} |
| 162 |
\end{lstlisting} |
| 163 |
% \iffalse |
| 164 |
%</example> |
| 165 |
% \fi |
| 166 |
% |
| 167 |
% \DefineCodeSection[true]{ExecuteMe} |
| 168 |
% \DefineCodeSection[false]{SkipMe} |
| 169 |
% \BeginCodeSection{ExecuteMe} |
| 170 |
% This sentence has ... |
| 171 |
% \EndCodeSection{ExecuteMe} |
| 172 |
% % |
| 173 |
% \BeginCodeSection{SkipMe} |
| 174 |
% no end. |
| 175 |
% \EndCodeSection{SkipMe} |
| 176 |
% % |
| 177 |
% \SetCodeSection{ExecuteMe}{false} |
| 178 |
% % |
| 179 |
% \BeginCodeSection{ExecuteMe} |
| 180 |
% a different ending. |
| 181 |
% \EndCodeSection{ExecuteMe}% |
| 182 |
% |
| 183 |
% \StopEventually{} |
| 184 |
% |
| 185 |
% \section{Implementation} |
| 186 |
% |
| 187 |
% \iffalse |
| 188 |
%<*codesection.sty> |
| 189 |
% \fi |
| 190 |
% |
| 191 |
% |
| 192 |
% \begin{macrocode} |
| 193 |
\NeedsTeXFormat{LaTeX2e}[1994/12/01] |
| 194 |
\ProvidesPackage{codesection} |
| 195 |
[2014/06/27 v0.1 disableable code sections] |
| 196 |
\RequirePackage{etoolbox} |
| 197 |
% \end{macrocode} |
| 198 |
% |
| 199 |
% \begin{macro}{\DefineCodeSection} |
| 200 |
% \begin{macrocode} |
| 201 |
%%---------------------------------------------------------------------- |
| 202 |
%% provide new if (\ifCodeSection<name>) |
| 203 |
%% with definition \CodeSection<name><true> |
| 204 |
%% |
| 205 |
\newcommand{\DefineCodeSection}[2][true]{% |
| 206 |
\expandafter\newif\csname ifCodeSection#2\endcsname |
| 207 |
\csname CodeSection#2#1\endcsname |
| 208 |
}% |
| 209 |
% \end{macrocode} |
| 210 |
% \end{macro} |
| 211 |
% |
| 212 |
% |
| 213 |
% \begin{macro}{\SetCodeSection} |
| 214 |
% \begin{macrocode} |
| 215 |
%%---------------------------------------------------------------------- |
| 216 |
%% Alternative to \DefineCodeSection |
| 217 |
\newcommand{\SetCodeSection}[2]{% |
| 218 |
\DefineCodeSection[#2]{#1} |
| 219 |
}% |
| 220 |
% \end{macrocode} |
| 221 |
% \end{macro} |
| 222 |
% |
| 223 |
% |
| 224 |
% \begin{macro}{\BeginCodeSection} |
| 225 |
% \begin{macrocode} |
| 226 |
%%---------------------------------------------------------------------- |
| 227 |
\newcommand\BeginCodeSection[1]{% |
| 228 |
\ifcsdef{ifCodeSection#1}{}{% |
| 229 |
\PackageError{codesection}{Section #1 is unknown\MessageBreak}{}% |
| 230 |
} |
| 231 |
\csname ifCodeSection#1\endcsname |
| 232 |
\expandafter\@secondoftwo |
| 233 |
\else |
| 234 |
\expandafter\@firstoftwo |
| 235 |
\fi |
| 236 |
{% comment all code inside template section |
| 237 |
\@bsphack |
| 238 |
%% open new group |
| 239 |
\begingroup |
| 240 |
%% save current template section name |
| 241 |
\def\@currtemplate{#1}% |
| 242 |
\let\do\@makeother\dospecials |
| 243 |
\catcode`\^^M\active |
| 244 |
%% enter main loop |
| 245 |
\codeSection@ |
| 246 |
}% |
| 247 |
{% execute all code inside template section |
| 248 |
%% = do nothing except trimming spaces |
| 249 |
\@bsphack\@esphack% |
| 250 |
}% |
| 251 |
} |
| 252 |
% \end{macrocode} |
| 253 |
% \end{macro} |
| 254 |
% |
| 255 |
% |
| 256 |
% \begin{macro}{\EndCodeSection} |
| 257 |
% \begin{macrocode} |
| 258 |
%%---------------------------------------------------------------------- |
| 259 |
%% The macros \@bsphack and \@esphack are internal to LaTeX; |
| 260 |
%% they ensure that an entity like a marginal note or |
| 261 |
%% label definition does not introduce any extra space |
| 262 |
%% into a paragraph, independently of whether or not |
| 263 |
%% it is attached to a word. |
| 264 |
\newcommand\EndCodeSection[1]{\@bsphack\@esphack} |
| 265 |
% \end{macrocode} |
| 266 |
% \end{macro} |
| 267 |
% |
| 268 |
% Modified code from \texttt{verbatim.sty}. This code is not very well documented, |
| 269 |
% because I do not understand it well enough. |
| 270 |
% \begin{macrocode} |
| 271 |
%%---------------------------------------------------------------------- |
| 272 |
%% usage ??? |
| 273 |
\@ifundefined{vrb@catcodes}{% |
| 274 |
\def\vrb@catcodes{% |
| 275 |
\catcode`\!12\catcode`\[12\catcode`\]12% |
| 276 |
}% |
| 277 |
}{}% |
| 278 |
%%---------------------------------------------------------------------- |
| 279 |
\begingroup |
| 280 |
\vrb@catcodes |
| 281 |
\lccode`\!=`\\ |
| 282 |
\lccode`\[=`\{ |
| 283 |
\lccode`\]=`\} |
| 284 |
\catcode`\~=\active |
| 285 |
\lccode`\~=`\^^M |
| 286 |
\lccode`\C=`\C |
| 287 |
\lowercase{% |
| 288 |
%%---------------------------------------------------------------------- |
| 289 |
\def\codeSection@#1{% |
| 290 |
\endgroup |
| 291 |
%% ---- |
| 292 |
\def\codeSection@##1~{\codeSection@@##1!#1\@nil}% |
| 293 |
%% ---- |
| 294 |
\def\codeSection@@##1!#1{\futurelet\next\codeSection@@@}% |
| 295 |
%% ---- |
| 296 |
\def\codeSection@@@##1\@nil{% |
| 297 |
\ifx\next\@nil |
| 298 |
\let\next\codeSection@ |
| 299 |
\else |
| 300 |
\def\@tempa####1!#1\@nil{####1}% |
| 301 |
\def\next{\expandafter\codeSection@test\@tempa##1\@nil~}% |
| 302 |
\fi |
| 303 |
\next |
| 304 |
}% |
| 305 |
%% ---- |
| 306 |
\def\codeSection@test##1{% |
| 307 |
\let\next\codeSection@test |
| 308 |
\if\noexpand##1\noexpand~\let\next\codeSection@ |
| 309 |
\else \if\noexpand##1 |
| 310 |
\else \if\noexpand##1\noexpand[\let\@tempc\@empty |
| 311 |
\let\next\codeSection@testend |
| 312 |
\else \def\next{\codeSection@##1}% |
| 313 |
\fi\fi\fi |
| 314 |
\next |
| 315 |
}% |
| 316 |
%% ---- |
| 317 |
\def\codeSection@testend##1{% |
| 318 |
\if\noexpand##1\noexpand~\let\next\codeSection@ |
| 319 |
\else \if\noexpand##1\noexpand]\let\next\codeSection@@testend |
| 320 |
\else\if\noexpand##1\noexpand!\def\next{\codeSection@!}% |
| 321 |
\else \expandafter\def\expandafter\@tempc\expandafter |
| 322 |
{\@tempc##1}% |
| 323 |
\fi\fi\fi |
| 324 |
\next |
| 325 |
}% |
| 326 |
%% ---- test if end statement belongs to current section |
| 327 |
%% saved in \@currtemplate |
| 328 |
\def\codeSection@@testend{% |
| 329 |
\ifx\@tempc\@currtemplate |
| 330 |
%% end group and call rescan |
| 331 |
\edef\next{\noexpand\endgroup\noexpand\@esphack |
| 332 |
\noexpand\codeSection@rescan{\@currtemplate}}% |
| 333 |
\else |
| 334 |
%% start loop gain |
| 335 |
\let\next\codeSection@ |
| 336 |
\fi |
| 337 |
\next |
| 338 |
}% |
| 339 |
%% ---- does what ??? |
| 340 |
\def\codeSection@rescan##1##2~{% |
| 341 |
\if\noexpand~\noexpand##2~% |
| 342 |
\else |
| 343 |
\@warning{% |
| 344 |
Characters dropped after |
| 345 |
`\string\EndCodeSection{##1}'% |
| 346 |
}% |
| 347 |
\fi |
| 348 |
}% |
| 349 |
}% |
| 350 |
%%---------------------------------------------------------------------- |
| 351 |
} % lowercase |
| 352 |
\codeSection@{EndCodeSection}% |
| 353 |
%%---------------------------------------------------------------------- |
| 354 |
% \end{macrocode} |
| 355 |
% |
| 356 |
% |
| 357 |
% \iffalse |
| 358 |
%</codesection.sty> |
| 359 |
% \fi |
| 360 |
% |
| 361 |
% \Finale |
| 362 |
\endinput |
| 363 |
|