Announcement

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

  • markdown not showing script - foreach statement

    Hello,

    I am using STATA markdown. I am getting HTML output through 'dyndoc script.txt' command.

    When I write down below codes, then the code below foreach x in `vars2' { is not showing up on the HTML of code block in which foreach is included (I attached screenshot.)
    It happens whenever I use foreach statement in markdown.

    Code:
    ~~~~
    <<dd_do: nooutput>>
    local vars2 stp_w_d stp_c_d
    foreach x in `vars2' {
    histogram `x', width(0.1) frequency addlabel
    graph export `x'_dist.svg, replace
    }
    <</dd_do>>
    ~~~~

    Output:
    Click image for larger version

Name:	Screenshot 2023-01-13 122025.png
Views:	1
Size:	4.0 KB
ID:	1697184

    What should I do to show whole codes in HTML output?

  • #2
    Honestly I had no idea this existed. That said, I can reproduce your issue in Stata 17.

    Code:
    ~~~~
    <<dd_do: nooutput>>
    clear
    input int(var1 var2 var3)
    1 1 1
    2 2 1
    3 3 2
    4 4 2
    5 5 3
    end
    
    sum var1 var2 var3
    foreach var in var*{
        sum `var'
    }
    
    sum var1
    <</dd_do>>
    ~~~~
    Code:
    dyndoc "test stata markdown.txt", replace
    I am going through the documentation, and it definitely looks like the nooutput option should give you just the code without the output. The problem is that Stata appears to consider anything within a code block (notably, including my input command) output. The above works as expected when the nooutput option is removed.

    As to how to "fix" this: maybe someone else has a better answer, but I I would honestly just go into the html file and edit it manually. I notice that the way Stata writes the code to html isn't all that smart or sophisticated. It doesn't produce html tags, there is no doctype, no body tags, no formatting as far as I can tell, and so on. Really, it looks like it just wraps code and output in <pre><code> tags and text in <p> tags. Assuming you actually want to suppress the output, Here is the html that I expect you want for the code block above:

    Code:
    <pre><code>local vars2 stp_w_d stp_c_d
    foreach x in `vars2' {
    histogram `x', width(0.1) frequency addlabel
    graph export `x'_dist.svg, replace
    }</code></pre>
    edit: forgot to paste in the tags! Fixed the html above.
    Last edited by Daniel Schaefer; 13 Jan 2023, 12:55.

    Comment


    • #3
      Originally posted by Daniel Schaefer View Post
      Honestly I had no idea this existed. That said, I can reproduce your issue in Stata 17.

      Code:
      ~~~~
      <<dd_do: nooutput>>
      clear
      input int(var1 var2 var3)
      1 1 1
      2 2 1
      3 3 2
      4 4 2
      5 5 3
      end
      
      sum var1 var2 var3
      foreach var in var*{
      sum `var'
      }
      
      sum var1
      <</dd_do>>
      ~~~~
      Code:
      dyndoc "test stata markdown.txt", replace
      I am going through the documentation, and it definitely looks like the nooutput option should give you just the code without the output. The problem is that Stata appears to consider anything within a code block (notably, including my input command) output. The above works as expected when the nooutput option is removed.

      As to how to "fix" this: maybe someone else has a better answer, but I I would honestly just go into the html file and edit it manually. I notice that the way Stata writes the code to html isn't all that smart or sophisticated. It doesn't produce html tags, there is no doctype, no body tags, no formatting as far as I can tell, and so on. Really, it looks like it just wraps code and output in <pre><code> tags and text in <p> tags. Assuming you actually want to suppress the output, Here is the html that I expect you want for the code block above:

      Code:
      <pre><code>local vars2 stp_w_d stp_c_d
      foreach x in `vars2' {
      histogram `x', width(0.1) frequency addlabel
      graph export `x'_dist.svg, replace
      }</code></pre>
      edit: forgot to paste in the tags! Fixed the html above.
      Thanks Daniel. It seems your answer putting <pre><code> is the best option for now.
      I checked and that works perfectly.
      I hope STATA fix that problem. Maybe I will email them.

      Comment

      Working...
      X