Search code examples
matlablatexstata

Latex variable automation: writing variables for Latex directly from Stata and MATLAB


I am working on this project which requires analyzing a large (>50GB) dataset in a server, both in Stata and MATLAB. Both parts are required and I cannot use only one of them.

My ultimate goal is to generate a .tex file named something like commands.tex which looks like this:

 \newcommand{\var1}{val1}
 \newcommand{\var2}{val2}     % MATLAB file matlab_file.m on DD/MM/YYYY
 \newcommand{\var3}{val3}     % Stata file stata_file.m on DD/MM/YYYY    
 ...

where variables are ordered alphabetically and each of the values is most probably a number. Note that the commands in the comments would help me trace where did I generate the values. The usage of the file is so that after a preamble I can use LaTeX on the following way:

 <preamble>
 \input{commands.tex}
 \begin{document}
 Variable 1 has a value of \var1 and variable 2 has a value of \var2.
 \end{document}

The purpose of this is so that I can analyze locally (or remotely) a sample, say of 0.1 or 10 percent of the total observations, write a report with those, and then run the analysis again with a bigger size. I want to completely eliminate the chances of me copying a number wrong.

I am trying to write some code both in MATLAB and Stata, but I think that is beyond my expertise, and would be very grateful if someone could help me figure out how to do it. To be honest, I feel I would be able to do the MATLAB part but the Stata I have no idea.

Stata code

What I am trying to do is to generate a command that takes as an input a name and a scalar and as an output defines the corresponding variable in my commands.tex file detailed above. My goal is to be able to generate something like this:

sysuse auto
reg price weight
define_variable PriceWeight = _b[weight], format(%4.2f) 

and what I hope the code to do is that:

  1. If \newcommand{\PriceWeight} does not exist in commands.tex then it adds its value to the list, preserving the alphabetical order.
  2. If the variable exists then it deletes its value and rewrites above it, with the value given in the scalar.

I know how to give the values to a program in Stata, but I do not exactly know how to use those values and perform the necessary commands. The syntax is something like:

program define define_variable
   syntax anything = X, [format(string)]
   <other code>
end

Note: Of course, I need something way deeper than regression coefficients, but as a simple example this would suffice.

MATLAB code

This seems to be easier in MATLAB, but I do not know exactly how to automate the process. In MATLAB what I want to be able to do is something like:

clc; clear;
PriceWeight = 3
define_variable('PriceWeight',PriceWeight,format)

again where it automatically goes to the single file and updates it accordingly. Any hel[p with be very much appreciated.


Solution

  • Based on your comments and assuming that your file with all relevant variables is not huge, I would suggest getting your data from Stata to Matlab, and update your variables there as necessary (using functions such as exist or strcmp if you have a list of names). A quick google search gives me this link for Stata to Matlab.

    To make it easy to process you might want to create a cell (I will call C), where one column contains all variable names and one column contains the scalar values.

    Then, once you have assembled all your variables, you can sort your cell alphabetically and write it to a file using this.. Of course you would write a .tex file, and then iterate over your cell with something like

    fprintf(fID,'\newcommand{\%s}{%f} ',C{i,1},C{i,2})
    

    I hope this is understandable and helps.