2

Consider the variable TeX-current-macro raised in the recent question detect if inside a LaTeX footnote and get its bounds? also in https://emacs.stackexchange.com/a/7634/2690. I would like to determine if I am inside the macros chapter or section in a latex file, I can write define a function which shows yes if it is the case, otherwise no. Of course I can use the following which does the job.

 (defun foo-test () (interactive)
    (if (or (equal (TeX-current-macro) "chapter") 
(equal (TeX-current-macro) "section")) 
(message "yes") (message "no"))
    )

But it is long. Like in my previous question What is the easiest way to check if a character belongs to a particular set of characters?, I am wondering if there is a better way to do it. Inspired by the answer https://emacs.stackexchange.com/a/7880/2609, I tried the below code, but unfortunately it doesn't work. I also tried with memq instead of member but it doesn't work either.

 (defun foo-test () (interactive)
    (if (member (TeX-current-macro) '(chapter section)) 
(message "yes") (message "no"))
    )

Any solution about checking if the value of a variable belong to a list of elements is welcomed.

Name
  • 7,689
  • 4
  • 38
  • 84
  • 1
    The second example doesn't work because you are not comparing objects of the same type, you are comparing symbols to strings. `"chapter"` does not equal `'chapter` – Jordon Biondo Feb 03 '15 at 19:18
  • 1
    Put `chapter` and `section` as strings rather than symbols, since `TeX-current-macro` returns a string. – Dan Feb 03 '15 at 19:19
  • 1
    @JordonBiondo let me ask what is the correct syntax for doing it. – Name Feb 03 '15 at 19:22
  • 1
    @Dan let me ask what is the correct way to do it. – Name Feb 03 '15 at 19:23
  • 1
    `(member "a" '("a" "b"))` will be true, `(member "a" '(a b))` will not because "a" is a string and 'a is a symbol, so just change `'(chapter section)` from your second example to `'("chapter" "section")` so it's a list of strings instead of a list of symbols. `'chapter` and `"chapter"` look alike, but they are really no more equal than `3` and `"three"` – Jordon Biondo Feb 03 '15 at 19:25
  • 1
    @JordonBiondo thank you very much for your clarification. – Name Feb 03 '15 at 19:30

1 Answers1

1

member's comparison is done with equal, so you can't mix types. Your example uses symbols for chapter and section, but TeX-current-macro returns a string. Hence, you want your list to have strings as well:

(defun foo-test () (interactive)
  (if (member (TeX-current-macro) '("chapter" "section")) 
      (message "yes")
    (message "no")))

Examples:

(setq macro-names '("chapter" "section"))
(member "chapter" macro-names)          ; => ("chapter" "section")
(member "puppies" macro-names)          ; => nil
Dan
  • 32,584
  • 6
  • 98
  • 168