0

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.

Code

\documentclass[twoside=semi]{scrbook}
\usepackage{polyglossia}
\setmainlanguage[numerals=mashriq,abjadjimnotail=true]{arabic} 
\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.
\MakeOuterQuote{"}
\begin{document}

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

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

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


\end{document}

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

Notes

  • 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?

doctorate
  • 1,789
  • 16
  • 39
  • 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 03 '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 03 '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 03 '15 at 21:11
  • I much appreciate your efforts and your notes to save the day. – doctorate Feb 03 '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 03 '15 at 21:14
  • it didn't work. I think I need some lanuguage-aware code (arabic in my case). Other thing: why 1, 2, 4 not 3! can you explain the code. – doctorate Feb 04 '15 at 07:36
  • also I need to have the same color of the standard quotation font-lock as in the second line of the screenshot (foreground: LightSalmon). – doctorate Feb 04 '15 at 07:39
  • In terms of defining your own face with your favorite colors, the following is an example: `(defface doctorate-light-salmon '((t (:foreground "LightSalmon"))) "Face for the color LightSalmon." :group 'doctorate)` If you do not set your face as a variable, then a backquote is needed in the following example -- it has been modified to only highlight what is between the double-quotation marks: `(font-lock-add-keywords 'latex-mode (list (list (concat "\\(\"\\)\\(\\(.\\|\n\\)+?\\)\\(\"\\)") '(2 'doctorate-light-salmon t)) ))` – lawlist Feb 04 '15 at 08:20
  • In terms of your question as to why the second and third regexp groupings exist, I believe `+?` needs to belong to a separate group in order for the highlighting to work properly -- each set of parentheses is a group. I do not have any Emacs experience with text that reads from right to left, and I am unsure whether the `font-lock` and/or `jit-lock` libraries make distinctions for language direction when highlighting a buffer. – lawlist Feb 04 '15 at 08:21
  • I think `+?` is placed correctly to indicate multiples of new feed lines that might lie between two widely separate quotes. but my question was: is it `'(4 font-lock...` or should be `'(3 font-lock...`? – doctorate Feb 04 '15 at 08:28
  • There are four (4) sets of parentheses for the regex, so there are four (4) groups -- 1 is the double-quote on the left; 4 is the double-quote on the right; 2 and 3 are the multi-line regex. – lawlist Feb 04 '15 at 08:30
  • I think we have only 3 groups (electric parentheses are helpful to tell how may: `\\(\"\\)` - `\\(\\(.\\|\n\\)+?\\)`-`\\(\"\\)`!. Now, instead of custom color I prefer using the default one of string face: `(font-lock-add-keywords 'latex-mode (list (list (concat "\\(\"\\)\\(\\(.\\|\n\\)+?\\)\\(\"\\)") '(2 'font-latex-string-face t)) ))`. Still problem this doesn't word for Rt-to-Lt or corruped by the preceding `"` of the preamble. – doctorate Feb 04 '15 at 08:34
  • I see we can simplify it a bit more because there is nothing being concatenated -- my mistake (due to borrowing code from my initial days learning Emacs, even though I'm still learning). Here is the revised example removing the concat: `(font-lock-add-keywords 'latex-mode (list (list "\\(\"\\)\\(\\(.\\|\n\\)+?\\)\\(\"\\)" '(2 'font-latex-string-face t)) ))` Another forum participant will need to help you with the language direction, since I have no knowledge in that area. – lawlist Feb 04 '15 at 08:37
  • Thanks. I checked that again, it has nothing to do with Rt-to-Lt. It is because of that single `"` in `\MakeOuterQuote{"}` which I need to tweak `csquotes` package. – doctorate Feb 04 '15 at 08:43
  • I'm glad we were able to get you headed in the right direction. If you aren't able to resolve it and are in need of a regex exclusion (i.e., don't match `{"}` or something to that effect), feel free to update your post to address that specific issue -- there may be some readers who would prefer a new post, but I'm not picky (especially when there is no answer yet). – lawlist Feb 04 '15 at 08:55
  • this gave me the desired effect: `(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))))`. But still how to avoid the preceding single `"`? can I enter `"` string using unicode in the preamble? – doctorate Feb 04 '15 at 09:55
  • @lawlist, I updated the post. – doctorate Feb 04 '15 at 10:14
  • Very good -- I'll take a look again in the morning/mid-day (after the sleep-cycle) with a cup of coffee -- perhaps one of the forum experts will stop by in the meantime . . . :) – lawlist Feb 04 '15 at 10:20

0 Answers0