Announcement

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

  • Can I underline the title of a legend in Stata?

    I am a co-author on a paper that was accepted, and the journal wants us to underline the title of the legend in our maps (!).
    I saw on statalist (https://www.stata.com/statalist/arch.../msg01211.html) a discussion about the title of the graph, which is not what I need. Either way, I tried the option {it:} just to test, and it did change the legend title to italic. The option {ul:} doesn't work, however.

    Any tips/solutions for this?

    Here is my code and the part that needs to be underlined is the title "Levels" (5th line):

    maptile varX, geo(statehex) fcolor(Reds) twopt(title("{fontface Times New Roman: varX (A)}", ring(0) size(*0.9)) ///
    legend(symy(*1.5) symx(*1.5) size(*0.8) position(4) ///
    label(1 "No Data") label(2 "Lowest (1st quartile)") label(3 "Below median (2nd quartile)") ///
    label(4 "Above median (3rd quartile)") label(5 "Highest (4th quartile)") ///
    title("Levels", size(*0.8) ///
    bexpand justification(left))))

    Thank you!

  • #2
    I posted this question on my Twitter (@lenaconstante), and Dr Tim Morris replied with an option that worked perfectly.
    If you are looking for a solution, here it is:

    1. Write the word you want to be underlined here: https://fancyfonts.co/en/underline-text-generator/
    2. Copy the best available option. In my case, it was the second one - L̲e̲v̲e̲l̲s̲
    3. Paste into your do-file.

    That´s it!
    Thank you again, Dr Morris!

    Here it is the link for the Twitter conversation - https://twitter.com/lenaconstante/st...LgjlmJroqUQcOw

    Comment


    • #3
      Is this just like a journal requirement? It seems goofy that they'd care about this so much, but I guess each journal has its own style. Either way, I think this is silly because the figure can't be replicated perfectly short of using this website. Consider
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear*
      input int year long gdp7 double cf_adid float(cfub cflb relative diff_)
      1960  2284 2149.2583049776104  2892.755  1405.762 -30   134.7417
      1961  2388 2277.7339806026253 3021.2305 1534.2375 -29  110.26602
      1962  2527  2414.721800934968  3158.218 1671.2255 -28   112.2782
      1963  2610  2544.351665672841  3287.848 1800.8552 -27   65.64833
      1964  2806  2721.952515413878  3465.449  1978.456 -26   84.04749
      1965  3005  2897.894218305181 3641.3906 2154.3977 -25  107.10578
      1966  3168  3116.468781552679  3859.965 2372.9724 -24   51.53122
      1967  3241  3296.017325421664  4039.514  2552.521 -23  -55.01733
      1968  3571   3600.79538803575  4344.292  2857.299 -22  -29.79539
      1969  3998  4010.171839002599 4753.6685 3266.6755 -21  -12.17184
      1970  4367  4399.133570035771   5142.63  3655.637 -20  -32.13357
      1971  4686  4761.765361931868  5505.262  4018.269 -19 -75.765366
      1972  5055  5173.161643846219  5916.658  4429.665 -18 -118.16164
      1973  5553  5744.196707432753  6487.693    5000.7 -17  -191.1967
      1974  6074  6327.206483065189  7070.703   5583.71 -16  -253.2065
      1975  6603  6843.201153332303  7586.698  6099.705 -15 -240.20116
      1976  7367  7491.566887480329  8235.063   6748.07 -14 -124.56689
      1977  8090  8114.251913867261  8857.748  7370.755 -13 -24.251913
      1978  8928  8889.073492692798   9632.57  8145.577 -12   38.92651
      1979 10067   9890.90964265361 10634.406  9147.413 -11  176.09036
      1980 11083 10959.400213881974 11702.896 10215.904 -10  123.59978
      1981 12115 12041.019686269101 12784.516 11297.523  -9  73.980316
      1982 12761 12784.533885408364  13528.03 12041.037  -8 -23.533886
      1983 13519 13499.554040823941  14243.05 12756.058  -7   19.44596
      1984 14481 14385.754869040284  15129.25  13642.26  -6   95.24513
      1985 15291 15244.110884908925 15987.607 14500.614  -5   46.88911
      1986 15998  15951.70094791048 16695.197 15208.204  -4   46.29905
      1987 16679 16721.112265269498  17464.61 15977.616  -3  -42.11227
      1988 17786 17827.113982665745 18570.611 17083.617  -2  -41.11398
      1989 18994 19016.866547563768 19760.363 18273.371  -1 -22.866547
      1990 20465 20098.341746311788  20841.84 19354.846   0   366.6583
      1991 21602 20920.701489223167   21664.2 20177.205   1   681.2985
      1992 22154 21617.903850208997   22361.4  20874.41   2   536.0961
      1993 21878 22135.557667325847 22879.055  21392.06   3 -257.55768
      1994 22371 23191.496431951877  23934.99     22448   4  -820.4965
      1995 23035  24184.31547946848  24927.81  23440.82   5 -1149.3154
      1996 23742 25169.488024112405 25912.984  24425.99   6  -1427.488
      1997 24156 26295.038819607697 27038.535 25551.543   7 -2139.0388
      1998 24931 27143.800638475703 27887.297 26400.305   8 -2212.8005
      1999 25755 28317.971650350133  29061.47 27574.475   9  -2562.972
      2000 26943  30315.65659424881  31059.15  29572.16  10 -3372.6565
      2001 27449 31469.701433467773   32213.2 30726.205  11 -4020.7014
      2002 28348  32650.07621182382 33393.574  31906.58  12  -4302.076
      2003 28855  33462.55321045635  34206.05 32719.057  13  -4607.553
      end
      format %ty year
      cls
      
      tsset year, y
      
      twoway (tsline gdp7) (tsline cf_adid), tline(1990) legend(title(ul: {This is my title})) note("T͟e͟x͟t͟") name(Fig1, replace)
      
      twoway (tsline gdp7) (tsline cf_adid), tline(1990) legend(title(ul: {This is my title})) note("T_e_x_t_") name(Fig2, replace)
      Where the second graph is formatted as such because Stata apparently interprets this (from the command line) as
      Code:
      twoway (tsline gdp7) (tsline cf_adid), tline(1990) legend(title(ul: {This is my title})) note("T_e_x_t_") name(Fig2, replace)
      Which means that in this instance, to make a graph, you have to outsource this to some website, which I guess is okay, but this shouldn't be something journals get hung up over, the typesetting staff should do all that. Perhaps Nick Cox who has more graphical knowledge than I do can comment?

      Comment


      • #4
        Thanks for the nudge. All I have to say is that underlining is what I and contemporaries used to do sometimes when handwriting or typewriting back in the day, meaning in the 20th century. Now we have so many ways to emphasize that are easier and as or more effective: font size, use of bold or even italic. I would not ever ask an author to add underlining to a graph myself.

        I don't speak for the journal in question, naturally, but their request isn't, I presume, for use of Stata plus a website; it is just that the graphics image as coded in a file meets certain standards.

        Comment


        • #5
          With Unicode, you can do all this inhouse. Letters such as "y" extend below the line, and the ensuing misalignment can be addressed by using a fixed width font.

          Code:
          sysuse auto, clear
          *INSERT TITLE HERE (HIGHLIGHTED)
          local tounderline `:di ustrtohex("underlined title")'
          local tounderline= ustrregexra("`tounderline'", "(\w)(\\)(\w)", "$1\\u0332$2$3")
          *SPACES SHOULD NOT BE UNDERLINED
          local tounderline= ustrregexra("`tounderline'", "\b(\\u0020\\u0332)\b", "\\u0020")
          *GRAPH
          scatter mpg weight, leg(on title(`=ustrunescape("`tounderline'\u0332")')) scheme(s1mono)
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	113.0 KB
ID:	1682346

          Comment


          • #6
            A wise philosopher once said "It seems impossible until it's done." I would never have thought of this.

            Comment


            • #7
              Just a reminder that this will underline each character separately. Thus, lower case letters hanging below line will probably not be rendered with "the same line" as other characters. A regex free version showing a word with "g":
              Code:
              local title "underlined underdog"
              
              forvalues p=1/`=ustrlen("`title'")' {
                  
                  loc char = usubstr("`title'", `p', 1)   
                  loc undl = cond("`char'" != " ", "\u0332", "")   
                  loc hex = "`hex'" + ustrtohex("`char'") + "`undl'"
              }
              
              loc utitle = ustrunescape("`hex'") 
                  
              tw fun 1 , title("`utitle'")

              Comment


              • #8
                Originally posted by Bjarte Aagnes View Post
                Just a reminder that this will underline each character separately. Thus, lower case letters hanging below line will probably not be rendered with "the same line" as other characters.
                You can substitute the Unicode character "\u0332" with "\u035f" to avoid this issue.

                Code:
                sysuse auto, clear
                *INSERT TITLE HERE (HIGHLIGHTED)
                local tounderline `:di ustrtohex("The quick brown fox jumps over the lazy dog")'
                local tounderline= ustrregexra("`tounderline'", "(\w)(\\)(\w)", "$1\\u035f$2$3")
                *SPACES SHOULD NOT BE UNDERLINED
                local tounderline= ustrregexra("`tounderline'", "\b(\\u0020\\u035f)\b", "\\u0020")
                *GRAPH
                scatter mpg weight, leg(on title(`=ustrunescape("`tounderline'\u035f")')) scheme(s1mono)
                Res.:
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	64.4 KB
ID:	1682418

                Last edited by Andrew Musau; 18 Sep 2022, 02:48.

                Comment


                • #9
                  Andrew Musau your code produces this textbox on my machine instead of the one you show. What do you think I need to fix?
                  Click image for larger version

Name:	Screenshot 2022-09-18 at 5.23.02 PM.png
Views:	1
Size:	37.4 KB
ID:	1682430

                  Comment


                  • #10
                    Ref #8:

                    Even if #2 worked perfectly for the OP, and its possible do do the same in Stata #5, only single characters are underlined.

                    Thus, characters that extends below the baseline will make "jumps", and wide characters will make "holes" in the "line". (And using "\u035f" Combining Double Macron Below introduce more problems.)

                    If one need to underline using "Stata only", one way can be to export as SVG, then insert text-decoration="underline"
                    Code:
                    <text ... text-decoration="underline" >The quick brown fox jumps over the lazy dog</text>

                    Comment


                    • #11
                      Originally posted by Hemanshu Kumar View Post
                      Andrew Musau your code produces this textbox on my machine instead of the one you show. What do you think I need to fix?
                      [ATTACH=CONFIG]n1682430[/ATTACH]
                      It may be operating systems. I use Windows and it renders as in #8.

                      Comment


                      • #12
                        Ah. I'm on Stata 17/MP for Mac, with macOS Monterey (v 12.6).

                        For the record, I also tried this with a fixed-width font, via

                        Code:
                        graph set window fontface "Monaco"
                        and the result is still ugly.

                        Comment


                        • #13
                          Originally posted by Bjarte Aagnes View Post
                          Ref #8:

                          Even if #2 worked perfectly for the OP, and its possible do do the same in Stata #5, only single characters are underlined.

                          Thus, characters that extends below the baseline will make "jumps", and wide characters will make "holes" in the "line". (And using "\u035f" Combining Double Macron Below introduce more problems.)

                          If one need to underline using "Stata only", one way can be to export as SVG, then insert text-decoration="underline"
                          Code:
                          <text ... text-decoration="underline" >The quick brown fox jumps over the lazy dog</text>

                          Originally posted by Hemanshu Kumar View Post
                          Ah. I'm on Stata 17/MP for Mac, with macOS Monterey (v 12.6).

                          For the record, I also tried this with a fixed-width font, via

                          Code:
                          graph set window fontface "Monaco"
                          and the result is still ugly.
                          That's the best you can get with Unicode. Do it in a third party software? Nah... as far as workarounds are concerned, nothing stops you from being explicit.

                          Code:
                          sysuse auto, clear
                          scatter mpg weight, leg(on title("The quick brown fox jumps over the lazy dog" ///
                          "{sup:{bf:__________________________________________________________________}}")) ///
                          scheme(s1mono)
                          Click image for larger version

Name:	Graph.png
Views:	1
Size:	59.4 KB
ID:	1682466

                          Comment

                          Working...
                          X