I am trying to match $$ resp $ pairs to switch into latex-mode via polymode in a markdown file. $$ is the delimiter for displayed math in this case and $ for inline math.

I know that this is not a good choice for delimiters because they are symmetric. However latex-mode itself i.e. when editing an usual latex-document just recognizes those pairs very well.

So is there any good regular expression I can use in my code below instead of just looking for $$ resp. $?

(require 'polymode)

(defcustom pm-host/markd
  (pm-bchunkmode "Markd"
                 :mode 'markdown-mode)
  "Markdown host chunkmode"
  :group 'hostmodes
  :type 'object)

(defcustom pm-inner/latex
  (pm-hbtchunkmode "latex"
           :mode 'latex-mode
           :head-reg  "\\$\\$\\|\\$"
           :tail-reg  "\\$\\$\\|\\$")
  "latex typical chunk."
  :group 'innermodes
  :type 'object)

(defcustom pm-poly/markdown-latex
  (pm-polymode-one "markdown-latex"
           :hostmode 'pm-host/markd
           :innermode 'pm-inner/latex)
  "markdown-python typical polymode."
  :group 'polymodes
  :type 'object)

(define-polymode poly-markdown+latex-mode pm-poly/markdown-latex)
(add-to-list 'auto-mode-alist '("\\.mdwn\\'" . poly-markdown+latex-mode))
  • Could it be simply that you forgot to quote those $ (which by default mean "match end of line" in regexps)? – Stefan Aug 24 '16 at 18:28
  • @Stefan: You mean escape them via \\$\\$\\|\\$, I will try it ... – student Aug 24 '16 at 19:04
  • @Stefan: It doesn't work, for example it interprets the closing delimiter of a block as the opening delimiter of the next block (which is wrong). – student Aug 24 '16 at 19:06
  • what about using \\$+ to match 1 or more $, instead of explicitly trying one (\\$) or two (\\$\\$)? That should treat $ and $$ the same, and shouldn't break unless you do weird things with $ inside math mode, or use more than two $ in some other context... – Tyler Aug 24 '16 at 23:08
  • @Tyler: I tried it, but with the same result as described in the comment above. – student Aug 25 '16 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.