Debian Codegraph @

texmf-dist/source/latex/csvmerge/csvmerge.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2019 by Laurence R Taylor
4 % -----------------------------------
5 %
6 % This file may be distributed and/or modified under the
7 % conditions of the LaTeX Project Public License, either version 1.3
8 % of this license or (at your option) any later version.
9 % The latest version of this license is in:
10 %
11 % http://www.latex-project.org/lppl.txt
12 %
13 % and version 1.3 or later is part of all distributions of LaTeX
14 % version 2005/12/01 or later.
15 %
16 % \fi
17 %
18 % \iffalse
19 %
20 %<*driver>
21 \documentclass{ltxdoc}
22 \usepackage{csvmerge}
23 \usepackage{hyperref}
24 \hypersetup{
25 colorlinks=true,
26 linkcolor=blue,
27 filecolor=magenta,
28 urlcolor=cyan,
29 }
30 \DisableCrossrefs
31 \CodelineNumbered
32 \RecordChanges
33 \CheckSum{525}
34 \begin{document}
35 \DocInput{csvmerge.dtx}
36 \end{document}
37 %</driver>
38 % \fi
39 %
40 %
41 % \CharacterTable
42 % {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
43 % 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
44 % Digits \0\1\2\3\4\5\6\7\8\9
45 % Exclamation \! Double quote \" Hash (number) \#
46 % Dollar \$ Percent \% Ampersand \&
47 % Acute accent \' Left paren \( Right paren \)
48 % Asterisk \* Plus \+ Comma \,
49 % Minus \- Point \. Solidus \/
50 % Colon \: Semicolon \; Less than \<
51 % Equals \= Greater than \> Question mark \?
52 % Commercial at \@ Left bracket \[ Backslash \\
53 % Right bracket \] Circumflex \^ Underscore \_
54 % Grave accent \` Left brace \{ Vertical bar \|
55 % Right brace \} Tilde \~}
56 %
57 %
58 %
59 % \changes{v1.0}{?2019?/?07?/?17?}{Initial version}
60 % \title{csvmerge}
61 % \author{Laurence R Taylor}
62 % \date{August 8, 2019}
63 % \maketitle
64 %
65 % \begin{abstract}
66 % The package |csvmerge.sty| is designed to take a bit of \TeX\ code such as a letter or a line
67 % in a table and produce
68 % multiple copies of it with parts of each copy filled in from a data file.
69 % It is invoked in the usual manner from a master-file, that is a standard
70 % \LaTeX\ file with
71 %
72 % \null\noindent\hskip 1in|\documentclass{whatever}|\\
73 % \null\hskip 1in|\usepackage{csvmerge}|\\
74 % \null\hskip 1.5in$\vdots$
75 %
76 % The data source is a standard csv file.
77 % The bit of \TeX\ code resides in a blueprint file.
78 % The |\usepackage{csvmerge}| can go anywhere in the preamble.
79 % \end{abstract}
80 % \tableofcontents
81 % \def\Hyperlink#1{\hyperlink{#1Description}{\tt \mm #1}}
82 % \def\Hypertarget#1#2{\hypertarget{#1Description}{#2}}
83 % \xdef\mm{$\backslash\hskip-5pt\backslash$}
84 % \section{Introduction}
85 %
86 % The |csvmerge.sty| merges \TeX\ code with a data file.
87 % \begin{itemize}
88 % \item
89 % The \TeX\ code comes from a blueprint file, which looks like
90 % a standard \TeX\ file except it contains no code which should
91 % go into the preamble of a normal \TeX\ file.
92 % The blueprint file should also not contain a |\begin{document}|
93 % nor an |\end{document}|. This stuff goes in the master-file.
94 % \item The data file is a standard csv file.
95 % \end{itemize}
96 % One difference between |csvmerge.sty| and other packages which
97 % merge |csv| data into a \TeX\ file is that the user does not need to do
98 % much with the |csv|-file. As long as the first row contains reasonable
99 % entries describing the contents of its column you are good to go.
100 % Extra columns are no problem; subsequent rows can be too short
101 % or too long; characters which are special to \TeX\ such as |$| and |&|
102 % can appear in the data; \dots\
103 % \noindent More details concerning the |csv|-file can be found in subsection 2.1.
104 %
105 % The |tex|-file consists of standard \TeX\ code. Each row of the |csv|-file is processed
106 % and you have access to the values of all the entries on that row as well as the row
107 % number. You also can tell if the entry in a particular column is empty.
108 % \noindent More details concerning the |tex|-file can be found in subsection 2.2.
109 %
110 % The main macro, |\mergeFields|, supports two modes.
111 % In one mode, \emph{stream mode}, the \TeX\ code from the |tex|-file
112 % is added to the input stream at the location from which |\mergeFields| is invoked.
113 % In the second mode, \emph{storage mode}, the code is written to a
114 % temporary file. You can then use the standard \LaTeX\ |\input| to read the
115 % temporary file back into the master-file.
116 %
117 % Good models for the two modes are the following. Most mail-merges can be
118 % done in streaming mode. Because \TeX\ is doing rather complicated things behind
119 % the scenes when it is setting a table or a matrix, you are usually better off using
120 % storage mode with your blueprint file containing the code for one line of your table/matrix.
121 % After the data is generated you can read it back into your table.
122 % See \hyperlink{subsubsection.B.3.1}{here} for a typical example.
123 %
124 % \newpage
125 % \section{Usage}
126 %
127 % \DescribeMacro{\mergeFields}
128 % The main macro is |\mergeFields| which takes one argument.
129 % The format is
130 %\begin{verbatim}
131 % \mergeFields{
132 % tex={<tex-file>}
133 % csv = {<csv-file>}
134 % tmp = {<tmp-file>}
135 % }
136 %\end{verbatim}
137 % The |tmp| entry is not required. If there is none, |mergeFields| operates in
138 % \emph{streaming} mode, otherwise it acts in \emph{storage} mode.
139 % The |<prefix-file>| entry should be a path to the corresponding sort of file.
140 % If the file name has spaces you will need to quote it: life is easier if there are no
141 % spaces in file names.
142 % The format |prefix = {<prefix-file>}| is quite forgiving: all leading and trailing spaces
143 % are stripped out so |prefix = { <prefix-file> }| works.
144 % The three lines can be in any order and do not even need to be on separate lines.
145 % The extensions of the three files are irrelevant: the |tex|-file contains \TeX\ code
146 % but it can have an extension of |.txt| (or anything else or none at all) if you prefer.
147 % All that matters is that
148 % when \TeX\ reads the file the contents have the proper content.
149 % If you have a simple project you may be able to complete it just by looking at
150 % the examples in \hyperlink{appendix.B}{appendix B}.
151 %
152 % \subsection{The data file}
153 % The data file is a |csv| spreadsheet file.
154 % By default the field delimiter is the comma and the
155 % text delimiter is the double-quotes character.
156 % This is default behavior for most spreadsheets when
157 % asked to produce a |cvs| file.
158 % If there are no {\tt "}'s in your |csv|-file there should be no problems.
159 % In some cases you may need a different set of delimiters
160 % and this is supported.
161 % See \Hyperlink{setDelimitersTabQuote}\ below for more details.
162 %
163 % The entries in the first row should consist solely of \emph{alphanumeric characters,
164 % punctuation and spaces}.
165 % Each entry is a \emph{label} for that column.
166 % Labels need not be unique although they usually are except sometimes there are
167 % several columns with blank labels.
168 %
169 % A \emph{field-name} is extracted from a label by deleting all spaces and making all
170 % characters lowercase: e.g. a column labeled |First Name| has a field-name
171 % |firstname|. Commas are also deleted so a column label of |Last, First Name|
172 % becomes a field-name of |lastfirstname|.
173 % Distinct labels can have the same field-name although again
174 % this seems unlikely except for blank labels.
175 %
176 % Associated to each field-name is a \emph{field-name-macro}.
177 % This macro is private: its name is found by prepending |@LRT@@| to the field-name.
178 % When |\mergeFields| is invoked, the data file is read one row at a time.
179 % All rows after the first are data.
180 % When a row is read, each entry is peeled off and placed in the field-name-macro
181 % whose field-name is the one associated to the label for that column
182 % with |@LRT@| prepended.
183 % For example, if row one of some column contains |First Name|, the field-name is |firstname| and
184 % the field-name-macro is |\@LRT@@firstname|. When a row after the first is read,
185 % |\@LRT@@firstname| will expand to the entry in the |First Name| column on that row.
186 %
187 % When |\mergeFields| has read a row and set the field-name-macros the |tex|-file is
188 % read and the \TeX\ code evaluated.
189 % In streaming mode the result is added to the input stream at
190 % the current location whereas in storage mode the result is written to the |tmp|-file.
191 %
192 % \DescribeMacro{End of csv-file}\Hypertarget{End of csv-file}
193 % This process continues until the last line in the |csv|-file is processed OR until an entirely
194 % blank line is encountered.
195 % \vskip10pt
196 % The request for only alphanumeric, punctuation and blanks for entries in row one is
197 % not actually enforced but the macro |caselower| from |stringstrings| is used to
198 % produce lowercase letters and may do weird things to characters not on the requested list.
199 % Without studying the |stringstrings| manual carefully %
200 % you may not be able to identify the field-names if you insist on weird characters.
201 %
202 % It is quite possible that several columns can have the same field-name, and hence
203 % the same field-name-macro. In that case the macro will expand to the contents of the
204 % column with that field-name which is furthest to the right.
205 %
206 % Because of the prepended |@LRT@@| you can not use these field-name-macros directly
207 % in your |tex|-file.
208 % The solution is to use \Hyperlink{Field}{}|{field-name}| which will expand correctly.
209 % See the examples in appendix B.
210 % \subsubsection{Entry issues}
211 % Except for issues discussed here, you should be able to use any
212 % standard |csv|-file.
213 %
214 % The data file is read with the usual \TeX\ issues turned off:
215 % |#|, |$|, |&|, |@|, |%|
216 % , |^|, |~|, and \verb!|!
217 % can all be in your data file.
218 % \begin{itemize}
219 % \item
220 % In streaming mode, only |#|, |$|, |&|, |@|, |%|, |^|, |~| print correctly.
221 % The characters \$, \#, \%, @ and \& often occur in data and they print correctly.
222 % \TeX\ code will not be rendered correctly so for example, |\$| in the data file produces
223 % {$``$\tt\$} when \TeX ed.
224 % As long as you are not too creative in your entries you should get the results you want.
225 % \item
226 % In storage mode the characters above end up printing correctly in the |tmp|-file
227 % BUT when you |\input| the |tmp|-file back into your master-file these characters
228 % will resume their usual \TeX\ meaning and wreak havoc with your code.
229 % If you absolutely must have them, writing |\&|, |\$|, |\#|,
230 % etc. in the |csv|-file may save you.
231 % \end{itemize}
232 %
233 % \DescribeMacro{Delimiters}\null\hskip-\parindent
234 % There is one more issue. For most csv files, the field separator is the comma.
235 % When a field has a comma in it, the entire field is delimited by double quotes ({\tt"}).
236 % When the field has a double quote it is converted to a pair of double quotes ({\tt""}) and so on.
237 % If you have both commas and quotes in a field the best case scenario is that
238 % you will get |""|'s where you were hoping for |"|'s. This probably is not what you want.
239 % Even worse, a |",| in a data entry will end the entry prematurely at this point
240 % and things deteriorate from there.
241 %
242 % There is a mechanism to deal with this. Most spreadsheets can save csv files with other
243 % field (and text) delimiters. The second most common version of csv has a tab as the
244 % field delimiter and quotes for fields which have a tab in a field.
245 %
246 % These two cases are supported out of the box. By default you get the comma-quotes version.
247 % If you add |\setDelimitersTabQuote| before calling |\mergeFields| you will get
248 % the tab-quotes version.
249 % If you have a second call to |\mergeFields| and the data file is comma-quotes,
250 % adding |\setDelimitersCommaQuote| before |\mergeFields| will restore comma-quotes.
251 % See \hyperlink{setDelimitersCommaQuoteDescription}{setDelimters\dots}
252 % for a more thorough discussion.
253 %
254 %
255 % \subsection{The blueprint file}
256 %
257 % The blueprint file is a normal \TeX\ file with macros defined as above from the |csv|-file.
258 % It should not contain a \verb!\begin{document}! or an \verb!\end{document}!
259 % nor anything that needs to be expanded in the preamble.
260 % Such things belong in the master file.
261 %
262 % For mail-merges, the |tex|-file should end with a \verb!\newpage!
263 % to guarantee that each copy of the message appears on a
264 % different page.
265 % If you need custom sizes or headers or you have page numbers that you want to
266 % reset for each copy, you should do this in the |tex|-file.
267 %
268 % To use the data from the |csv|-file you need to write |\Field{some-field-name}| wherever
269 % you want the data in your |tex|-file.
270 % Some field-names may be rather long so if you are going to use them a lot in your
271 % |tex|-file, feel free to write something like |\def\sfn{\Field{some-field-name}}| at the
272 % top of your |tex|-file and then use |\sfn| thereafter.
273 %
274 % One additional issue occurs in storage mode. You may very well want \TeX\ code in your
275 % |tex|-file. When |mergeFields| reads a line of data, you want the field-name-macros to
276 % expand to their current values but you often do not want your \TeX\ code to expand.
277 % It is good practice stick a |\noexpand| in front of each \TeX\ macro.
278 % Each line is expanded once when the line is written to the |tmp| file, so the |\noexpand|
279 % disappears and you are left with just your \TeX\ macros.
280 %
281 % If you do define macros like |\sfn| as above, you will need
282 % to write\\ |\noexpand\def\noexpand\sfn{\Field{some-field-name}}|.
283 % \StopEventually{\PrintIndex}
284 %
285 % \section{List of public macros}
286 % There are the 6 public macros: links are to descriptions of the |@LRT@|-versions which
287 % actually do the work.
288 %
289 % \null\hskip-10pt \begin{tabular}{lll}
290 % \Hyperlink{mergeFields}%
291 % &\Hyperlink{Field}%
292 % &\hbox to 0pt{\Hyperlink{ifFieldEmpty}\hss}%
293 % \\
294 % \Hyperlink{setDelimitersCommaQuote}&\Hyperlink{setDelimitersTabQuote}%
295 % &\Hyperlink{makeMePublic}\\
296 % \end{tabular}
297 % \begin{itemize}
298 % \item |\mergeFields{tex={tex-file}csv={csv-file}}| or
299 % \\|\mergeFields{tex={tex-file}csv={csv-file}{tmp={tmp-file}}|\\
300 % sets up to do the merge.
301 % \hyperlink{section.2}{Section 2} is devoted to a detailed discussion.
302 % \item |\Field{field-name}| expands to the current value of the field-name-macro.
303 % \item |\ifFieldEmpty{field-name to test}{do if empty}{do if not}|
304 % \item The two |\setDelimiters| set the delimiters to match those used in the |csv| file:
305 % comma-quotes is the default.
306 % \item |\makeMePublic| must be immediately followed by |\@LRT@| or you will get a
307 % |Use of \makeMePublic doesn't match its definition.| error. Then you can write
308 % |\makeMePublic \@LRT@foo\becomes\Foo| to make the public macro |\Foo| that
309 % you just define equal to the private macro |\@LRT@foo|. You may not write
310 % |\makeMePublic{\@LRT@foo}\becomes\Foo| or other variants: the first non-white-space
311 % character \TeX\ sees after |\makeMePublic| needs to be the start of |\@LRT@|.
312 %
313 % As a useful example,
314 % |\makeMePublic\@LRT@Row\becomes\Row| means you can get your hands on
315 % the row number using |\Row|.
316 % \end{itemize}
317 %
318 % \section{Implementation}
319 %
320 % \begin{macro}{Header}
321 % First we have the standard package header stuff.
322 % The line numbers in the typeset \jobname.dtx file match the line numbers
323 % in the \jobname.sty file
324 % generated by \TeX ing the \jobname.ins file.
325 % \setcounter{CodelineNo}{15}
326 % \begin{macrocode}
327 \NeedsTeXFormat{LaTeX2e}[2005/12/01]
328 \ProvidesPackage{csvmerge}
329 [2019/07/17 v1.0 merges TeX code with csv data]
330
331 % \end{macrocode}
332 % \end{macro}
333 % \begin{macro}{Required Packages}
334 % These required packages are part of the standard \href{https://www.tug.org/texlive/}{{\tt TeX Live}}
335 %and \href{https://miktex.org}{{\tt MiKTEX}} distributions.
336 % \begin{macrocode}
337 \RequirePackage{stringstrings}
338 \RequirePackage{trimspaces}
339 \RequirePackage{etoolbox}
340
341 % \end{macrocode}
342 % \end{macro}
343 % \begin{macro}{Options}
344 % |csvmerge| supports an option which is rarely needed. All the private macros
345 % (except \hyperlink{exceptionDescription}{one}) in
346 % this package have |@LRT@| or |@lrt@| somewhere in their name so are very
347 % unlikely to produce name conflicts. In the very unlikely chance that one or more of
348 % the six public macros has a name conflict,
349 % this can be alleviated by putting |\usepackage[foo]{csvmerge}|
350 % in your master file. Here |foo| can be any string of alphabetical characters.
351 % Then the names of all six public macros are invoked with a leading |foo|:
352 % |\mergeFields| no longer exists but becomes |\foomergeFields|. The evident change happens
353 % with the other five macros.
354 %
355 % The next two lines of code merely save the option for use
356 % \hyperlink{optionsDescription}{later} at the end.
357 % \begin{macrocode}
358 \DeclareOption*{\xdef\@LRT@CO{\CurrentOption}}
359 \ProcessOptions\relax
360
361 % \end{macrocode}
362 % \end{macro}
363 % \begin{macrocode}
364 \makeatletter
365
366 % \end{macrocode}
367 % \begin{macro}{\mergeFields}
368 % \Hypertarget{mergeFields}
369 % |\@LRT@mergeFields| actually initiates the work.
370 % It initializes some macros for later use.
371 % Then it collects the |prefix={<prefix-file>}| arguments using |\@LRT@getFilePaths|
372 % and checks to be sure it has a |csv|-file
373 % and a |tex|-file. If not it bails.
374 % Then it checks if it has a |tmp| file and sets the file reading subroutine,
375 % |\@LRT@Input| to |\@LRT@streaming| is there is no |tmp| file and to
376 % |\@LRT@storage| if there is. If there is a |tmp| file, it is opened.
377 % Then |\@LRT@mergeData| is called which actually does the merging.
378 % Finally, if there is a |tmp| file, it is closed.
379 % \begin{macrocode}
380 \newcommand{\@LRT@mergeFields}[1]{%
381 \gdef\@LRT@csv{}%
382 \gdef\@LRT@tex{}%
383 \gdef\@LRT@tmp{}%
384 \setcounter{\@LRT@macroPrefix Row}{2}\relax%
385 \gdef\@LRT@csvBlankLine{}%
386 \@LRT@getFilePaths#1=@%
387 \if@LRT@empty{\@LRT@csv}{\@LRT@missingArgumentMessage{csv}}{}%
388 \if@LRT@empty{\@LRT@tex}{\@LRT@missingArgumentMessage{tex}}{}%
389 \if@LRT@empty{\@LRT@tmp}%
390 {\global\let\@LRT@Input\@LRT@streaming}%
391 {\global\let\@LRT@Input\@LRT@storage}%
392 \if@LRT@empty{\@LRT@tmp}{}{%
393 \immediate\openout\@lrt@tmpFile=\@LRT@tmp\relax}%
394 \@LRT@mergeData%
395 \if@LRT@empty{\@LRT@tmp}{}{\immediate\closeout\@lrt@tmpFile\relax}%
396 }
397
398 % \end{macrocode}
399 % \end{macro}
400 % \Hypertarget{macroPrefix}{}
401 % \begin{macro}{\@LRT@macroPrefix}
402 % \begin{macrocode}
403 \gdef\@LRT@macroPrefix{@LRT@@}
404
405 % \end{macrocode}
406 % \end{macro}
407 % \Hypertarget{Field}{}
408 % \begin{macro}{\Field}
409 % |\@LRT@Field| is just a wrapper for a |\csname ... \endcsname|.
410 % Notice it adds the |\@LRT@macroPrefix| prefix to the argument so
411 % the input is always the field-name and the output is always the value of
412 % the corresponding field-name-macro.
413 % Because of the third |@| there is no danger that these macros defined from
414 % entries in the |csv|-file will conflict with other macros defined in this package
415 % since none of them have a |@@| in their names.
416 % \begin{macrocode}
417 \def\@LRT@Field#1{%
418 \csname\@LRT@macroPrefix#1\endcsname%
419 }
420
421 % \end{macrocode}
422 % \end{macro}
423 %
424 % \begin{macro}{\ifFieldEmpty}
425 % \Hypertarget{ifFieldEmpty}{}
426 % |\ifFieldEmpty| takes three arguments.
427 % The first argument is a field-name and if the content of the corresponding
428 % macro is empty then the second argument is executed: otherwise the
429 % third argument is executed.
430 %
431 % \begin{macrocode}
432 \long\gdef\@LRT@ifFieldEmpty#1#2#3{\ifbool{\@LRT@macroPrefix#1}{#2}{#3}}
433
434 % \end{macrocode}
435 % \end{macro}
436 %
437 % \begin{macro}{\setDelimitersCommaQuote}
438 %\end{macro}
439 % \begin{macro}{\setDelimitersTabQuote}
440 % \Hypertarget{setDelimitersCommaQuote}{}
441 % \Hypertarget{setDelimitersTabQuote}{}
442 % These two |setDelimiters| are similar.
443 % Recall how delimiters work. A row in a |csv|-file with comma delimiters
444 % looks like |foo1,foo2,foo3,...| so the delimiter is the comma and if
445 % |\@LRT@getFieldDelimited| is put in front of this string it will peal off
446 % |foo1|. Since |^^I| is \TeX\ for the tab,
447 % a row in a csv file with tab delimiters
448 % looks like |foo1^^Ifoo2^^Ifoo3^^I...| and so the first two lines of
449 % |\@LRT@setDelimitersCommaQuote| and |\@LRT@setDelimitersTabQuote|
450 % look the same after switching |,| and |^^I|.
451 %
452 % There is a wrinkle in the tab case. The tab normally looks to \TeX\ like white space and
453 % so by the time the line reaches |\@LRT@getFieldDelimited| the line looks like
454 % |foo1foo2foo3...| and there is no way to break off |foo1|. The |\catcode`\^^I12\relax|
455 % insures |\@LRT@getFieldDelimited| sees the line as |foo1^^Ifoo2^^Ifoo3^^I...|..
456 % This means that the catcode of |^^I| must also be 12 when each line is read and the line
457 % and the macro |\gdef\@LRT@catcodes{|\\|\catcode`\^^I12\relax}| insures this.
458 % In the comma delimited version no catcode heroics are needed so |\@LRT@catcodes|
459 % is empty.
460 %
461 % If some field contains one or more |\@LRT@fieldDelimiterBack|'s then the |csv|-file will use
462 % the text-delimiter at the front and back. When quote is the text delimiter and |foo2| contains
463 % a comma but |foo1| and |foo3| do not, the comma delimited string looks like
464 % |foo1,"foo2",foo3,...| and the tab delimited string looks like
465 % |foo1^^I"foo2"^^Ifoo3^^I...|.
466 %
467 % The text delimiters have a front one, the double-quotes in both versions, and the back one which is
468 % the front text delimiter followed by the back field delimiter.
469 %
470 % The macro |\@LRT@blankFieldItem| is used to construct what a blank line looks like.
471 % In the comma delimited case, you need a string of commas of the same length as
472 % the number of entries on line one of the |csv|-file. In the tab delimited case, the blank line
473 % turns out to be a blank macro.
474 %
475 % The |\@LRT@catcodes| macro is a collection of catcodes that need to be changed:
476 % nothing for the comma delimited case and |^^I| for the tab delimited case.
477 %
478 % See \hyperlink{newDelimiterAppendix}{Appendix A} for more discussion on writing your own delimiters.
479 % \begin{macrocode}
480 {{%
481 \gdef\@LRT@setDelimitersCommaQuote{
482 \gdef\@LRT@fieldDelimiterBack{,}
483 \gdef\@LRT@getFieldDelimited##1,{\@LRT@getFirstArg{##1}}
484 \gdef\@LRT@textDelimiterFront{"}
485 \gdef\@LRT@getTextDelimited"##1",{\@LRT@getFirstArg{##1}}
486 \gdef\@LRT@blankFieldItem{,}
487 \gdef\@LRT@catcodes{}
488 }%
489 }}
490
491 {{\catcode`\^^I12\relax%
492 \gdef\@LRT@setDelimitersTabQuote{
493 \gdef\@LRT@fieldDelimiterBack{^^I}
494 \gdef\@LRT@getFieldDelimited##1^^I{\@LRT@getFirstArg{##1}}
495 \gdef\@LRT@textDelimiterFront{"}
496 \gdef\@LRT@getTextDelimited"##1"^^I{\@LRT@getFirstArg{##1}}
497 \gdef\@LRT@blankFieldItem{}
498 \gdef\@LRT@catcodes{\catcode`\^^I12\relax}
499 }%
500 }}
501
502 % \end{macrocode}
503 % \end{macro}
504 %
505 % \begin{macro}{\@LRT@makeMePublic}
506 % \hypertarget{exceptionDescription}{}
507 % \hypertarget{makeMePublicDescription}{}
508 % The next line of code defines the only macro which does not have |@LRT@|
509 % in its name. It seems sufficiently weird that there should be no name conflicts.
510 % Notice that |\makeatletter| is still in effect so |@| has catcode 11.
511 %
512 % Then we switch to |\makeatother| so |@| has catcode 12.
513 % The next line expands to |\@LRT@makeMePublic| with the initial
514 % |\@LRT@| having its |@|'s of catcode 11 so it is a private macro.
515 % In the |\@LRT@| that you see, the |@|'s have catcode 12 so it is really
516 % the macro |\@| with extra stuff. Since it should never be expanded
517 % it doesn't really matter. Then we drop back into |\makeatletter| to define
518 % more private macros.
519 %
520 % The net result is that you can write \\
521 % \null\hskip1in |\makeMePublic \@LRT@foo\becomes\newMacro| \\in your master-file and
522 % the private macro |\@LRT@foo| becomes the same as |\newMacro| and you can
523 % use |\newMacro| in your |tex|-file.
524 % \begin{macrocode}
525 \expandafter\gdef\csname1238LRTLRTsvbneLRT\endcsname{@LRT@}
526 \makeatother
527 \expandafter\gdef\csname\csname1238LRTLRTsvbneLRT\endcsname
528 makeMePublic\endcsname\@LRT@#1\becomes#2{\global\letcs{#2}{@LRT@#1}}%
529 \makeatletter
530
531 % \end{macrocode}
532 % \end{macro}
533 % \begin{macro}{private macros}
534 % \end{macro}
535 % \begin{macro}{\@LRT@Row}
536 % \Hypertarget{Row}{}
537 % The macro |\@LRT@Row| expands to the number of the row of the |csv|-file currently
538 % being read. To get the |\xdef| to expand correctly we needed to define (and did)
539 % |\@LRT@macroPrefix| first. Recall that the first line of actual data is row 2.
540 % \begin{macrocode}
541 \xdef\@LRT@Row{\noexpand\number\noexpand\value{\@LRT@macroPrefix Row}}
542
543 % \end{macrocode}
544 % \end{macro}
545 % \begin{macro}{\if@LRT@empty}\Hypertarget{if@LRT@empty}{}
546 % Takes three arguments: if |#1| expands to empty do the code in |#2|;
547 % otherwise do the code in |#3|.
548 % \begin{macrocode}
549 \newcommand{\if@LRT@empty}[3]{\ifthenelse{\equal{#1}{}}{#2}{#3}}
550
551 % \end{macrocode}
552 % \end{macro}
553 % \begin{macro}{\@LRT@fieldName}
554 % Takes an alphanumeric string, uses |\caselower| from the |stringstrings| package
555 % to convert the string to all lower case; then it strips the spaces from the answer
556 % using |\convertchar|, also from the |stringstrings| package. Next it strips commas from
557 % the answer.
558 % The final |\@LRT@TheString| makes the answer global.
559 % \begin{macrocode}
560 \gdef\@LRT@fieldName#1{%
561 \caselower[q]{#1}%
562 \convertchar[q]{\thestring}{ }{}%
563 \convertchar[q]{\thestring}{,}{}%
564 \xdef\@LRT@TheString{\thestring}%
565 }
566
567 % \end{macrocode}
568 % \end{macro}
569 % \begin{macro}{\@LRT@getFilePaths}
570 % |\@LRT@getFilePaths#1=#2@| is a recursive macro which works through the
571 % |prefix={<prefix-file>}| argument list.
572 % The first argument, |#1|, picks up one |prefix={<prefix-file>}| and |#2| contains the rest of the arguments.
573 % If |#1| is not empty it is passed to |\@LRT@extractKeyValue| which collects the
574 % prefix and file path.
575 % \begin{macrocode}
576 \gdef\@LRT@getFilePaths#1=#2@{%
577 \xdef\@LRT@hashKey{\trim@spaces{#1}}%
578 \if@LRT@empty{\@LRT@hashKey}{}{
579 \def\@LRT@test{tex}\ifthenelse{\equal{\@LRT@hashKey}{\@LRT@test}}{}%
580 {\def\@LRT@test{csv}\ifthenelse{\equal{\@LRT@hashKey}{\@LRT@test}}{}%
581 {\def\@LRT@test{tmp}\ifthenelse{\equal{\@LRT@hashKey}{\@LRT@test}}{}%
582 {\@LRT@unknownArgumentMessage{#1}}}}%
583 \@LRT@extractKeyValue#2@%
584 \expandafter\@LRT@getFilePaths\@LRT@doWeStillHaveStuffToDo @}}
585
586 % \end{macrocode}
587 % \end{macro}
588 % \begin{macro}{\@LRT@extractKeyValue}
589 % |\@LRT@hashKey| contains the prefix and |\@LRT@hashValue| contains the file path.
590 % If the prefix is not one of |tex|, |csv| or |tmp| you will be warned.
591 % \begin{macrocode}
592 \def\@LRT@extractKeyValue#1#2@{%
593 \edef\@LRT@hashValue{\trim@spaces{#1}}%
594 \xdef\@LRT@doWeStillHaveStuffToDo{\trim@spaces{#2}}%
595 \expandafter\xdef\csname @LRT@\@LRT@hashKey\endcsname{\@LRT@hashValue}%
596 }
597
598 % \end{macrocode}
599 % \end{macro}
600 % \begin{macro}{\@LRT@SecondifFieldEmpty}
601 % |\@LRT@SecondifFieldEmpty| checks for an empty macro directly.
602 % The previously defined \Hyperlink{ifFieldEmpty} uses a different method so both are needed at
603 % various points in the code.
604 % \begin{macrocode}
605 \newcommand{\@LRT@SecondifFieldEmpty}[3]{%
606 \edef\@LRT@fieldValue{#1}%
607 \ifx\@LRT@fieldValue\@empty\relax{#2}\else{#3}\fi\relax}
608
609 % \end{macrocode}
610 % \end{macro}
611 % \begin{macro}{\@LRT@getCommaFieldDelimited}
612 % The macros |\@LRT@getCommaFieldDelimited|and
613 % |\@LRT@getFieldDelimited| are the same when the field delimiter is the comma.
614 % The list of field-names is always comma delimited and so
615 % we need a |\@LRT@getCommaFieldDelimited| to iterate through it.
616 % \begin{macrocode}
617 \gdef\@LRT@getCommaFieldDelimited#1,{\@LRT@getFirstArg{#1}}
618
619 % \end{macrocode}
620 % \end{macro}
621 % \begin{macro}{New counter} The counter containing the row number.
622 % \begin{macrocode}
623 \newcounter{\@LRT@macroPrefix Row}
624
625 % \end{macrocode}
626 % \end{macro}
627 % \begin{macro}{New read/write's}
628 % \begin{macrocode}
629 \newread\@lrt@csvFile%
630 \newread\@lrt@texFile%
631 \newwrite\@lrt@tmpFile%
632
633 % \end{macrocode}
634 % \end{macro}
635 % \begin{macro}{Messages}
636 % \begin{macrocode}
637 \gdef\@LRT@justProcessedMessage{%
638 \immediate\typeout{}%
639 \immediate\typeout{Just processed row %
640 \@LRT@Row\space of the data file.}%
641 \immediate\typeout{}%
642 \stepcounter{\@LRT@macroPrefix Row}\relax%
643 }
644
645 \gdef\@LRT@failedToOpenMessage#1{%
646 \immediate\typeout{}%
647 \immediate\typeout{File #1 failed to open.}%
648 \immediate\typeout{}\fi%
649 \end{document}%
650 }
651
652 \gdef\@LRT@NoMoreDataMessage{%
653 \immediate\typeout{No more data.}%
654 \immediate\typeout{}%
655 }
656
657 \gdef\@LRT@missingArgumentMessage#1{
658 \immediate\typeout{}%
659 \immediate\typeout{I was never given a path to the #1 file.}%
660 \immediate\typeout{}%
661 \end{document}%
662 }
663
664 \gdef\@LRT@unknownArgumentMessage#1{
665 \immediate\typeout{}%
666 \immediate\typeout{I do not recognize an argument of type #1.}%
667 \immediate\typeout{}%
668 }
669
670 % \end{macrocode}
671 % \end{macro}
672 % \begin{macro}{Inputs}
673 % When |mergeFields| starts up, it knows immediately whether it is in streaming or in storage
674 % mode. It sets |\@LRT@Input| to |\@LRT@streaming| or |\@LRT@storage|.
675 % Each time a new line of data in the |csv|-file is read the
676 % the macros are updated to reflect that data on the current line.
677 % |\@LRT@streaming| uses the \LaTeX\ command |\input| to add the |tex|-file to the
678 % stream.
679 % |\@LRT@storage| works through the data in the |tex|-file.
680 % First it opens the |tex| file; then it
681 % reads the |tex|-file one line at a time; expands that line; and
682 % writes the answer to a the |tmp|-file. When the |eof| for the |tex|-file is reached,
683 % the |tex|-file is closed.
684 %
685 % |\@LRT@catcodeMagic| is used in two places to set the catcodes of certain characters
686 % to values that won't cause trouble.
687 %
688 % |\@LRT@texNotAtEOF{#1}| is used to tighten up the code looping over the lines in
689 % the |tex|-file in |\@LRT@storage|.
690 % \begin{macrocode}
691 \gdef\@LRT@streaming{\input{\@LRT@tex}}
692
693 \gdef\@LRT@catcodeMagic{%
694 \catcode`\^^M9\relax%
695 \catcode`\#12\relax
696 \catcode`\$12\relax
697 \catcode`\&12\relax
698 \catcode`\^12\relax
699 \catcode`\_12\relax
700 \catcode`\%12\relax
701 }
702
703 \gdef\@LRT@texNotAtEOF#1{%
704 \ifeof\@lrt@texFile{\global\booltrue{@LRT@texEOF}}\else{#1}\fi%
705 }
706
707 \gdef\@LRT@storage{{%
708 \@LRT@catcodeMagic%
709 \openin\@lrt@texFile=\@LRT@tex\relax%
710 \@LRT@texNotAtEOF{\global\boolfalse{@LRT@texEOF}}%
711 \unlessboolexpr{bool{@LRT@texEOF}}{%
712 \read\@lrt@texFile to \@LRT@preItem%
713 \@LRT@texNotAtEOF{%
714 \immediate\write\@lrt@tmpFile{\@LRT@preItem}\relax}%
715 }{}%
716 \immediate\closein\@lrt@texFile%
717 }}
718
719 % \end{macrocode}
720 % \end{macro}
721 % \begin{macro}{\@LRT@dataLineIterator}
722 % This macro looks to see if the next character is |\@LRT@textDelimiterFront|.
723 % If it is the entry is collected using |\@LRT@getTextDelimited| and if not we collect it
724 % using |\@LRT@getFieldDelimited|.
725 % \begin{macrocode}
726 \gdef\@LRT@dataLineIterator{%
727 \expandafter\@ifnextchar\@LRT@textDelimiterFront%
728 {\@LRT@getTextDelimited}{\@LRT@getFieldDelimited}}
729
730 % \end{macrocode}
731 % \end{macro}
732 % \begin{macro}{\@LRT@getFirstArg}
733 % This is the |\@LRT@getFirstArg| from the |\@LRT@getFieldDelimited| and \\
734 % |\@LRT@getTextDelimited| macros. It collects the first field and then
735 % uses |\@LRT@getSecondArg| to collect the rest of the line from the |csv|-file.
736 % \begin{macrocode}
737 \gdef\@LRT@getFirstArg#1{\gdef\@LRT@firstArg{#1}\@LRT@getSecondArg}
738 \gdef\@LRT@getSecondArg#1@{\gdef\@LRT@restArg{#1}}
739
740 % \end{macrocode}
741 % \end{macro}
742 % \begin{macro}{\@LRT@getLOFirstArg}
743 % These next two macros do the same thing with the first line of the |csv|-file.
744 % The difference is that these two macros put their answers in a different pair of
745 % macros than the previous two so we can iterate along both the first line (field-names)
746 % and the current line of the |csv|-file.
747 % \begin{macrocode}
748 \gdef\@LRT@getLOFirstArg#1{\gdef\@LRT@ffirstArg{#1}\@LRT@getLOSecondArg}
749 \gdef\@LRT@getLOSecondArg#1@{\gdef\@LRT@frestArg{#1}}
750
751 % \end{macrocode}
752 % \end{macro}
753 % \begin{macro}{\@LRT@getLineOne}
754 % This is the version of |\@LRT@getFirstArg| which is used on the first line of the |csv|-file.
755 % \begin{macrocode}
756 \gdef\@LRT@fieldNameIterator#1,{\@LRT@getLOFirstArg{#1}}
757
758 % \end{macrocode}
759 % \end{macro}
760 % \begin{macro}{\@LRT@setFirstLineAndBlankLine}
761 % This macro collects the first line of the |csv|-file and packages it for later use:
762 % |\@lrt@firstline| is the first line when we are done.
763 % We also construct |\@LRT@csvBlankLine| which is used to check if
764 % a line of the |csv|-file is blank.
765 % \begin{macrocode}
766 \providebool{@LRT@iterator}
767 \gdef\@LRT@setFirstLineAndBlankLine#1{%
768 \gdef\@lrt@firstline{}%
769 \gdef\@LRT@csvBlankLine{}
770 \edef\@LRT@restArg{#1\@LRT@fieldDelimiterBack}
771 \booltrue{@LRT@iterator}%
772 \whileboolexpr{bool{@LRT@iterator}}{%
773 \expandafter\@LRT@dataLineIterator\@LRT@restArg @%
774 \@LRT@fieldName{\@LRT@firstArg}%
775 \xdef\@lrt@firstline{\@lrt@firstline\@LRT@TheString,}%
776 \if@LRT@empty{\@LRT@restArg}%
777 {\boolfalse{@LRT@iterator}}{\booltrue{@LRT@iterator}}%
778 \ifboolexpr{bool{@LRT@iterator}}%
779 {\xdef\@LRT@csvBlankLine{\@LRT@csvBlankLine\@LRT@blankFieldItem}}{}%
780 }{}%
781 }
782
783 % \end{macrocode}
784 % \end{macro}
785 % \begin{macro}{\@LRT@getAndProcessNextLine}
786 % This is the recursive macro which processes each line of the |csv|-file
787 % until there are no more.
788 % The code in the first |{{| \dots |}}| pair
789 % reads a line from the |csv|-file: the last line here converts
790 % the answer from local to global.
791 % The code in the second |{{| \dots |}}| pair checks
792 % if the line is \hyperlink{End of csv-fileDescription}{blank} or not.
793 % |\@LRT@exitGetAndProcessNextLine| exits the recursion and
794 % |\@LRT@repeatGetAndProcessNextLine| reloops.
795 % \begin{macrocode}
796 \gdef\@LRT@getAndProcessNextLine{%
797 \unlessboolexpr{bool{@LRT@csvEOF}}{%
798 {{%
799 \@LRT@catcodeMagic%
800 \@LRT@catcodes
801 \read\@lrt@csvFile to\@LRT@x@nextLine%
802 \ifeof\@lrt@csvFile\relax{\global\booltrue{@LRT@csvEOF}}\else%
803 {\global\boolfalse{@LRT@csvEOF}}\fi%
804 \xdef\@LRT@nextLine{\@LRT@x@nextLine}%
805 }}%
806 {{%
807 \if@LRT@empty{\@LRT@nextLine}%
808 {\global\let\@LRT@next\@LRT@exitGetAndProcessNextLine\relax}%
809 {\ifx\@LRT@csvBlankLine\@LRT@nextLine\relax%
810 {\global\let\@LRT@next\@LRT@exitGetAndProcessNextLine\relax}\else%
811 {\global\let\@LRT@next\@LRT@repeatGetAndProcessNextLine\relax}\fi}%
812 \@LRT@next%
813 }}%
814 }{}%
815 }
816
817 % \end{macrocode}
818 % \end{macro}
819 % \begin{macro}{\@LRT@exitGetAndProcessNextLine}
820 % This just exits.
821 % \begin{macrocode}
822 \gdef\@LRT@exitGetAndProcessNextLine{%
823 \closein\@lrt@csvFile\relax\global\booltrue{@LRT@csvEOF}}
824
825 % \end{macrocode}
826 % \end{macro}
827 % \begin{macro}{\@LRT@repeatGetAndProcessNextLine}
828 % This sets up to process the line we just read.
829 % \begin{macrocode}
830 \gdef\@LRT@repeatGetAndProcessNextLine{%
831 \@LRT@processNextLine{\@LRT@nextLine\@LRT@fieldDelimiterBack}}
832
833 % \end{macrocode}
834 % \end{macro}
835 % \begin{macro}{\@LRT@setMacrosForOneEntry}
836 % This bit of code sets the macro and the if-empty boolean for a single item:
837 % \#1 is the field-name and \#2 is the entry in that (row,column).
838 % \begin{macrocode}
839 \gdef\@LRT@setMacrosForOneEntry#1#2{%
840 \expandafter\xdef\csname\@LRT@macroPrefix#1\endcsname{#2}%
841 \@LRT@SecondifFieldEmpty{#2}%
842 {\global\booltrue{\@LRT@macroPrefix#1}}%
843 {\global\boolfalse{\@LRT@macroPrefix#1}}%
844 }
845
846 % \end{macrocode}
847 % \end{macro}
848 % \begin{macro}{\@LRT@processNextLine}
849 % This code actually processes the line from the |csv|-file.
850 % The first three lines are setup.
851 % The two |\edef|'s do the following: |\@LRT@restArg{#1}| has the entire current row; and
852 % |\@LRT@frestArg| has a comma separated list of field-names.
853 % Setting |\booltrue{@LRT@iterator}| means the iteration will start.
854 % The two |\expandafter| lines do the iteration: the current values are in |\ffirstArg| for
855 % the field-name and |\firstArg| for the entry in the current row.
856 % The line |\@LRT@setMacrosForOneEntry| sets the contents of the field-name-macro.
857 % Finally the
858 % |\if@LRT@empty| line checks if we are done.
859 %
860 % The |\edef| between the two |\expandafter|'s adds a blank entry to the back of
861 % |\@LRT@restArg| so if the row is too short in the |csv|-file it will still scan correctly.
862 %
863 % The |\@LRT@Input| outputs the result either to the stream or to the |tmp|-file
864 % and |\@LRT@justProcessedMessage| writes to the terminal which row was just processed.
865 % If something suddenly goes wrong you know which row you were working on
866 % when things messed up.
867 % \begin{macrocode}
868 \gdef\@LRT@processNextLine#1{%
869 \edef\@LRT@restArg{#1}%
870 \edef\@LRT@frestArg{\@lrt@firstline}%
871 \booltrue{@LRT@iterator}%
872 \whileboolexpr{bool{@LRT@iterator}}{%
873 \expandafter\@LRT@fieldNameIterator\@LRT@frestArg @%
874 \edef\@LRT@restArgA{\@LRT@restArg\@LRT@fieldDelimiterBack}
875 \expandafter\@LRT@dataLineIterator\@LRT@restArgA @%
876 \@LRT@setMacrosForOneEntry{\@LRT@ffirstArg}{\@LRT@firstArg}%
877 \if@LRT@empty{\@LRT@frestArg}%
878 {\boolfalse{@LRT@iterator}}{\booltrue{@LRT@iterator}}%
879 }{}%
880 \@LRT@Input\relax%
881 \@LRT@justProcessedMessage%
882 }
883
884 % \end{macrocode}
885 % \end{macro}
886 % \begin{macro}{\@LRT@mergeData}
887 % This macro opens the |csv|-file, reads the first row.
888 % The code between the |{{ ... }}| makes a comma separated list of field-names
889 % in the order in which they occur in the |csv|-file. It also creates a macro which
890 % expands to what a blank line in the |csv|-file will look like. It also defines one
891 % boolean for each field-name.
892 % This requires an iteration over |\@lrtfirstline|.
893 %
894 % Because \TeX\ |\newif|'s are not global, we need to define the ones we need as soon
895 % as possible, which is right here.
896 %
897 % Then |\@LRT@mergeData| calls |\@LRT@getAndProcessNextLine| which
898 % reads and processes the subsequent lines of the |csv| file.
899 %
900 % Finally it puts out a no-more-data message to let you know |\mergeFields| is finished.
901 % \begin{macrocode}
902 \gdef\@LRT@mergeData{%
903 \providebool{@LRT@texEOF}%
904 \immediate\openin\@lrt@csvFile=\@LRT@csv\relax%
905 \providebool{@LRT@csvEOF}%
906 \boolfalse{@LRT@csvEOF}%
907 \ifeof\@lrt@csvFile\@LRT@failedToOpenMessage{\@LRT@csv}\fi%
908 {{\@LRT@catcodes
909 \read\@lrt@csvFile to\@lrt@firstlineX%
910 \@LRT@setFirstLineAndBlankLine{\@lrt@firstlineX}
911 }}%
912 \booltrue{@LRT@iterator}
913 \edef\@LRT@restArg{\@lrt@firstline}%
914 \whileboolexpr{bool{@LRT@iterator}}{%
915 \expandafter\@LRT@getCommaFieldDelimited\@LRT@restArg @%
916 \providebool{\@LRT@macroPrefix\@LRT@firstArg}%
917 \if@LRT@empty{\@LRT@restArg}%
918 {\boolfalse{@LRT@iterator}}{\booltrue{@LRT@iterator}}%
919 }{}%
920 \@LRT@getAndProcessNextLine%
921 \@LRT@NoMoreDataMessage%
922 }
923
924 % \end{macrocode}
925 % \end{macro}
926 % \Hypertarget{options}{}
927 % \begin{macro}{Options code}
928 % Here is the code which
929 % examines the option string and maps the public macro names to the
930 % private codes which actually do the work.
931 % \begin{macrocode}
932 \gdef\@LRT@weirdOption#1{\@ifundefined{@LRT@CO}%
933 {\global\csletcs{#1}{@LRT@#1}}{{\global\csletcs{\@LRT@CO #1}{@LRT@#1}}}%
934 }
935
936 \@LRT@weirdOption{mergeFields}%
937 \@LRT@weirdOption{Field}%
938 \@LRT@weirdOption{ifFieldEmpty}%
939 \@LRT@weirdOption{setDelimitersCommaQuote}%
940 \@LRT@weirdOption{setDelimitersTabQuote}%
941 \@LRT@weirdOption{makeMePublic}%
942
943 % \end{macrocode}
944 % \end{macro}
945 % \begin{macro}{Set the default delimiters}
946 % Here \Hypertarget{defaults}{is}
947 % where the default delimiters are set.
948 % \begin{macrocode}
949 \@LRT@setDelimitersCommaQuote
950
951 % \end{macrocode}
952 % \end{macro}
953 % \begin{macro}{Finish up}
954 % \begin{macrocode}
955 \makeatother
956 \endinput
957 % \end{macrocode}
958 % \end{macro}
959 % \Finale
960 % \newpage
961 % \appendix
962 % \section{Writing new delimiter macros}\hypertarget{newDelimiterAppendix}{}
963 % If the field delimiter is not something \TeX\ sees as white space, life is fairly easy.
964 % In your master-file, copy and paste one of the code examples below.
965 % Change to suit your needs.
966 %
967 % \noindent***********************************************************************
968 %
969 % \begin{verbatim}
970 % {{%
971 % \makeatletter
972 % \gdef\setMyDelimiters{%
973 % \gdef\@LRT@fieldDelimiterBack{,}
974 % \gdef\@LRT@getFieldDelimited##1,{\@LRT@getFirstArg{##1}}
975 % \gdef\@LRT@textDelimiterFront{"}
976 % \gdef\@LRT@textDelimiterBack{",}
977 % \gdef\@LRT@getTextDelimited"##1",{\@LRT@getFirstArg{##1}}
978 % \gdef\@LRT@blankFieldItem{,}
979 % \gdef\@LRT@catcodes{}
980 % }%
981 % \makeatother
982 % }}
983 % \end{verbatim}
984 %
985 % \noindent***********************************************************************
986 %
987 % Replace all occurrences of {\tt ,} by your field delimiter. Replace all occurrences of {\tt "}
988 % with your text deliminator
989 % and you should be ready to go.
990 % Just add |\setMyDelimiters| in your master-file before using |\mergeFields|.
991 %
992 % If the field delimiter is a white space character such as a tab, then copy and paste
993 % the next example
994 %
995 % \noindent***********************************************************************
996 %
997 % \begin{verbatim}
998 % \makeatletter
999 % {{\makeatletter
1000 % \catcode`\^^I12\relax%
1001 % \gdef\@LRT@setDelimitersTabQuote{
1002 % \gdef\@LRT@fieldDelimiterBack{^^I}
1003 % \gdef\@LRT@getFieldDelimited##1^^I{\@LRT@getFirstArg{##1}}
1004 % \gdef\@LRT@textDelimiterFront{"}
1005 % \gdef\@LRT@textDelimiterBack{"^^I}
1006 % \gdef\@LRT@getTextDelimited"##1"^^I{\@LRT@getFirstArg{##1}}
1007 % \gdef\@LRT@blankFieldItem{}
1008 % \gdef\@LRT@catcodes{\catcode`\^^I12\relax}
1009 % }%
1010 % }}
1011 % \makeatother
1012 % }}
1013 % \end{verbatim}
1014 %
1015 % \noindent***********************************************************************
1016 %
1017 % Use Chapter 8 of the \TeX\ book to find how to write your delimiter and replace
1018 % all occurrences of |\^^I| with the code for your delimiter.
1019 % Then replace all occurrences of {\tt "} with your text deliminator
1020 % and hope for the best.
1021 % Try leaving |\@LRT@blankFieldItem| blank and hope for the best.
1022 % Again add |\setMyDelimiters| in your master-file before using |\mergeFields|.
1023 %
1024 % \section{Some examples}
1025 % \subsection{\TeX\ code in a table}
1026 % A typical line in a tabular environment looks like |item1 & item2& item3\\|.
1027 % Suppose you have 4 columns in your |csv|-file with entries |Last Name|,
1028 % |First name|, |Numerator| and |Denominator| and you want |item1| to be
1029 % |Last Name|, |item2| to be |First name| and |item3| to be |\frac{Numerator}{Denominator}|.
1030 % Here is the needed code in your |tex|-file:
1031 %
1032 % \begin{verbatim}
1033 % \Field{lastname}&\Field{firstname}&%
1034 % $\noexpand\frac{\Field{numerator}}{\Field{denominator}}$\noexpand\\
1035 % \end{verbatim}
1036 % You might have expected to see a |\noexpand| in front of the \$'s but remember \$ is one of
1037 % those characters which goes through unscathed to the |tmp|-file.
1038 % It certainly doesn't hurt to write |\noexpand$|. When you read the |tmp|-file back in
1039 % you do want |$| to resume its meaning of making math mode and it does.
1040 %
1041 % The |\noexpand| in front of the |\frac| and the |\\| are needed.
1042 % You don't want to try to expand either one
1043 % until the |tmp|-file is read back in.
1044 %
1045 % Assuming that the file |merge.tex| contains the code above and that the file |data.csv| contains your data,
1046 % the following code typesets the table.
1047 % \begin{verbatim}
1048 % \documentclass[12pt]{article}
1049 % \RequirePackage{csvmerge}
1050 % \begin{document}
1051 % \mergeFields{
1052 % tmp={example.tmp}
1053 % tex ={merge.tex}
1054 % csv = {data.csv}
1055 % }
1056 % \begin{tabular}{lll}
1057 % \input{example.tmp}
1058 % \end{tabular}
1059 % \end{document}
1060 % \end{verbatim}
1061 % The |csv|-file needs to contain the four field-names that appear in the |tex|-file,
1062 % but it is fine if it contains additional columns.
1063 %
1064 % \subsection{\TeX\ code in a mail-merge}
1065 % Here is a snippet of code to do a header in a mail-merge business letter.
1066 % The field-names from the |csv|-file should be obvious. The interesting point
1067 % is the usage of |\ifFieldEmpty|.
1068 % \begin{verbatim}
1069 % \Field{firstname} \ifFieldEmpty{middleinitial}{}
1070 % {\Field{middleinitial}.~}\Field{lastname}\par
1071 % \ifFieldEmpty{streetline1}{}{\Field{streetline1}\par}
1072 % \ifFieldEmpty{streetline2}{}{\Field{streetline2}\par}
1073 % \ifFieldEmpty{streetline3}{}{\Field{streetline3}\par}
1074 % \Field{city}, \Field{stateprovince} \Field{postalcode}
1075 % \end{verbatim}
1076 %
1077 % The |middleinitial| column is a middle initial with no period. Some recipients will have no middle initial
1078 % but you don't want just a floating period. Also you don't want an additional space.
1079 % Knuth also suggests that names should be \TeX ed with |First M.~Last| [The \TeX\ book, Ch. 6].
1080 % The |\ifFieldEmpty| with |streetline1| is probably redundant but often |streetline2| will be empty.
1081 % If it is not, we want a new line after it, but if it is empty we don't want a blank new line.
1082 %
1083 % In storage mode you need a |\noexpand| before the |~|, as well as before the |\par|'s.
1084 % \subsection{Some master-files}
1085 % \subsubsection{A table}
1086 % We are doing a table so we want storage mode.
1087 % We also want a horizontal line after each row of the table.
1088 % The master-file:
1089 % \begin{verbatim}
1090 % \documentclass[12pt]{amsart}
1091 % \RequirePackage{csvmerge}
1092 % \begin{document}
1093 % \mergeFields{
1094 % tmp= { ./table.tmp}
1095 % csv = {./data.csv}
1096 % tex ={./table.tex }
1097 % }
1098 % \begin{tabular}{|l|l|l|l|l|}
1099 % \hline%
1100 % \input{./table.tmp}
1101 % \end{tabular}
1102 % \end{document}
1103 % \end{verbatim}
1104 %
1105 % The |tex|-file |table.tmp|:
1106 % \begin{verbatim}
1107 % % one table line
1108 % \Field{firstname} &\Field{id} & \Field{city} &\Field{lastname}&
1109 % \Field{postalcode} \noexpand\\
1110 % \noexpand\hline
1111 % \end{verbatim}
1112 %
1113 % \subsubsection{A mail-merge}
1114 % We are doing a mail-merge so we can use streaming mode.
1115 % Since the |tex|-file name has spaces, we have quoted it.
1116 % The master-file:
1117 % \begin{verbatim}
1118 % \documentclass[12pt]{letter}
1119 % \RequirePackage{./csvmerge}
1120 % \begin{document}
1121 % \mergeFields{ tex = {"./Second letter.template" }
1122 % csv ={ ./Invites.csv}}
1123 % \end{document}
1124 % \end{verbatim}
1125 %
1126 % The |tex|-file |Second letter.template|:
1127 %
1128 % \begin{verbatim}
1129 % %First page of letter
1130 % \setcounter{page}{1}
1131 %
1132 % \Field{firstname} \ifFieldEmpty{middleinitial}{}%
1133 % {\Field{middleinitial}.~}\Field{lastname}\par
1134 % \ifFieldEmpty{streetline1}{}{\Field{streetline1} \par}
1135 % \ifFieldEmpty{streetline2}{}{\Field{streetline2}\par}
1136 % \ifFieldEmpty{streetline3}{}{\Field{streetline3}\par}
1137 % \Field{city}, \Field{stateprovince} \Field{postalcode}
1138 %
1139 % \vskip.25in
1140 % Dear \Field{preferredfirstname},\par
1141 % \vskip10pt
1142 % How come no RSVP?
1143 % \vskip10pt
1144 % Bilbo Baggins
1145 % \newpage
1146 % \end{verbatim}
1147 %
1148 % Here is an equivalent version using storage mode.
1149 % The master-file:
1150 % \begin{verbatim}
1151 % \documentclass[12pt]{letter}
1152 % \RequirePackage{./csvmerge}
1153 % \begin{document}
1154 % \mergeFields{ tex = {"./Second letter.template" }
1155 % csv ={ ./Invites.csv}tmp={letters}}
1156 % \input{letters}
1157 % \end{document}
1158 % \end{verbatim}
1159 %
1160 % We must adjust the |tex|-file |Second letter.template|:
1161 %
1162 % \begin{verbatim}
1163 % %First page of letter
1164 % \noexpand\setcounter{page}{1}
1165 %
1166 % \Field{firstname} \ifFieldEmpty{middleinitial}{}%
1167 % {\Field{middleinitial}.\noexpand~}\Field{lastname}\noexpand\par
1168 % \ifFieldEmpty{streetline1}{}{\Field{streetline1} \noexpand\par}
1169 % \ifFieldEmpty{streetline2}{}{\Field{streetline2}\noexpand\par}
1170 % \ifFieldEmpty{streetline3}{}{\Field{streetline3}\noexpand\par}
1171 % \Field{city}, \Field{stateprovince} \Field{postalcode}
1172 %
1173 % \noexpand\vskip.25in
1174 % Dear \Field{preferredfirstname},\par
1175 % \noexpand\vskip10pt
1176 % How come no RSVP?
1177 % \noexpand\vskip10pt
1178 % Bilbo Baggins
1179 % \noexpand\newpage
1180 % \end{verbatim}
1181 %
1182 % Usually using storage mode when you don't need it is wasteful since you have to create and
1183 % save a |tmp|-file. It can come in handy if your letter code has lots of field macros and
1184 % you can't get one |tex|-file to not have overfull/underfull boxes and the like after the
1185 % various expansions. The |tmp|-file contains all the letters and you can fine tune each one and
1186 % then just do
1187 % \begin{verbatim}
1188 % \documentclass[12pt]{letter}
1189 % \begin{document}
1190 % \input{letters}
1191 % \end{document}
1192 % \end{verbatim}
1193
1194
1195
1196
1197
1198