Debian Codegraph @

texmf-dist/doc/latex/titlecaps/titlecaps.tex

raw
1 \documentclass{article}
2 %
3 % This work may be distributed and/or modified under the
4 % conditions of the LaTeX Project Public License, either version 1.3
5 % of this license or (at your option) any later version.
6 % The latest version of this license is in
7 % http://www.latex-project.org/lppl.txt
8 % and version 1.3c or later is part of all distributions of LaTeX
9 % version 2005/12/01 or later.
10 %
11 % This work has the LPPL maintenance status `maintained'.
12 %
13 % The Current Maintainer of this work is Steven B. Segletes.
14 %
15 \usepackage{titlecaps}
16 \usepackage{needspace}
17 \parskip 1em
18 \parindent 0em
19 \newcommand\rl{\rule{1em}{0in}}
20 \def\tcp{\textsf{titlecaps}}
21 \def\QL{\titlecap{quirks, tricks, and limitations}}
22 \usepackage{verbatimbox}
23 \let\vb\verb
24 \def\bs{$\backslash$}
25 \reversemarginpar
26 \marginparwidth 1.6in
27 \newcommand\margtt[1]{\marginpar{\hfill\ttfamily#1}}
28 \newcommand\margcmd[1]{\marginpar{\hfill\ttfamily\char'134#1}}
29 \begin{document}
30 \begin{center}
31 \LARGE The {\tcp} Package\\
32 \rule{0em}{.7em}\small Routines for setting rich-text input into
33 Titling Caps\\
34 \rule{0em}{2.7em}\large Steven B. Segletes\\
35 [email protected]\\
36 \rule{0em}{1.7em}\titlecapsVersionDate\\
37 v\titlecapsVersionNumber
38 \end{center}
39
40 \Addlcwords{for a is but and with of in as the etc on to if}
41 \section{\titlecap{description and commands}}
42
43 The {\tcp} package is intended to be used to convert lower-cased text
44 into titling caps, wherein the first letter of every word is
45 capitalized, except for words designated to remain in lower case, for
46 example prepositions and conjunctions. While this function has been
47 performed elsewhere, for example in the \textsf{stringstrings} package,
48 it is here significantly enhanced. The \textsf{stringstrings}
49 implementation of titling caps is particularly limited because of its
50 slow speed (I criticize \textsf{stringstrings} because I wrote it).
51 Furthermore, that implementation only works on textual expressions,
52 which do not contain any font-modification commands.
53
54 In contrast, the {\tcp} package is set up to work in conjunction with
55 font-modification commands that change the family, series, or shape of
56 the font. Likewise, it will work with the fontsize changing commands
57 (\vb|\tiny|, \vb|\scriptsize|\ldots\SoftSpace\vb|\Huge|) embedded in the
58 argument. It is also, unlike the \textsf{stringstrings} version, able
59 to screen out most punctuation when deciding whether a word is
60 predesignated as lower case. Furthermore, it looks for symbols that
61 might signify the beginning of a new text ``group'', such as (, [, \{,
62 `, and -, and thereafter titles the word that follows, even though the
63 first alphabetic letter is not technically at the lead of the ``word.''
64
65 The primary commands that have been implemented in this package are the
66 following:\\
67 \itshape
68 \rl\vb|\titlecap[|option\vb|]{|rich text\vb|}|\\
69 \rl\vb|\Addlcwords{|designated lower-case space-separated word list\vb|}|\\
70 \rl\vb|\Resetlcwords|\\
71 \upshape
72 In addition, the following auxiliary commands are also available:\\
73 \itshape
74 \rl\vb|\textnc{|text\verb|}|\\
75 \rl\vb|\def\converttilde{|\textup{T} or \textup{F}\vb|}|\\
76 \rl\vb|\noatinsidetc|\\
77 \rl\vb|\usestringstringsnames|
78 % (addlcwords,resetlcwords,addlcword,capitalizetitle,getargs)
79 \upshape
80
81 ~\\
82 The \vb|\titlecap| command\margcmd{titlecap} is the primary contribution
83 of this package. It will (within constraints) capitalize the first
84 letter of each word in the argument. The primary constraint under which
85 it operates is the existence of a predefined lower-cased word list that
86 is set by the user, using the \verb|\Addlcwords|
87 command\margcmd{Addlcwords}. These predefined lower-cased words are
88 passed as an argument to \verb|\Addlcwords| in a space-separated list.
89 Subsequent invocations add to the existing list of predefined
90 lower-cased words. The user may clear the list of predefined
91 lower-cased words through the issuance of the \verb|\Resetlcwords|
92 command\margcmd{Resetlcwords}.
93
94 When \verb|\titlecap| is invoked, it first breaks the argument into
95 individual pieces that constitute ``words.'' Note, however, that these
96 ``words'' may include punctuation and text formatting commands
97 interspersed with the actual text. This is an essential challenge to
98 overcome.
99
100 After breaking the argument into words, \verb|\titlecap| will attempt to
101 match each word of the argument to the list of predesignated lower-cased
102 words. In order to assist this process, the command (temporarily)
103 screens out text-formatting commands and punctuation marks from the
104 argument, so that their presence does not inhibit a word match with the
105 lower-cased word list (see {\QL} for exceptions). It
106 flags matches, so that these matching words will not later be titled.
107
108 The command will then reconstitute the ``words'' of the argument with
109 their punctuation and font-changing commands intact. In the default
110 invocation, \verb|\titlecap| will capitalize the first word of the
111 argument, even if that word is on the lower-cased word list. This
112 default may be overridden with the command's optional argument.
113 Employing anything other than a capital ``P'' will treat the first word
114 of the argument like any other word, meaning it will be title-capped
115 only if it does not appear on the predesignated lower-cased word list.
116
117 The algorithm that searches the ``words'' of the argument character by
118 character, in order to titlecap the first letter of a word, is, generally
119 speaking, able to process only textual content. However, special
120 provisions have been enacted to handle the following text-formatting
121 commands within the argument of \verb|\titlecap|:
122
123 \begin{minipage}{\textwidth}
124 \rl
125 \begin{tabular}{llll}
126 \verb|\textup| & \verb|\upshape| & \verb|\tiny| & \verb|\Huge|\\
127 \verb|\textit| & \verb|\itshape| & \verb|\scriptsize| &
128 \verb|\textnc|\footnote{This command is introduced by this package.}\\
129 \verb|\textsc| & \verb|\scshape| & \verb|\footnotesize|&\\
130 \verb|\textsl| & \verb|\slshape| & \verb|\small|&\\
131 \verb|\textmd| & \verb|\mdseries| & \verb|\normalsize|&\\
132 \verb|\textbf| & \verb|\bfseries| & \verb|\large|&\\
133 \verb|\textrm| & \verb|\rmfamily| & \verb|\Large|&\\
134 \verb|\textsf| & \verb|\sffamily| & \verb|\LARGE|&\\
135 \verb|\texttt| & \verb|\ttfamily| & \verb|\huge|&\\
136 \end{tabular}
137 \end{minipage}
138
139 Other macros can generally be handled in the argument to
140 \verb|\titlecap| only if they expand to textual content.
141
142 \needspace{3\baselineskip}
143 Once the first word of the argument has been titled ``by force'' (or
144 not, with the use of the optional argument), the command proceeds
145 through each word, deciding whether or not to title the word, based on
146 the lower-cased word flag that has previously been set. The {\tcp}
147 package can notably handle the titling of strings containing both
148 diacritical marks found in various languages (such as \`o, \'o, \^o,
149 \"o, \~o, \=o, \.o, \u o, \v o, \H o, \t{oo}, \c o, \d o, and \b o), as
150 well as national symbols (such as \oe, \ae, and \o~[see {\QL}]).
151
152 While punctuation had been earlier screened out in order to search for
153 predefined lower-cased words, that is a slightly different problem from
154 that of figuring out how to titlecap a punctuated word not on the
155 lower-cased list. While many punctuation marks trail a word and are,
156 therefore, not a problem, several punctuation marks lead a word, or
157 indicate a group separator, even in the absence of whitespace.
158 \verb|\titlecap| must make sure that these sorts of punctuation marks do
159 not inhibit the capitalization of the subsequent letter. To this end,
160 \verb|\titlecap| looks for instances of the following five characters:
161 -, (, [, \{, and `, and flags the next character for possible
162 capitalization (unless the word had been previously identified as
163 predefined lower-cased [see {\QL}]).
164
165 \section{\QL}
166
167 While {\tcp} has been set up to run with certain embedded size and
168 font-changing commands, it will not, in general, work with macros in the
169 argument, unless the macros expand directly to a text string.
170
171 The {\tcp} package is designed to work with diacritical marks (for
172 example, umlauts) as well as national symbols (symbols like \ae, \oe,
173 \textit{etc.}). There remain, however, two national symbols which are
174 not handled properly by this package. They are {\aa} and {\l}. They
175 will not be capitalized, even if found at the beginning of a word.
176
177 The {\tcp} package is designed to screen out punctuation when searching
178 for words that are pre-designated as lower-cased. So, for example, the
179 word (\textit{if}) or \textit{``if''} or [\textit{if} or \textit{if,}
180 will all be found to match \textit{if} when it is predesignated as lower
181 cased. However, {\tcp} cannot screen out the curly braces \verb|\{| and
182 \verb|\}| from the punctuation list. Thus, \{if\} will be capitalized
183 as \{If\} by \tcp. A workaround is detailed in each of the next two
184 paragraphs.
185
186 To prevent a word from being titled (to force it into lower case), it
187 can be immediately preceded by a \verb|\relax|. In this way, the
188 \verb|\relax| is titled, rather than the following word. This method
189 can be used to for one-time exceptions to titling, or to overcome the
190 curly-brace problem described above, as in \verb|\{\relax if\}|.
191
192 The package introduces a command, \verb|\textnc|\margcmd{textnc}
193 (standing for ``text no-change''). If used outside of the
194 \verb|\titlecap| argument, it is defined as \verb|\def\textnc#1{#1}|
195 and so is leaves the argument intact. Inside of the \verb|\titlecap|
196 argument, however, it forces its argument to be
197 independently considered as text, regardless of any surrounding
198 punctuation or other characters. Thus, this approach may also be used
199 to address the curly-brace issue as \verb|\{\textnc{if}\}|. In this
200 case, ``if'' would be titled if it is not on the lower-cased word list,
201 but left in lower case if it were on the lower-cased word list. The
202 \verb|\textnc| command is useful in a number of ways inside the argument
203 to \verb|\titlecap|, but always to signify its argument is to be treated
204 as a block of text, independently evaluated for its predesignated
205 lower-cased content.
206
207 If a separator like, let's say, a left paren, is used without
208 whitespace, in the fashion of ``\verb|a(b)|,'' then neither \verb|a| nor
209 \verb|b| could possibly be detected as lower-cased words, since
210 the ``word'' without punctuation would be \verb|ab|, which is neither
211 \verb|a| nor is it \verb|b|. For individual instances or exceptions,
212 the insertion of a \verb|\relax| prior to ``\verb|a|'' or ``\verb|b|''
213 would prevent titling of these terms. Alternately, the word
214 ``\verb|ab|'' could be added to the predesignated lower-cased words
215 list, in which case, ``\verb|a(b)|'' would be preserved in lower case.
216 A third (and perhaps preferred) method is the use of the
217 \verb|\textnc{}| construct, which is introduced solely for the purpose
218 of designating embedded text as a separate word grouping. Thus,
219 \verb|\textnc{a}(\textnc{b})| as an argument to \verb|\titlecap| would
220 guarantee that both ``\verb|a|'' and ``\verb|b|'' would be independently
221 examined for their presence in the lower-cased word list, irrespective
222 of any surrounding punctuation. In this paragraph, the letters
223 ``\verb|a|'' and ``\verb|b|'' have been used for simplicity, but could
224 actually represent words or groups of words.
225
226 By \LaTeX{} convention, expansion of the construct
227 ``\vb|very \large big|'' will
228 associate the \vb|\large| as the first letter of \vb|big|, rather than
229 as the last letter of \vb|very|. Unfortunately, leaving it that way
230 will screw up the titling of \vb|big|. Thus, when adapting the use of
231 fontsize changes to the {\tcp} package, the prior space is unskipped,
232 and a space is added after the fontsize change invocation, so that the
233 fontsize change command is at the end of the prior word, rather than at
234 the beginning of the next word. The one adverse side effect to this
235 approach is that a space \textbf{will} appear after a fontsize command,
236 even if one is not desired (for example, changing font size just prior
237 to punctuation). One can either issue a \vb|\unskip| following the
238 fontsize change to back-gobble the newly introduced space, or else place
239 the fontsize change following the punctuation.
240
241 While \vb|\uppercase| will not work within the argument of a
242 \vb|\titlecap|, it was found that enclosing part of the argument in
243 double braces \margtt{\char'173\char'173~\char'175\char'175}will produce
244 upper case for that double-enclosed text. Thus,\\
245 \rl\vb|\titlecap{This is a {{v\"ery}} big test}|\\
246 will produce \titlecap{This is a {{v\"ery}} big test}.
247
248 The implementation to make the fontsize change commands work within the
249 arguments of \vb|\titlecap| required the use of \vb|\makeatletter|
250 within \vb|\titlecap| itself (which is revoked with a
251 \vb|\makeatother| upon exit from \vb|\titlecap|). If that is not
252 considered a ``good practice'' for your situation, then you can disable
253 this feature with the command \vb|\noatintc|\margcmd{noatintc}.
254 However, it is likely, at that point, that the fontsize changing
255 commands as arguments will break the \vb|\titlecap| command.
256
257 Except in the case of the various \vb|\text|\textit{xx}\vb|{}| commands,
258 for which special provision has been made, and for the double-brace
259 quirk mentioned above, the argument of \vb|\titlecap| should not contain
260 braces used as group delimiters, for example, in the fashion of:\\
261 \rl\vb|\titlecap{this is a {\itshape test of|\\
262 \rl\rl\vb|the} emergency broadcast system}|\\ It may not break the code,
263 but will likely not produce a desired result. If groupings must be
264 placed in the argument, using \verb|\textnc{}| to condition the argument
265 is recommended. Thus,\\
266 \rl\vb|\titlecap{this is a \textnc{\itshape test of|\\
267 \rl\rl\vb|the} emergency broadcast system}|\\
268 will result in ``%
269 \titlecap{this is a \textnc{\itshape test of the} emergency
270 broadcast system}'' (of course, in this case, a \vb|\textit{}| would
271 have worked directly, without a problem).
272
273 There should be no direct use or need to nest \vb|\titlecap| commands.
274 However, if it is absolutely necessary, the embedded invocation of
275 \verb|\titlecap| should be expressed as\\
276 \rl\verb|\titlecap{...\titlecap\textnc{...}...}|.
277
278 \section{\titlecap{auxiliary commands}}
279
280 In addition to the primary commands offered by this package, there are
281 several auxiliary commands.
282
283 As described in the section {\QL}, the \verb|\textnc|\margcmd{textnc}
284 command was introduced when additional grouping must occur inside the
285 argument of a \verb|\titlecap| command. The command \verb|\textnc|
286 will itself make no changes to its argument, but will force
287 \verb|\titlecap| to independtly evaluate the \verb|\textnc| argument for
288 its lower-cased content, regardless of any surrounding punctuation or
289 text. The command may only be \textit{needed} within an argument to
290 \verb|\titlecap|. However, if your application requires its invocation
291 outside of a \verb|\titlecap| argument, it will merely output the
292 argument without any modification.
293
294 By default, the {\tcp} package treats hardspaces ($\sim$) as characters and
295 not white space. This default treatment can be changed by setting the
296 following parameter: \verb|\def\converttilde{T}|\margcmd{converttilde}.
297 Following that invocation, hardspaces in the argument of
298 \verb|\titlecap| should be indistinguishable from white space.
299
300 The command \verb|\noatinsidetc|\margcmd{noatinsidetc} was briefly
301 described in {\QL}. Its invocation will prevent the \verb|\titlecap|
302 command from employing the \verb|\makeatletter| command, if this is
303 considered bad practice for your setting. However, its invocation will
304 likely make the {\tcp} package unable to process fontsize command
305 changes.
306
307 The last of these auxiliary commands is
308 \verb|\usestringstringsnames|\margcmd{usestringstringsnames}. This
309 command will redefine certain command names previously defined by the
310 \textsf{stringstrings} package to instead use the corresponding commands
311 of the {\tcp} package, in essence redirecting the \textsf{stringstrings}
312 invocation to the current package. The redirected commands include the
313 following (note: see the \textsf{stringstrings} package documentation
314 for command arguments and options):\\
315 \rl\verb|\addlcwords|\\
316 \rl\verb|\resetlcwords|\\
317 \rl\verb|\addlcword|\\
318 \rl\verb|\getargs|\\
319 \rl\verb|\capitalizetitle|\\
320 If this command is invoked without the prior loading of the
321 \textsf{stringstrings} package, these commands will still be enabled for
322 subsequent use. Because the {\tcp} package produces output which can
323 include font changes and other material that cannot be placed into an
324 \verb|\edef|, the output of the quiet version of the newly redirected
325 \verb|\capitalizetitle[q]| produces a \verb|\def\thestring{}| rather
326 than an \verb|\edef\thestring{}|.
327
328 \section{Acknowledgements}
329
330 I would like to acknowledge the assistance of David Carlisle who,
331 through the \textsf{tex.stackexchange} website, assisted with both the
332 string parsing and punctuation screening techniques of this package:
333
334 \verb|http://tex.stackexchange.com/questions/101604/parsing-strings|\\
335 \verb|-containing-diacritical-marks-macros|
336
337 \verb|http://tex.stackexchange.com/questions/105735/ignoring|\\
338 \verb|-punctuation-during-comparison|
339
340 I would also like to thank Prof. Enrico Gregorio for his discovery of several
341 bugs in the package and suggestions for fixes:
342
343 \verb|http://tex.stackexchange.com/questions/225434/|\\
344 \verb|how-to-workaround-conflict-between-greek-and-titlecaps|
345
346 \clearpage
347 \section{\titlecap{a \ttfamily\bs titlecap \rmfamily demonstration
348 for beginners,\\ expressed in \texttt{\bs titlecap}}}
349
350 \Resetlcwords
351 \Addlcwords{for a is but and with of in as the etc on to if}
352 %\noatinsidetc
353 \titlecap{%
354 %{
355 to know that none of the words typed in this paragraph were initially
356 upper cased might be of interest to you. it is done to demonstrate the
357 behavioral features of this package. first, you should know the words
358 that i have pre-designated as lower case. they are: ``for a is but and
359 with of in as the etc on to if.'' you can define your own list. note
360 that punctuation, like the period following the word ``if'' did not mess
361 up the search for lower case (nor did the quotation marks just now).
362 punctuation which is screened out of the lower-cased word search pattern
363 include . , : ; ( ) [ ] ? ! ` ' however, I cannot screen text braces;
364 \{for example in\} is titled, versus (for example in), since the braces
365 are not screened out in the search for pre-designated lower-case words
366 like for and in. However, \texttt{\bs textnc} provides a workaround:
367 \{\textnc{for example in}\}. titlecap will consider capitalizing
368 following a (, [, \{, or - symbol, such as (abc-def). you can use your
369 text\textit{\relax xx} commands, like i just did here with the prior xx,
370 but if you want the argument of that command to not be titled, you
371 either need, in this example, to add \textit{xx} to the lowercase word
372 list, which you can see i did not. instead, i put ``\bs relax~xx'' as
373 the argument, so that, in essence, the \bs relax was capitalized, not
374 the x. Or you could use \texttt{\bs textnc} . here i demonstrate that
375 text boldface, \textbf{as in the \bs textbf command}, also works fine,
376 as do \texttt{texttt}, \textsl{textsl}, \textsc{textsc},
377 \textsf{textsf}, \textit{etc}. titlecap will work on diacritical marks,
378 such as \"apfel, \c cacao \textit{etc.}, \scriptsize fontsize \LARGE
379 changing commands\normalsize\unskip, as well as national symbols such as
380 \o laf, \ae gis, and \oe dipus. unfortunately, i could not get it to
381 work on the \aa~nor the \l~symbols. the method will work with some
382 things in math mode, capitalizing symbols if there is a leading space,
383 $x^2$ can become $ x^2$, and it can process but it will not capitalize
384 the greek symbols, such as $\alpha$, and will choke on most macros, if
385 they are not direct character expansions. additionally,
386 \textsf{titlecaps} also works with font changing declarations, for
387 example, \bs itshape\bs sffamily. \itshape\sffamily you can see that it
388 works fine. likewise, any subsequent \bs textxx command will, upon
389 completion, return the font to its prior state, such as this
390 \textbf{textbf of some text}. you can see that i have returned to the
391 prior font, which was italic sans-serif. now I will return to upright
392 roman\upshape\rmfamily. a condition that will not behave well is inner
393 braces, such as \ttfamily \bs titlecap\{blah \{inner brace material\}
394 blah-blah\}. \rmfamily see the section on quirks and limitations for a
395 workaround involving \texttt{\bs textnc}. titlecap will always
396 capitalize the first word of the argument (\textbf{even if it is on the
397 lower-case word list}), unless \texttt{\bs titlecap} is invoked with an
398 optional argument that is anything other than a capital p. in that case,
399 the first word will be titled \textit{unless} it is on the lowercase
400 word list. for example, i will do a \bs titlecap[\relax s]\{\relax
401 a~big~man\} and get ``\titlecap[s]\textnc{a big man}'' with the ``a''
402 not titled. i hope this package is useful to you, but as far as using
403 \textsf{titlecaps} on such large paragraphs\ldots \textbf{do not try
404 this at home!}}
405
406 \section{Code Listing}
407
408 \verbatiminput{titlecaps.sty}
409
410 \end{document}
411
412
413