Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Writing scalar values form Stata into latex document

    It is possible to include a scalar value from stata into a latex document?

  • #2
    How are you building the LaTeX document? If you are using estout it has functionality to add scalar values to the tables. If you're doing things manually, you can try and if not you might be able to trick Stata into evaluating it as a local macro:

    Code:
    `= scalarName'

    Comment


    • #3
      Sure - you can insert it similar to how you might reference/insert values from a variable or a macro. You can use `=scalar' to refer to value. I'm guessing you're either using a program like -texdoc- (from SSC) or the -file- commands to write your document. Here's an example:

      Code:
      sysuse auto, clear
      scalar x = 99
      forval n = 1/10 {
           scalar n`n' =  `"`=make[`n']'"'
          }
      scalar li
      
      
      *could use file open/write commands but I prefer:
        texdoc init ex.tex, replace
      tex  \documentclass{article}
      tex \begin{document}
      tex \begin{table}[ht]
      tex \caption{something here}
      tex \begin{center}
      tex \begin{tabular}{|c|c|} 
      tex \hline Car make & Value \\ \hline \hline
      forval n = 1/10 {
      tex `=n`n'' & `=x' \\ 
      }
      tex \hline \hline
      tex \end{tabular}
      tex \end{center}
      tex  \end{table}%
      tex  Here is some text referring to `=x' value and `=n2'
      tex \end{document}
      texdoc close
      Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

      Comment


      • #4
        (I missed that buchanan's offered similar advice when I was writing my post.)
        One other point is that referencing the scalar using `=scalarname' works as long as scalarname is not already a variable name. In that case, using that syntax will reference the first observation of the variable , not the scalar. (that is, `=varname'/`=scalarname' is in that case equivalent to `=varname[1]') .

        Consider the examples below:

        Code:
        clear
        set obs 3
        
        scalar x = "scalar"
        di `"`=x'"'
        di x
        
        
        //now gen vars using scalar 'x'
        g a1 = x  //from scalar
        g a2 = `"`x'"' //this wont work yet
        g a3 = `"`=x'"' //from scalar
        l a* , div
        
        //now gen vars and a local 'x'
        local x  "local"
        g x = "var"
        replace x = "var1st" in 1 //to see the difference
        scalar li
        di `"`x'"'
        di `"`=x'"'
        di `"`=x[1]'"'
        di `"`=x[2]'"'
        di x
        
        
        g b1 = x  //now from variable
        g b2 = `"`x'"'  //now from local
        g b3 = `"`=x'"' //now from only the first obs of variable x
        l x a* b* , div  //examine the diff between a* and b*
        Last edited by eric_a_booth; 26 Feb 2016, 08:05.
        Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

        Comment


        • #5
          I really appreciate your very complete answers. They really answered my question for sure.

          I would like to add that the "textdoc" function looks awsome, really cool and exactly what I am looking for. I have always been working with a textmaker-do.file combination. In that way I have never worked with a more dynamic document.

          What is your prefered combination tex editor - stata compiler? To work in a more dynamic-document shape.

          Regards,
          Fernando

          Comment

          Working...
          X