-------------------------------------------------------------------------------
help for rewrite                                                    (Rosa Gini)
-------------------------------------------------------------------------------

Rewrite text files from disk performing macro substitutions

Syntax

rewrite modelfile using finalfile , {replace|append} [ tex html ]

modelfile must be the name of a text file.

Description

rewrite copies modelfile to finalfile. The difference with copy is that modelfile is not copied verbatim: rewrite performs macro substitution on modelfile using the values of global macros that are valid for the current Stata session. rewrite automatizes the tip contained in (Gini 2008). As an option, html or LaTeX special characters are also substituted.

Options

replace specifies that finalfile be replaced if it already exists, append specifies that the rewritten file will be appended to finalfile. If finalfile doesn't exist, either option will create it.

tex causes rewrite to substitute to some character that LaTeX considers special their corresponding LaTeX commands. html does the same thing for html.

Remarks

rewrite is mainly thought of as an instrument to manipulate code of various programming languages (sql, html, tex): the document in modelfile is a piece of language code containing some global macros. Before executing the code rewrite substitutes the active values of the global macros, so that finalfile contains valid programming language code and can be executed-possibly from within the same Stata session by means of the shell command. A very suitable programming language for this procedure is Stata's language itself.

A second use is for generating small texts in html and tex starting form the same text file.

Examples

. rewrite "my document" using "rewritten document", replace

. global year=2007 . rewrite do_file_with_some_macros.txt using finalfile.do, replace . do finalfile.do

. global year=2007 . rewrite headertex.txt using text.tex, replace . rewrite text.txt using text.tex, tex append . rewrite foottex.txt using text.tex, append . shell pdflatex text.tex . shell acroread text.pdf

. forvalues year=2000/2007{ . global year=`year' . rewrite code_with_macros.sql using query.sql, append . } . shell sqlplus username/psswd@servername @query.sql

Author

Rosa Gini, Agenzia regionale di sanità della Toscana, Italy. Email: rosa.gini@arsanita.toscana.it

References

Gini, R. 2008. Stata tip 56: Writing parameterized text files. The Stata Journal 8(1): 134-136.