Announcement

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

  • Adding images to Word with Stata having their size value depending on a variables value

    Dear all

    I am using Stata 15.0 and am looking for a way to define the # in 'putdocx image imagename.png, height(#[unit])' as a variable value rather than a fix number. Is this possible? I tried workarounds like tabulating variables prior to running 'putdocx image imagename.png, height(#[unit])' and replacing the '#' with 'r(min)' (which gives an error; option height not specified correctly) or even defining a 'local var=r(min)' and running the command 'putdocx image imagename.png, height(`$var')' (which doesnt give an error but seems to be ignored, so the images are added to word in default size).

    Data example is really simple here, yet I'll give one:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float sports
    83.91846
    83.91846
    83.91846
    83.91846
    83.91846
    end
    So let's say I want the image height to be 83.91846 (or 84 after rounding sports variable) pt. But variable, as image sizes will refer to values of different variables throughout do-File.

    Thanks in advance for your help!

  • #2
    Consider the following example run on your data.
    Code:
    . summarize sports
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
          sports |          5    83.91846           0   83.91846   83.91846
    
    . local num `r(min)'
    
    . display "the minimum is `num'"
    the minimum is 83.91845703125
    Four things to note.

    1) You didn't tell us what command you used that gave you the returned value r(min) so I supplied the summarize command, which does so.

    2) Because r(min) will be destroyed if any intermediate command returns values in r(), I save its value in a new local macro which I call num.

    3) This is crucial. You wrote
    Code:
    replacing the '#' with 'r(min)'
    but what you really needed was
    Code:
    `r(min)'
    noting that the opening single quote is a "left single quote" - on my US English keyboard, in the upper left corner under the tilde. Do see help macro for more details if this is new to you.

    4) It's possible that # is supposed to be an integer, in which case I would do the following.
    Code:
    . local num = ceil(`r(min)')
    
    . display "the minimum is `num'"
    the minimum is 84

    Comment


    • #3
      A couple points.
      1. 84pt graph would be slight larger than an inch which is rather small
      2. this height option height(`$var')is combining the global macro prefix along the local left grave accent and right single quote
      Here is an example defining the size of an image based on variable

      Code:
      sysuse auto, clear
      putdocx clear
      putdocx begin
      putdocx paragraph
      scatter price mpg
      graph export auto.png, replace
      
      gen imageheight = .
      forv i = 1/3 {    
          replace imageheight = 85 *`i' in `i'
          putdocx image auto.png, height(`=imageheight[`i']'pt)
      }
      putdocx save example.docx, replace
      Which produces three different sized graphs in the document:

      Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	30.4 KB
ID:	1458481

      Comment


      • #4
        Dear William and Scott

        First of all, thanks for your replies.
        1) You didn't tell us what command you used that gave you the returned value r(min) so I supplied the summarize command, which does so.
        Sorry. I indeed used summarize myself.

        So the following code is doing the trick for me:
        Code:
        local num `r(min)'
        putdocx clear
        putdocx begin
        putdocx paragraph
        sum var1
        putdocx image var1.png, height(`r(min)'pt)
        sum var2
        putdocx image var2.png, height(`r(min)'pt)
        putdocx save Bilder, replace
        From my (non-existing) understanding of defining and using locals, I would have expected "#" having to be " $`num' " or at least " `num' " instead of " `r(min)' ". However I will keep this in mind.
        Don't worry, I will be looping the command as well.

        Thank you both for your help!

        Comment


        • #5
          Is it now possible to use the square root of r(min) as image size?
          Code:
          putdocx image sports.png, height(`r(min)^0.5'pt)
          produces same image size as
          Code:
          putdocx image sports.png, height(`r(min)'pt)
          while defining locals and refering to them as I'd do (see above; probably wrong) doesn't produce individual image size but one (I guess default size) for all.
          Code:
          local num `r(min)'
          local test `r(min)^0.5'
          putdocx clear
          putdocx begin
          putdocx paragraph
          sum var1
          putdocx image var1.png, height(`num'pt)
          putdocx image var1.png, height(`test'pt)
          As soon as I try to implement "^0.5" directly in height option in a different way than specified in the first code it says height option was specified incorrectly.

          Comment


          • #6
            my (non-existing) understanding of defining and using locals
            This will be a problem for you if you expect to do more than the most basic Stata, such as the automation task that is causing your current problems.

            When I began using Stata in a serious way, I started - as others here did - by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through Stata's Help menu. The objective in doing this was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and manual.

            Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively. And when you are familiar with the documentation, you don't have to wait two hours for someone to solve your problem in an online forum.

            With that said,
            Code:
            local test `r(min)^0.5'
            should be
            Code:
            local test = `r(min)'^0.5

            Comment


            • #7
              Dear William

              Thanks again! I will consider studying Stata manuals as well when circumstances like spare time and incoming amount of 'Stata-work' will be larger.

              Kind regards

              Comment

              Working...
              X