Announcement

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

  • Help on single, double, and nested quotes in locals

    Might someone who is better at quote nesting than I am—that would be roughly 99.9% of the participants on StataList—suggest a solution to the following?

    The problem is a generic one but is illustrated with this example:
    Code:
    sysuse auto
    scatter rep headroom mpg, legend(label(1 "Rep" "Variable") label(2 "Headroom" "Variable"))
    I'd like to be able to pass local arguments to the scatter command that would replicate this graph, using

    Code:
    local l1=<something1>
    local l2=<something2>
    scatter rep headroom mpg, legend(label(1 <something3>) label(2 <something4>))
    Assuming this is possible, what is the proper way to specify l1 and l2 and their corresponding arguments in the label options?

    Thanks much in advance.

  • #2
    Try

    Code:
    local l1 `""Rep" "Variable""'
    local l2 `""Headroom" "Variable""'
    
    scatter rep headroom mpg, legend(label(1 `l1') label(2 `l2'))

    Depending on details, you might not even need nested quotes at all

    Code:
    local l1 1 "Rep" "Variable"
    local l2 2 "Headroom" "Variable"
    
    scatter rep headroom mpg, legend(label(`l1') label(`l2'))

    Comment


    • #3
      Thanks Daniel. Much appreciated.

      Comment


      • #4
        The principles start with

        1. Double quotes are treated as delimiters, defining when a string starts and ends, and as such they are not part of the string strict sense.


        Code:
        . local foo "Stata is sometimes puzzling"
        
        . mac li _foo
        _foo:           Stata is sometimes puzzling
        Here I used a slightly unusual command to show that the local definition was accepted but the double quotes have gone: otherwise put, they were stripped.

        2. So, each time a macro reference is interpreted, double quotes or compound quotes delimiting the string are stripped. Hence the price of a delayed definition is that you must slap on extra quotes knowing that they will get stripped.

        Comment


        • #5
          Thank you, Nick.

          Comment


          • #6
            I'm back, flummoxed as usual by quote nesting. What I'm trying to do can be illustrated simply by the following:
            Code:
            sysuse auto
            scatter price mpg, ylab(0 15906 "max(price)")
            In my program scprog I'd like to pass three arguments that replicate the previous scatter command:
            Code:
            scprog price mpg "third argument that respects the quotes around max(price)"
            That is, I'd like to pass a string argument as the third argument that gives the desired labeling. This would appear to require nested quotes but I've yet to find the combination of single and double quotes that accomplishes this.

            Any suggestions? Thanks in advance.

            Comment


            • #7
              I am not sure that I can follow. Does this help:

              Code:
              capture program drop scprog1
              program scprog1
                  
                  syntax varlist(numeric min=2 max=2) [ , LABEL(string asis) ]
                  
                  scatter `varlist' , ylab(0 15906 `label')
                  
              end
              
              capture program drop scprog2
              program scprog2
                  
                  args var1 var2 label
                  
                  scatter `var1' `var2' , ylab(0 15906 "`label'")
                  
              end

              Comment


              • #8
                You seem to be trying to write (a) non-standard syntax (b) something difficult, and I feel zero shame in saying that I had to fiddle around a bit to get what I think you want. But in writing Stata programs over 30 years or so I've learned to use standard syntax whenever I can and only depart from it with really good reason.

                First, running this may help.

                Code:
                capture program drop scprog
                
                program scprog
                    gettoken y 0 : 0
                    gettoken x 0 : 0
                    local stuff `"`0'"'
                    di "x is `x'"
                    di "y is `y'"
                    di `"stuff is `stuff'"'
                end
                
                scprog y x `"for John, with regards about use of "quotation marks""'

                Let me try to write down as many of the rules as I think I understand and can remember. I often write quotes for brevity while still feeling pedantic enough to regard that as improper variation on quotation marks.

                0. Stata parses left to right but otherwise is inclined to respect pairing of quotation marks as explained below.

                1. Single quotes that are paired ` ' have a major role in Stata as enclosing the name of a local macro. If text between is not a local macro name, that could be benign -- you're deliberately referring to a local macro that isn't yet defined -- or it could be your bug. If you're a beginner programmer, guess accordingly. The opening left-inclined quotation mark ` is essential for local macro names; there is no scope for any other symbol, and similarly for the other mark. (There is a tiny exception best not learned that a local macro is in fact really a special case of a global macro. Forget this immediately!) The fact that in many fonts ` ' don't look well balanced visually -- one leans left while its partner stands straight with no risk of back trouble -- is neither here nor there. The fact that in some software (e.g. MS Office) a program may guess what you mean and correct accordingly is also neither here nor there. Stata is almost always a program that thinks you're smart and have thought things through and that you mean what you say. (Exception: try using the seperate command.)

                2. Single quotes that are matched ' ' or ` ` have no special meaning to Stata. Your best use if you want them is just within text you want to display in output, say as header or footer text or as part of a graph axis title.

                3. Double quotes " " have a major role in Stata as delimiting text (marking the start and end of a string).

                4. Quotes tend to get stripped on reading, unless you insist otherwise (which you can do variously, such using option switches in the syntax command). There are low-level parsing commands such as gettoken where you get to tell Stata the parsing rules.

                5. As a corollary, quotes that you want to be shown literally have to be protected by other quotes on their outside.

                6. Every time a string is processed the outermost quotation marks will get stripped. This is just rule #4 all over again for emphasis. This inhibits bizarre practices such as defining local macros well in advance, much though that style is encouraged elsewhere.

                6. As it is clear that sequences with marks such as " " " " are ambiguous -- does that mean "a" "b" or " "a b" "? -- Stata provides special compound double quotation marks `" "' to indicate opening and closing quotation marks for text. Note that (a) these are composite marks, just like >= or <= or != or == (b) they have nothing to do with local macros. In general compound quotation marks are allowed anywhere that " " are allowed, but they are essential to avoid ambiguity when you have two or more levels of double quotation marks.

                I must have forgotten or mangled something. I find that I have never stopped making errors in use of quotation marks; I just get faster at fixing them.
                Last edited by Nick Cox; 05 Nov 2022, 12:35.

                Comment


                • #9
                  Thank you Daniel and Nick. I'm copying your suggestions into a Word document that I've named "stata quote quotes quotation nest nesting" so these rules will be easy to find the next time—and there will be a next time—I get confused about this.
                  Last edited by John Mullahy; 05 Nov 2022, 13:05.

                  Comment


                  • #10
                    Probably obvious, but

                    Originally posted by Nick Cox View Post
                    2. Single quotes that are matched ' ' or ` ` have no special meaning to Stata.
                    does not explicitly mention nested local macros as an exception. We can have

                    Code:
                    local inner foo
                    local outer inner
                    display "``outer''"

                    Comment


                    • #11
                      Ok; that is different. I meant e.g. ‘frog’.

                      Comment


                      • #12
                        While we are (more or less) on the topic, could someone help with the rules of escaping quotation marks to show them explicitly?

                        For instance, I had raised this ages ago here: https://www.statalist.org/forums/for...-quote-problem

                        I have never quite understood why this happens:

                        Code:
                        . dis `"\`"'
                        `"'
                        whereas I would have expected/wanted it to display just `

                        Relatedly, I am utterly confused by the behaviour noted in this parallel thread:
                        https://www.statalist.org/forums/for...s-not-collapse
                        Last edited by Hemanshu Kumar; 05 Nov 2022, 23:49.

                        Comment


                        • #13
                          Here is a second edition and it's not personal property if anyone wants to treat it as a wiki to be copied and edited (but please keep names of contributors and dates visible). I have answered Hemanshu Kumar's question by not answering it!


                          Quotation marks in Stata: some points explained and others left cryptic, as life is short and complicated

                          Let me try to write down as many of the rules as I (NJC) think I understand and can remember. I often write quotes for brevity while still feeling pedantic enough to regard that as improper variation on quotation marks.

                          0. Stata parses left to right but otherwise is inclined to identify and respect pairing of quotation marks as explained below. In brief, Stata attaches special meaning to examples such as

                          `foo'
                          "frog"
                          `"more complicated text possibly including "quoted text""'


                          As already hinted, quotation marks can be nested, the leading examples being `" " " "' as just above, and ``j'' (local macro references nested).

                          1. Single quotes that are paired ` ' have a major role in Stata as enclosing the name of a local macro. If text between is not a local macro name, that could be benign -- you're deliberately referring to a local macro that isn't yet defined -- or it could be your bug. If you're a beginner programmer, guess accordingly. The opening left-inclined quotation mark ` is essential for local macro names; there is no scope for any other symbol, and similarly for the other mark. (There is a tiny exception best not learned that a local macro is in fact really a special case of a global macro. Forget this immediately!) The fact that in many fonts ` ' don't look well balanced visually -- one leans left while its partner stands straight with no risk of back trouble -- is neither here nor there. The fact that in some software (e.g. MS Office) a program may guess what you mean and correct accordingly is also neither here nor there. Stata is almost always a program that thinks you're smart and have thought things through and that you mean what you say. (Exception: try using the seperate command.)

                          2. Single quotes that are matched ' ' or ` ` have no special meaning to Stata. Your best use if you want them is just within text you want to display in output, say as header or footer text or as part of a graph axis title.

                          3. Double quotes " " have a major role in Stata as delimiting text (marking the start and end of a string). For example, filenames with embedded spaces usually must be enclosed in double quotes.

                          4. Quotes tend to get stripped on reading, unless you insist otherwise (which you can do variously, such as by using option switches in the syntax command). There are low-level parsing commands such as gettoken where you get to tell Stata your parsing rules.

                          5. As a corollary, quotes that you want to be shown literally have to be protected by other quotes on their outside.

                          6. Every time a string is processed the outermost quotation marks will get stripped. This is just rule #4 all over again for emphasis. This inhibits bizarre practices such as defining local macros well in advance, much though that style is encouraged elsewhere.

                          6. As it should be clear that sequences with marks such as " " " " are ambiguous -- does that mean "a" "b" or " "a b" "? -- Stata provides special compound quotation marks `" "' to indicate opening and closing quotation marks for text. Note that (a) these are composite marks, just like >= or <= or != or == (b) they have nothing to do with local macros. In general compound quotation marks are allowed anywhere that " " are allowed, but they are essential to avoid ambiguity when otherwise you need two or more levels of double quotation marks.

                          7. Escaping quotation marks with escape character \ is best explained elsewhere by someone who understands it. A desire for it seems to arise when people want to define string constants early in a program, and together, as if they were programming in C or a C-like language. If you want to do that, you may need to find out what the rules are -- or else might prefer to program in a way that avoids the need for escaping, such as by using include or by defining local macros, just in time, as and when you need them.

                          NJC 6 November 2022
                          Last edited by Nick Cox; 06 Nov 2022, 03:16.

                          Comment

                          Working...
                          X