Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Auto-wrapping text in graph notes


From   "Raymond P. Guiteras" <[email protected]>
To   [email protected]
Subject   st: Auto-wrapping text in graph notes
Date   Mon, 21 Jan 2008 12:20:27 -0500

Hi all,

I have written some simple code to automate the wrapping of text in graph notes. All comments, corrections and improvements are welcome.

This is based on Scott Merryman's code for wrapping titles:
http://www.stata.com/statalist/archive/2007-03/msg00778.html


-- begin code --

cap log close

* autowrapnote.do
* Raymond P Guiteras
* 20 Jan 2008

* simple example of automating text wrap in notes
* the length below is 110 because that looks best when exporting to .emf
* if you prefer to export in other formats then you should adjust this number
* the main trick is the use of compound double-quotes (`" "') to create (and use) a macro that itself contains double-quotes (" ")
* the double-quotes are necessary to get Stata's "note()" suboption to write to separate lines

log using autowrapnote, replace

sysuse auto

* write out note here as a local macro
local MyNote "Notes: This is a very long note, the point of which is just to test the auto-wrapping code below, as well as see how the text size changes when exporting to different file types, i.e. wmf, emf, ps, eps."
* the goal is to create a local macro with the above as its contents, but split into pieces of 110 characters or fewer
* with these pieces enclosed in double-quotes, e.g. "Notes: This is a very long note ...as well as" "to see...eps."


* obtain length of note (number of characters) and number of pieces of length 110
local l = length("`MyNote'")
local b = ceil(`l'/110)

if `l' > 110 {
forv i = 1/`b' {
// use extended macro function "piece" to break MyNote into chunks of 110 characters
local lab`i': piece `i' 110 of "`MyNote'", nobreak
// di "local macro lab`i' contains"
// di "`lab`i''"
// use compound double quotes to create macro MyNote2 that contains simple double-quotes
// create first piece
if `i'==1 {
local MyNote2 `""`lab`i''""'
}
// append subsequent pieces
if `i'>=2 {
local MyNote2 `"`MyNote2' "`lab`i''""'
}
// di "local macro MyNote2 contains"
// di `"`MyNote2'"'
}
}
else {
local MyNote2 `""`MyNote'""'
}

//di `"`MyNote2'"'

twoway scatter weight length, ///
title("Testing Position of Title") ///
note(`MyNote2' ///
, span )
graph save autowrapnote, replace
local exporttypes "emf wmf eps ps"
foreach ext of local exporttypes {
cap graph export autowrapnote.`ext', replace
di "Exporting to `ext' returned"
di _rc
}



cap log close


-- end code --
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index