Announcement

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

  • Stata commands in Python

    Dear all,

    I am using Ben Jann's -texdoc- to write some text about the integration of Python in Stata. In this text, there is the following structure:

    Code:
    Some LaTeX  that explains Stata and Python. 
    
    ***/
    texdoc stlog
    python
    from sfi import Data
    end
    texdoc stlog close 
    /***
    
    some following up LaTeX 
    
    ***/
    texdoc stlog
    python
    myname = Data.get("ybirth")
    end
    texdoc stlog close 
    /***
    This works in the sense that it runs withouth errors. However, in the PDF that is finally produced, I get:

    Some LaTeX that explains Stata and Python.

    . python
    . from sfi import Data
    . end

    some following up LaTeX

    . python
    . myname = Data.get("ybirth")
    . end

    Instead of this, I would like -python- to be called only once at the beginning, and -end- only once at the end. Is there a way to achieve this?

    My idea was to use -stata.run()- from -pystata- to issue the commands -texdoc stlog close- and -texdoc stlog- from the python environment:

    Code:
    . python
    >>> import stata_setup
    >>> stata_setup.config('/usr/local/stata18','se')
    >>> from pystata import stata
    >>> stata.run("texdoc stlog")
    but stata.run() crashes the running Stata 18 instance (Linux Ubuntu). I guess -pystata- just isn't meant to be run from within a python environment started within Stata.

    Does anybody have another solution? (This is a larger project that went quite far already, so working from Python completely, or getting rid of -texdoc- is not really an option)

    Many regards
    Uli







  • #2
    Maybe a brute-force add "tags" and (regex) search-replace in *.log.tex files might be considered if you do not find another solution, some variant of:
    Code:
    texdoc stlog
    * remove_python 
    python
    2 + 2
    print("remove_end")
    end
    texdoc stlog close
    .log.tex
    Code:
    . type "`fn'"
    . * remove_python 
    . python
    \HLI{47} python (type {\bftt{end}} to exit) \HLI{81}
    >>> 2 + 2
    4
    >>> print("remove_end")
    remove_end
    >>> end
    \HLI{155}
    {\smallskip}
    Example remove both "python" and "end":
    Code:
    local fn "myexample_3.log.tex"
    
    qui di filewrite("`fn'", ///
        ustrregexrf(fileread("`fn'"),  ///
            "(?ms)\* remove_python.+?(?:(?!>>>).)*","") ///
        , 1 ////
        )
        
    qui di filewrite("`fn'", ///
        ustrregexrf(fileread("`fn'"),  ///
            "(?ms)^>>>.*remove_end.+end.*?$.","") ///
        , 1 ///
        )
     
    cat "`fn'"
    Code:
    . >>> 2 + 2
    4
    \HLI{155}
    {\smallskip}

    Comment


    • #3
      or add option to texdoc.ado, similar to stlogoption matastrip (see mata functions _texdoc_striplog_mata() and _texdoc_striplog_mata_end() )

      Comment


      • #4
        Dear Bjarte, thank you very much for this. This is awesome!

        Comment

        Working...
        X