summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/phfquotetext/phfquotetext.dtx
blob: fda41bdd5a294e503d23c304be2b31bbfcadc3a7 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
% \iffalse meta-comment
%
% Copyright (C) 2016 by Philippe Faist <philippe.faist@bluewin.ch>
% -------------------------------------------------------
% 
% This file 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.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{phfquotetext.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[2005/12/01]
%<package>\ProvidesPackage{phfquotetext}
%<*package>
    [2016/08/15 v1.0 phfquotetext package]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{xcolor}
\usepackage{phfquotetext}
\usepackage[preset=xpkgdoc]{phfnote}
\usepackage[normalem]{ulem}
\EnableCrossrefs         
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{phfquotetext.dtx}
\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{v1.0}{2016/04/20}{Initial version}
%
% \GetFileInfo{phfquotetext.dtx}
%
% \DoNotIndex{\newcommand,\newenvironment,\def,\gdef,\edef,\xdef,\if,\else,\fi,\ifx}
% 
% \title{\phfqitltxPkgTitle{phfquotetext}}
% \author{Philippe Faist\quad\email{philippe.faist@bluewin.ch}}
% \date{\pkgfmtdate\filedate}
% \maketitle
%
% \begin{abstract}
%   \pkgname{phfquotetext}---Provides an environment for displaying
%   block text with special characters, such as verbatim quotes from a
%   referee report.
% \end{abstract}
%
% \inlinetoc
%
% \section{Introduction}
%
% This package provides a quoting environment useful to quote, e.g., email text.  It is
% somewhat like a verbatim environment (for instance, there is no macro expansion), but
% with also no white space formatting.
%
% For instance, you might find this useful for formatting replies to a
% referee, because any pseudo-\LaTeX{} commands are reproduced
% verbatim, but the bulk is still nicely formatted:
%
% \begin{verbatim}
% \begin{quotetext}
%   This is some text which was provided in plain text, for example,
%   by the referee.  The object \calE should probably be defined with
%   a factor 1/2 to help the \sum_k \ket{k} states not overwhelm rho
%   by this and that by more than 50%....
% \end{quotetext}
% \end{verbatim}
%
% \verbdef\tmpA|\quotetextfont|
% \verbdef\tmpB|\sffamily\color{blue}|
% The above code gives the following output:\footnote{
%   Here, we've assumed the \tmpA{} was redefined as
%   \tmpB{}.  Because it does look nicer.  See
%   \autoref{sec:customize-appearance}.}
%
% \renewcommand\quotetextfont{\sffamily\color{blue}}
% \begin{quotetext}   This is some text which was provided in plain text, for example, by a   referee.  The object \calE should probably be defined with a factor 1/2 to   help the \sum_k \ket{k} states not overwhelm rho by this and that by more than 50%.... \end{quotetext}
%
%
% 
%
%
% \section{Usage}
%
% \subsection{The main environment}
%
% The main environment is simply |\begin{quotetext} ... \end{quotetext}|.
%
% \DescribeEnv{quotetext}
% Delimit your email quote within this environment:
%
% \begin{verbatim}
% \begin{quotetext}
%   Stuff which you want to display verbatim, but with automatic white
%   space formatting and text re-flow.  \commands will not be
%   expanded, and will be displayed verbatim.  Percent signs also work
%   100%.
% \end{quotetext}
% \end{verbatim}
%
% The |{quotetext}| environment does not start a paragraph on its own, nor does it end it.
% You can thus in principle use this environment to quote text inline within a paragraph
% if you want to, unless you're enforcing new paragraphs in |\quotetextstart| and
% |\quotetextend|.
%
%
% \subsection{Customize appearance}
% \label{sec:customize-appearance}
%
% \DescribeMacro{\quotetextfont} This macro contains the formatting commands to set up the
% quote font.  By default, the contents is displayed in typewriter font.  You may redefine
% this command to achieve your favorite style:
% \begin{verbatim}
% \renewcommand\quotetextfont{\sffamily\color{blue}}
% \end{verbatim}
% 
% This macro may also be defined to accept one argument, which is the whole contents of
% the quoted text.  Make sure you define this with |\renewcommand| or |\long\def|, because
% the argument may contain several paragraphs.
%
% \DescribeMacro{\quotetextstart}
% This command gets called at the beginning of the quote environment.  It may be used
% e.g.\@ to add space, a horizontal rule, or add other formatting goodies.  By default, it
% is empty.
%
% \DescribeMacro{\quotetextend}
% This command gets called at the end of the quote environment.  It may be used e.g.\@ to
% add space, a horizontal rule, or add other formatting goodies.  By default, it is empty.
%
%
% The macros |\quotetextfont|, as well as |\quotetextstart| and
% |\quotetextend| are called within a \TeX{} group, so you don't need
% to worry that font overrides leak outside of the |quotetext|
% environment.
%
%
% You may try, for instance, the following to get started with customization:
% \begin{verbatim}
% \renewcommand\quotetextstart{%
%   \par\noindent{\color{blue}\hrule height 0.8pt}\vspace{2pt}\vspace{-\parskip}}
% \renewcommand\quotetextend{%
%   {\color{blue}\vspace{2pt}\hrule height 0.8pt\vspace{2pt}}}
% % \sout{} requires \usepackage[normalem]{ulem}:
% \renewcommand\quotetextfont[1]{\color{orange}\itshape\sout{\ignorespaces #1}}
% \end{verbatim}
% which gives:
% \renewcommand\quotetextstart{%
%   \par\noindent{\color{blue}\hrule height 0.8pt}\vspace{2pt}\vspace{-\parskip}}
% \renewcommand\quotetextend{%
%   {\color{blue}\vspace{2pt}\hrule height 0.8pt\vspace{2pt}}}
% \renewcommand\quotetextfont[1]{\color{orange}\itshape\sout{\ignorespaces #1}}
% \begin{quotetext} Stuff which you want to display verbatim, but with automatic white   space formatting and text re-flow.  \commands will not be expanded, and   will be displayed verbatim.  Percent signs also work 100%. \end{quotetext}
% 
%
% \subsection{Customize Catcodes---Advanced Usage}
%
% \DescribeMacro{\quotetextcatcodedefs}
% You may redefine this command to set catcodes before parsing the bulk of the
% |{quotetext}| environment.  This macro is called after \TeX's specials are already
% assigned catcode |12| (other).  By default, this macro is empty.
%
% \section{Known Caveats}
%
% \begin{itemize}
% \item The leading space in the environment content is not trimmed
%   from the argument passed on to |\quotetextfont|.  This just means
%   that if |\quotetextfont| is defined to accept an argument, it must
%   not forget to precede it with |\ignorespaces| as appropriate (as
%   in the example above in \autoref{sec:customize-appearance}).
%   I'm not sure how to remove trailing whitespace.
% \item By default, the |{quotetext}| environment does not start a new paragraph nor end
%   the paragraph, unless you enforce this explicitly in |\quotetextstart| and
%   |\quotetextend|.  This is purposely left as a feature to be aware of.
% \end{itemize}
%
%
%
%
% \StopEventually{\PrintChanges\PrintIndex}
%
% \section{Implementation}
%
% \subsection{Main Macros and Environment}
%
% \begin{macro}{\quotetextcatcodedefs}
%   Allows to set custom catcodes for parsing of quotetext contents.
%
%   By default, all char catcodes are set as "other" characters, but you may
%   redefine this macro to define you own catcodes.
%   
%   This macro is empty by default.
%    \begin{macrocode}
\newcommand\quotetextcatcodedefs{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\quotetextstart}
% \begin{macro}{\quotetextend}
%   Definitions which are called at the beginning or end of every
%   quote environment.  They are empty by default.
%    \begin{macrocode}
\newcommand\quotetextstart{}
\newcommand\quotetextend{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\quotetextfont}
% Set up the font and display for the quote text environment.
%
% This command may also be defined to take one single argument, which will be
% the whole contents of the quotetext environment.
%    \begin{macrocode}
\newcommand\quotetextfont{\ttfamily}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{environment}{quotetext}
%   The main environment.  Starts group and relays the call to |\phfquotetext@warg| to
%   start reading the contents of the environment.
%
%   Start the environment definition.
%    \begin{macrocode}
\newenvironment{quotetext}{%
%    \end{macrocode}
% \iffalse meta-comment %} -- for emacs' paren matching
% \fi
%
% Do anything we should do before the |quotetext| environment.
%    \begin{macrocode}
  \quotetextstart%
%    \end{macrocode}
%
% Make sure to enclose the full quote text stuff into its own group to make definitions
% such as catcode overrides local.
%    \begin{macrocode}
  \begingroup%
%    \end{macrocode}
% 
% Finally, relay the call to |\phfquotetext@warg| to read the whole contents of the
% environment, and finish the environment definition.
%    \begin{macrocode}
  \phfquotetext@warg%
}
%    \end{macrocode}
% 
% Finally, in the closing part of the environment, do anything which
% needs to be done at the end.
%    \begin{macrocode}
{%
  \quotetextend%
}
%    \end{macrocode}
% \end{environment}
%
%
%
% \subsection{Internal Stuff}
%
% \begin{macro}{\phfquotetext@warg@}
%   Reads an argument delimited by ....|\end{quotetext}|, ends the current \TeX{} group
%   assumed open (in order to change the catcodes), and calls the start/show/end commands.
%
%   We need deep catcode dark magic to just even define this command.
%   \begin{macrocode}
\begingroup
  \catcode`|=0
  \catcode`\<=1
  \catcode`\>=2
  \catcode`\{=12
  \catcode`\}=12
  \catcode`\\=12
  |long|gdef|phfquotetext@warg@#1\end{quotetext}<|endgroup
    |phfquotetext@show<#1>>
|endgroup
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\phfquotetext@show}
%   Shows the content given as argument with the proper formatting.  Includes an actual
%   call to |\end{quotetext}| to make \LaTeX's bookcounting mechanism for |\begin|/|\end|
%   happy, and which finally also calls |\quotetextend|.
%   \begin{macrocode}
\newtoks\qtt@mytoks
\long\def\phfquotetext@show#1{%
  \qtt@mytoks={#1}%
  \begingroup%
%    \end{macrocode}
% Crucially, |\the\toks| only expands once and prohibits further
% expansion.  This way, we may pass the argument directly to
% |\quotetextfont|, with tokens expanded exactly as they were given:
%    \begin{macrocode}
  \expandafter\quotetextfont\expandafter{\the\qtt@mytoks}%
  \endgroup%
  \end{quotetext}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\phfquotetext@warg}
%   Prepares to read the contents of the environment.  A new \TeX{} group has already been
%   opened by |\begin{quotetext}|.
%    \begin{macrocode}
\def\phfquotetext@warg{%
  %
%    \end{macrocode}
% Set the catcode of all special chars to ``other'' (12).  We use |\dospecials| provided
% by \LaTeX{} itself.\footnote{see \url{http://tex.stackexchange.com/a/12890/32188}} We
% leave the space token to the ``space'' catcode, so that white space is properly managed
% by LaTeX.  Finally, include any user catcode overrides in |\quotetextcatcodedefs|.
%    \begin{macrocode}
  \let\do\@makeother%
  \dospecials%
  \catcode`\ =10\relax% keep space
  \quotetextcatcodedefs%
%    \end{macrocode}
%
% Finally, relay the call to |\phfquotetext@warg@| to read the contents of the group and
% close the \TeX{} group.
%
%    \begin{macrocode}
  \phfquotetext@warg@%
}
%    \end{macrocode}
% \end{macro}
%
%
%\Finale
\endinput