I had a hard time typing with Rt-to-Lt language and using quotes in LaTeX while keeping the font-locking of quotes. I use csquotes package latest version (Version 5.1d -- 2011) for many advantages like toggling between different variants in a style. To make a long story short (see: https://tex.stackexchange.com/q/226097/26295) I want to have font-locking of text between two "s.

How to do achieve this?

enter image description here

I want to have font-lock for the first line, i.e. between the two " marks, using the same color of default quotes seen in the second line.


\newfontfamily\arabicfont[Script=Arabic,Ligatures=TeX]{Simplified Arabic} 

\usepackage[style=arabic,arabic=quotes]{csquotes} % this is a custom style -- but it is not important for this font-lock question I think.

هذا نص باللغة العربية  "داخل علامتي الافتباس".

هذا نص باللغة العربية  ``داخل علامتي الافتباس''.

هذا نص باللغة العربية \enquote{داخل علامتي الاقتباس}.


%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


  • font-latex-quote variable was set to auto
  • face: font-latex-string face: foreground LightSalmon

Trial Code

The comments of lawlist has put me on the right track but there is still the problem of that single " in the preamble of \MakeOuterQuote{"} as it interferes with the regexp code. The below code in the .init gives the desired effect if that single unmatched " in the preamble were not there:

(font-lock-add-keywords 'latex-mode (list (list "\\(\"\\)\\(\\(.\\|\n\\)+?\\)\\(\"\\)" '(1 'font-latex-string-face t) '(2 'font-latex-string-face t) '(3 'font-latex-string-face t) '(4 'font-latex-string-face t))))  

enter image description here

Trial to avoid it
I used this regexp to avoid that single " and that increased number of potential groups from 4 to 5.

\\(:?[^{]\\) in front of the previous regexp  

So I put in in .init:

(font-lock-add-keywords 'latex-mode (list (list "\\(:?[^{]\\)\\(\"\\)\\(\\(.\\|\n\\)+?\\)\\(\"\\)" '(2 'font-latex-string-face t) '(3 'font-latex-string-face t) '(4 'font-latex-string-face t) '(5 'font-latex-string-face t))))

M-x re-builder

enter image description here

So re-builder for regexp confirms that the regexp is avoiding that single unmatched " (not shown) and shows that there are now 5 regexp groups (1 blue, 2 green, 3 red, 4 blue, 5 green), but I want to font-lock all except group 1 which is the avoid group. However, the current problem is that font-lock won't take effect unless I hit enter of the quote line; as I write quoted text there is no font-lock until I hit enter then and only then it will take the effect. So am I missing something? is it because of the avoid group it doesn't font-lock immediately?

Suggestion as a workaround
Is it possible to enter that single " as a unicode in the preamble to avoid being detected by the regexp?

  • Here is a snippet from my custom version of tex-mode.el from the variable tex-font-lock-keywords-2: (list (concat (regexp-opt [backtick]("\"") t) "\\(\\(.\\|\n\\)+?\\)" (regexp-opt [backtick]("\"") t)) '(1 'font-lock-keyword-face t) '(2 'font-lock-function-name-face) '(4 'font-lock-keyword-face t)) Replace [backtick] with a literal backtick. If you can find the equivalent section in AUCTeX, then that should do the trick. – lawlist Feb 3 '15 at 21:04
  • can I override that from within .init or .emacs? I don't which relevant part for auctex you are referring to. – doctorate Feb 3 '15 at 21:09
  • I'll check my notes from last year -- I believe I had set it up for AUCTeX in my init.el when I first started using AUCTeX -- I'll add another comment if can find it in my old setup. – lawlist Feb 3 '15 at 21:11
  • I much appreciate your efforts and your notes to save the day. – doctorate Feb 3 '15 at 21:12
  • Perhaps this would work?: (font-lock-add-keywords 'latex-mode (list (list (concat "\\(\"\\)\\(\\(.\\|\n\\)+?\\)\\(\"\\)") '(1 font-lock-keyword-face t) '(2 font-lock-function-name-face t) '(4 font-lock-keyword-face t)) )) – lawlist Feb 3 '15 at 21:14

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.