summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/trimspaces/trimspaces.tex
blob: d8041d36c7b995154e5b2a037ab111b451acde6a (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
\RequirePackage{filecontents}
\begin{filecontents*}{README.txt}
______________________
The TRIMSPACES package
v1.1

This package provides commands that allow you to expandably
remove spaces around a token list or remove surrounding
spaces within a macro definition.

Will Robertson
Copyright 2009
Released under the LaTeX Project Public License

\end{filecontents*}

\begin{filecontents}{trimspaces.sty}
\ProvidesPackage{trimspaces}[2009/09/17 v1.1
  Trim spaces around a token list]

% Trimming surrounding spaces:
\catcode`\Q=3
\newcommand\trim@spaces[1]{%
  \romannumeral-`\q\trim@trim@\noexpand#1Q Q%
}
\long\def\trim@trim@#1 Q{\trim@trim@@#1Q}
\long\def\trim@trim@@#1Q#2{#1}
\catcode`\Q=11

\newcommand\trim@spaces@noexp[1]{%
  \unexpanded\expandafter\expandafter\expandafter
    {\trim@spaces{#1}}%
}
    
\newcommand\trim@spaces@in[1]{%
  \edef#1{\expandafter\trim@spaces@noexp\expandafter{#1}}%
}

% Trimming preceding spaces:
\newcommand\trim@pre@space[1]{%
  \romannumeral-`\.\expandafter\noexpand#1%
}

\newcommand\trim@pre@space@noexp[1]{%
  \unexpanded\expandafter{%
    \romannumeral-`\.\expandafter\noexpand#1%
  }%
}

\newcommand\trim@pre@space@in[1]{%
  \expandafter\def\expandafter#1\expandafter{%
    \romannumeral-`\.\expandafter\noexpand#1%
  }%
}

% Trimming trailing space:
\catcode`\Q=3
\newcommand\trim@post@space[1]{\trim@trim@#1Q Q}
\catcode`\Q=11

