title: Latex variable automation: writing variables for Latex directly from Stata and MATLAB
body: <p>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.</p>
  
  <p>My ultimate goal is to generate a .tex file named something like <code>commands.tex</code> which looks like this:</p>
  
  <pre><code> \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    
   ...
  </code></pre>
  
  <p>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:</p>
  
  <pre><code> &lt;preamble&gt;
   \input{commands.tex}
   \begin{document}
   Variable 1 has a value of \var1 and variable 2 has a value of \var2.
   \end{document}
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <h1>Stata code</h1>
  
  <p>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 <code>commands.tex</code> file detailed above. My goal is to be able to generate something like this:</p>
  
  <pre><code>sysuse auto
  reg price weight
  define_variable PriceWeight = _b[weight], format(%4.2f) 
  </code></pre>
  
  <p>and what I hope the code to do is that:</p>
  
  <ol>
  <li>If <code>\newcommand{\PriceWeight}</code> does not exist in <code>commands.tex</code> then it adds its value to the list, <em>preserving the alphabetical order</em>.</li>
  <li>If the variable exists then it deletes its value and rewrites above it, with the value given in the scalar.</li>
  </ol>
  
  <p>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:</p>
  
  <pre><code>program define define_variable
     syntax anything = X, [format(string)]
     &lt;other code&gt;
  end
  </code></pre>
  
  <p>Note: Of course, I need something way deeper than regression coefficients, but as a simple example this would suffice.</p>
  
  <h1>MATLAB code</h1>
  
  <p>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:</p>
  
  <pre><code>clc; clear;
  PriceWeight = 3
  define_variable('PriceWeight',PriceWeight,format)
  </code></pre>
  
  <p>again where it automatically goes to the single file and updates it accordingly. Any hel[p with be very much appreciated.</p>