Announcement

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

  • Writing \$ to text file

    I am trying to write customizable tables to TeX files.

    file open output using "$path/output.tex'",write
    file write output "Try to escape \$"
    file close output

    However the resulting TeX file only says "Try to escape $" without the backslash. How can I include the backslash? "\\$" doesn't work either.


  • #2
    Code:
    file write output "Try to escape \\$"

    Comment


    • #3
      Thanks, but doesn't work for me. It omits both slashes

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Code:
        file write output "Try to escape \\$"
        Thanks, but doesn't work for me. It omits both slashes

        Comment


        • #5
          I don't know what to say. It works on my setup (Stata MP4 version 18.5, Windows 10)
          Code:
          . capture erase output.tex
          
          . file open output using "output.tex",write
          
          . file write output "Try to escape \\$"
          
          . file close output
          
          .
          . type output.tex
          Try to escape \$
          Have you tried -file write output "Try to escape \\\$"- ? That would actually make the most sense as you first escape the backslash and then escape the $. It actually makes more sense than what I showed. But for me, with \\\$, I get Try to escape \\$ in the output.

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            I don't know what to say. It works on my setup (Stata MP4 version 18.5, Windows 10)
            Code:
            . capture erase output.tex
            
            . file open output using "output.tex",write
            
            . file write output "Try to escape \\$"
            
            . file close output
            
            .
            . type output.tex
            Try to escape \$
            Have you tried -file write output "Try to escape \\\$"- ? That would actually make the most sense as you first escape the backslash and then escape the $. It actually makes more sense than what I showed. But for me, with \\\$, I get Try to escape \\$ in the output.
            Thank you. You are right, I'm able to replicate. I have written a custom program to make this type of Stata-to-TeX table making easier. It appears, the slashes are lost when I pass in the string into the custom program. I'll keep investigating. Thanks for your help. In case you are interested, below is the program. When I pass in "\\$" to the `string' local, it loses the slashes". I don't expect you to debug this, but if you have any knee-jerk reactions I would be interested.

            Code:
            cap prog drop write
            
            *Writes toa  text file
            
            program define write
            syntax,[CLOSE|NONEWLINE OPEN PATH(string) EXT(string) APPEND Filename(string)|String(string)]
            
            if missing("`nonewline'")==0{
                local newline = ""
            }
            else{
                local newline = "_n"
            }
            
            if missing("`append'")==0{
                local apprep = "append"
            }
            else{
                local apprep = "replace"
            }
            
            if missing("`ext'")==0{
                local extension = "`ext'"
            }
            else{
                local extension = "tex"
            }
            
            *Open a new file
            if missing("`open'")==0{
                global path__ "`path'"
                global filename__ "`filename'"
                cap file close
                cap file close $filename__
                file open $filename__ using "$path__/$filename__.`extension'",write `apprep'
            }
            else if missing("`close'")==0{
                file close $filename__
                macro drop path__
                macro drop filename__
            }
            else{
                if missing("$filename__")==1{
                    di `"First run with "open" command"'
                }
                else{
                    file write $filename__ "`string'" `newline'
                }
            }
            noi di `" `string' "'
            end
            Using the command is the following:
            Code:
            global p "$tabs"
            global fn "complier_chars_`samp'"
            write, open path($p) f("$fn") 
            write, s("Escape the \\$")
            write, close
            Here is the output
            Code:
            . write, open path($p) f("$fn") 
              
            
            . write, s("Escape the \\$")
             Escape the $ 
            
            . write, close

            Comment


            • #7
              Option 1:
              Code:
              write, s("Escape the \\\$")
              Option 2:
              Code:
              file write $filename__ "`macval(string)'" `newline'
              then
              Code:
              write, s("Escape the \\$")
              Note: You don't need to escape the $ if it's not followed by a character, because it can't be a macro if it's not followed by a character. This is why Clyde was surprised that \$ wasn't just escaping the $. If you wanted to write \$x, then you'd have to escape both the \ and the $.

              Comment


              • #8
                As an aside,

                Code:
                if missing("`open'")==0
                Is equivalent to

                Code:
                if !missing("`open'")
                because Stata will evaluate 0 as false. Similarly

                Code:
                if missing("$filename__")==1
                Can be written as
                Code:
                if missing("$filename__")

                Comment


                • #9
                  I'll add a few quick comments. One is that the user command texsave might be useful to you, depending on what types of tables you are trying to automate. Second, if you are trying to output a literal "\$", ie, displaying a dollar sign in LaTeX text mode, I often find it helpful to use Stata's char() commands. E.g.,
                  Code:
                  di "`=char(92)'`=char(36)'"
                  Third, if you are trying to indicate a math environment in LaTeX, note that \(...\) is an alternative syntax to $...$. It’s usually easier to use the \(...\) syntax because $ marks global macros in Stata.
                  Associate Professor of Finance and Economics
                  University of Illinois
                  www.julianreif.com

                  Comment


                  • #10
                    I think forum software might have eaten some of Julian's syntax. I'll try with code delimiters. I think Julian was pointing out that
                    Code:
                    \(...\)
                    is an alternative syntax to
                    Code:
                    $...$
                    Julian, please correct me if I misunderstood.

                    Comment


                    • #11
                      Yes, thank you Nils Enevoldsen, what you wrote is correct. In LaTeX,

                      Code:
                      \(...\)
                      is an alternative syntax to:
                      Code:
                      $...$
                      Associate Professor of Finance and Economics
                      University of Illinois
                      www.julianreif.com

                      Comment


                      • #12
                        Here are a few ways to do it. My personal favourite is to separate the backslash and dollar sign as separate strings because it's easier than having to use char() and makes clear the intent of what you actually want. It is a little cumbersome, though.

                        Code:
                        tempfile myfile
                        tempname fh
                        
                        local slashdollar "\$"
                        
                        file open `fh' using "`myfile'", write text
                        file write `fh' ("some text " + "\" + "$") _newline
                        file write `fh' ("another \" + "$") _newline
                        file write `fh' ("a third \\$") _newline
                        file close `fh'
                        type `myfile'
                        Result

                        Code:
                        . type `myfile'
                        some text \$
                        another \$
                        a third \$
                        Another approach is to make a temporary dataset where you have exactly 1 string variable, and each observation represents the lines you want to write to a file. Preparing this as a dataset avoids a lot of the hassle of escaping/macro expansion. Then you can write the dataset to a file and dispose of the temporary data.

                        Comment

                        Working...
                        X