Hello everyone.
Basic stuff: I'm using a Mac, Stata/MP 13.1 version, and this is my first post.
Well, the basic idea is that I want to link Stata outputs automatically to LaTeX (for my dissertation). My approach is to write a LaTeX-friendly log file, in which I could specify global macros that LaTeX could find anywhere in the document I'm writing.
For example,
This will end up with a log file that I can later use it as an \input{outputstata.txt} in LaTeX, and therefore, write a document with the global \@price (commas included), instead of writing the number on my own and coming back and forth to Stata and check if my answer has changed given my new procedures.
Until here, I found that there is no problem at all and I have successfully compile this log file in LaTeX. Hooray!
My problem came when I wanted to check if that global has been previously defined, so I could replace it with the new value. That is, I can't just use the before procedure to write an ado-file such as:
With macros `name' and `value' because, as it is, this will only add and add and add new lines into my log file, even if I have already defined that same LaTeX global before.
So, what I found is that there is a file command, with which you can read and write to and from any file.
Therefore, my approach was the following,
What I do first is to check if I want a brand new log file. If I don't, then I will open the previously used log file, named here outputstata.txt. Then, I will check if I have already written a LaTeX global under the name `name'. I compare the complete line, to avoid the use of substr() or other string functions. If I do find a previous global, I will like to replace the value of `name' with the new `value'. I assume that if I found the first line, the second one will be the one I'm looking for replacement.
AND HERE IS THE PROBLEM, this procedure does not replace the line completely. It stops at the first comma. How should I tell Stata that I want to whole line to be replaced? And more over, I formerly tried to replace the first line too, but I encountered that I could not write on the exact line in which I though I was, but the next one.
So, any ideas of how can I code this ado-file with successful results? Maybe I'm not thinking it correctly from the first place, I there could be an easier more efficient and simple procedure to do. But that is what I come up with. So, any help is appreciated.
Thanks a lot!
Basic stuff: I'm using a Mac, Stata/MP 13.1 version, and this is my first post.
Well, the basic idea is that I want to link Stata outputs automatically to LaTeX (for my dissertation). My approach is to write a LaTeX-friendly log file, in which I could specify global macros that LaTeX could find anywhere in the document I'm writing.
For example,
Code:
sysuse auto, clear mean price matrix R = r(table) * I will write a LaTeX-friendly log file with the result previously found quietly log using outputstata.txt di in w "\def\price#1{\gdef\@price{#1}}" // LaTeX code for defining a command di in w `"\price{`=string(`=R[1,1]',"%20.0fc")'}"' // LaTeX code for declaring the values of the command quietly log close
Until here, I found that there is no problem at all and I have successfully compile this log file in LaTeX. Hooray!
My problem came when I wanted to check if that global has been previously defined, so I could replace it with the new value. That is, I can't just use the before procedure to write an ado-file such as:
Code:
outputlatex, name(price) value(`=R[1,1]')
So, what I found is that there is a file command, with which you can read and write to and from any file.
Therefore, my approach was the following,
Code:
program define outputlatex syntax , NAME(string) VALue(string) [NEW] * Open * if "`new'" == "new" { quietly log using "$results/outputstata.txt", name(latex) replace text } else { quietly log using "$results/outputstata.txt", name(latex) append text } * Check if there is a previous global * tempname myfile file open `myfile' using "$results/outputstata.txt", read write file read `myfile' line while r(eof) == 0 { if `"`line'"' == "\def\\`name'#1{\gdef\@`name'{#1}}" { file write `myfile' `"\\`name'{`=string(`value',"%20.0fc")'}"' _n * Close * file close `myfile' quietly log close latex exit } file read `myfile' line } file close `myfile' * If nothing was found * di in w "\def\\`name'#1{\gdef\@`name'{#1}}" di in w `"\\`name'{`=string(`value',"%20.0fc")'}"' * Close * quietly log close latex end
AND HERE IS THE PROBLEM, this procedure does not replace the line completely. It stops at the first comma. How should I tell Stata that I want to whole line to be replaced? And more over, I formerly tried to replace the first line too, but I encountered that I could not write on the exact line in which I though I was, but the next one.
So, any ideas of how can I code this ado-file with successful results? Maybe I'm not thinking it correctly from the first place, I there could be an easier more efficient and simple procedure to do. But that is what I come up with. So, any help is appreciated.
Thanks a lot!
Comment