\newcommand\trim@post@space@noexp[1]{%
  \unexpanded\expandafter\expandafter\expandafter
  \expandafter\expandafter\expandafter\expandafter
    {\trim@post@space{#1}}%
}
    
\newcommand\trim@post@space@in[1]{%
  \edef#1{\expandafter\trim@post@space@noexp\expandafter{#1}}%
}

% That's it.
\end{filecontents}
%%%%%%%%%1%%%%%%%%%2%%%%%%%%%3%%%%%%%%%4%%%%%%%%%5


% Conditionally compile the documentation & generate the .ins file:
\providecommand\pstoolCompile{Y}
\if\pstoolCompile N
  \expandafter\endinput
\fi


\begin{filecontents*}{trimspaces.ins}
%&latex
\def\pstoolCompile{N}
\input pstool.tex
\csname @@end\endcsname
\end{filecontents*}




\makeatletter
\documentclass{article}
\usepackage[rm,medium]{titlesec}
\usepackage{xcolor}
\usepackage[colorlinks,linktocpage]{hyperref}
\usepackage{gmdoc}
\usepackage{gmverb}
\dekclubs
\stanzaskip=\bigskipamount 
\CodeSpacesGrey

\let\pkg\textsf

\usepackage{trimspaces}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{lmodern}
\usepackage[sc,osf]{mathpazo}
\linespread{1.1}
\frenchspacing

\GetFileInfo{trimspaces.sty}
\begin{document}

\title{The \pkg{trimspaces} package}
\author{Package author: Will Robertson\\Code thanks to Morten H\o gholm}
\date{\fileversion\qquad\filedate}

\maketitle
\tableofcontents

\section{Use of the package}
This package provides a collection of space trimming macros for the \LaTeX\ programmer. Tools are provided to remove either preceding space, trailing space, or both from a token list or the contents of a macro.

For an extensive discussion on this topic, see chapter fifteen of Peter Wilson's compendium of Michael Downes' `Around the Bend' exercises:
\par\centerline{\url{http://tug.ctan.org/pkg/around-the-bend}}

The code that this package is based on, however, originates with Morten H\o gholm. Thanks!

\subsection{Trimming spaces}
\begin{itemize}
\item \cs{trim@spaces} \marg{text}\\ 
\cs{trim@pre@space} \marg{text}\\
\cs{trim@post@space} \marg{text}\par 
Fully expandable, removes spaces at the beginning and/or end of \meta{text}.

\item \cs{trim@spaces@noexp} \marg{text}\\
\cs{trim@pre@space@noexp} \marg{text}\\
\cs{trim@post@space@noexp} \marg{text}\par
Variants of the above that after full expansion result in the original unexpanded text stripped of surrounding spaces.
(\textit{Requires $\varepsilon$-\TeX}.)

\item \cs{trim@spaces@in} \meta{macro}\\
\cs{trim@pre@space@in} \meta{macro}\\
\cs{trim@post@space@in} \meta{macro}\par
Re-defines \meta{macro} as itself stripped of its surrounding spaces
(these commands are obviously not expandable).
\end{itemize}

\subsection{Examples}

Trimming surrounding spaces:
\begin{verbatim}
\def\foo{bar}
\edef\a{\trim@spaces{ a \foo ! }}
\edef\aa{\trim@spaces@noexp{ aa \foo ! }}
\def\b{ b \foo ! }
\trim@spaces@in\b
a:  [\detokenize\expandafter{\a}] \\
aa: [\detokenize\expandafter{\aa}] \\
b:  [\detokenize\expandafter{\b}]
\end{verbatim}
which produces:
\begin{quote}
\makeatletter
\def\foo{bar}
\edef\a{\trim@spaces{ a \foo ! }}
\edef\aa{\trim@spaces@noexp{ aa \foo ! }}
\def\b{ b \foo ! }
\trim@spaces@in\b
a:  [\detokenize\expandafter{\a}] \\
aa: [\detokenize\expandafter{\aa}] \\
b:  [\detokenize\expandafter{\b}]
\end{quote}
Only removing preceding spaces:
\begin{verbatim}
\def\foo{bar}
\edef\a{\trim@pre@space{ a \foo ! }}
\edef\aa{\trim@pre@space@noexp{ aa \foo ! }}
\def\b{ b \foo ! }
\trim@pre@space@in\b
a:  [\detokenize\expandafter{\a}] \\
aa: [\detokenize\expandafter{\aa}] \\
b:  [\detokenize\expandafter{\b}]
\end{verbatim}
which produces:
\begin{quote}
\makeatletter
\def\foo{bar}
\edef\a{\trim@pre@space{ a \foo ! }}
\edef\aa{\trim@pre@space@noexp{ aa \foo ! }}
\def\b{ b \foo ! }
\trim@pre@space@in\b
a:  [\detokenize\expandafter{\a}] \\
aa: [\detokenize\expandafter{\aa}] \\
b:  [\detokenize\expandafter{\b}]
\end{quote}
Only removing trailing spaces:
\begin{verbatim}
\def\foo{bar}
\edef\a{\trim@post@space{ a \foo ! }}
\edef\aa{\trim@post@space@noexp{ aa \foo ! }}
\def\b{ b \foo ! }
\trim@post@space@in\b
a:  [\detokenize\expandafter{\a}] \\
aa: [\detokenize\expandafter{\aa}] \\
b:  [\detokenize\expandafter{\b}]
\end{verbatim}
which produces:
\begin{quote}
\makeatletter
\def\foo{bar}
\edef\a{\trim@post@space{ a \foo ! }}
\edef\aa{\trim@post@space@noexp{ aa \foo ! }}
\def\b{ b \foo ! }
\trim@post@space@in\b
a:  [\detokenize\expandafter{\a}] \\
aa: [\detokenize\expandafter{\aa}] \\
b:  [\detokenize\expandafter{\b}]
\end{quote}


\section{Package information}
The most recent publicly released version of \pkg{trimspaces} is available at \textsc{ctan}: \url{http://tug.ctan.org/pkg/trimspaces/}. Historical and developmental versions are available at GitHub: \url{http://github.com/wspr/trimspaces/}.

While general feedback via email is welcomed, specific bugs or feature requests should be reported through the issue tracker: \url{http://github.com/wspr/trimspaces/issues}.

\subsection{Licence}

This package is freely modifiable and distributable under the terms and conditions of the \LaTeX\ Project Public License,\footnote{Available at the \LaTeX\ Project website: \url{http://www.latex-project.org/lppl/}} version 1.3c or greater (your choice). This work consists of the source file \texttt{\jobname.tex} and the derived files \texttt{\jobname.sty}, \texttt{\jobname.ins}, and the documentation \texttt{\jobname.pdf}.

This work is author-maintained by \textsc{Will Robertson}.

\section{Implementation}
\parindent=0pt
\DocInput{trimspaces.sty}

\end{document}