Subject: Re: bug#24667: AUCTeX citation color

From: Arash Esbati <[email-masked]>
Date: Sat, 15 Oct 2016 14:53:34 +0200

Hi Mosè,

Mosè Giordano <[email-masked]> writes:

> 2016-10-11 12:49 GMT+02:00 Travis Knepp <[email-masked]>:
>> Coloring of citations (when using \citep, \citet, etc. as opposed to just
>> using \cite) is not properly working.  Details from the stackexchange post
>> (http://emacs.stackexchange.com/questions/27591/auctex-citation-color) are
>> here:
>>
>> When inserting a reference in a LaTeX document (using Auctex) I normally
>> type:
>>
>> \cite{joebob_2010}
>>
>> where "\cite" shows up in blue font, while "joebob_2010" shows up
>> reddish/purple. I like this coloring. However, when I use
>>
>> \citep{joebob_2010}
>>
>> all text shows up white.

[...]

> If you want instead to write some Elisp, you can put this
>
> (font-latex-add-keywords
>  '(("citep" "*[[{"))
>  'reference)
>
> inside the hook.
>
> Out of curiosity, do you use natbib or jurabib packages?

First of all, sorry for my late response.  Travis uses biblatex.  And
while we're at it: Travis, how do insert this macro?  Do you type
\citep{<key>} or do you hit `C-c m RET citep RET' or use Reftex?  I'm
asking since you're asking for the fontification; I think the completion
support (see below) is much more needed.

> If you have parsing enabled, you should automatically get
> fontification of citep macro.

That was also was my first answer, but then I realized that biblatex.el
doesn't have support for natbib compat macros (cf. biblatex manual,
§3.8.9 natbib Compatibility Commands).  I asked Travis to file this bug
report (thanks btw), I think the appended patch fixes the issue.  It is
mostly taken from natbib.el, function `LaTeX-biblatex-natbib-note-args'
is a modified version of `natbib-note-args' and I think that `(when
TeX-arg-cite-note-p' is not really needed, but I didn't touch it since
it doesn't hurt either.

--8<---------------cut here---------------start------------->8---
diff --git a/style/biblatex.el b/style/biblatex.el
index 06cd57a..f0e235d 100644
--- a/style/biblatex.el
+++ b/style/biblatex.el
@@ -216,6 +216,32 @@ for citation keys."
 	(TeX-argument-insert (mapconcat 'identity items ",") optional))
       (setq noinsert t))))
 
+(defun LaTeX-biblatex-natbib-note-args (optional)
+  "Prompt for two note arguments of a natbib compat citation command."
+  (when TeX-arg-cite-note-p
+      (let ((pre (TeX-read-string
+		  (TeX-argument-prompt optional nil "Prenote")))
+	    (post (TeX-read-string
+		   (TeX-argument-prompt optional nil "Postnote"))))
+	(cond
+	 (;; Both optional args are given
+	  (and pre (not (string= pre ""))
+	       post (not (string= post "")))
+	  (insert LaTeX-optop pre LaTeX-optcl
+		  LaTeX-optop post LaTeX-optcl))
+	 (;; pre is given, post is empty: Make sure that we insert an
+	  ;; extra pair of `[]', otherwise pre becomes post
+	  (and pre (not (string= pre ""))
+	       (string= post ""))
+	  (insert LaTeX-optop pre LaTeX-optcl
+		  LaTeX-optop LaTeX-optcl))
+	 (;; pre is empty, post is given
+	  (and (string= pre "")
+	       post (not (string= post "")))
+	  (insert LaTeX-optop post LaTeX-optcl))
+	 (;; both empty
+	  t (ignore))))))
+
 (TeX-add-style-hook
  "biblatex"
  (lambda ()
@@ -456,6 +482,53 @@ for citation keys."
     '("DefineHyphenationExceptions"
       (TeX-arg-eval completing-read "Language: " LaTeX-biblatex-language-list) t)
     "NewBibliographyString")
+
+   ;; § 3.8.9 natbib Compatibility Commands
+   (when (or (LaTeX-provided-package-options-member "biblatex" "natbib")
+	     (LaTeX-provided-package-options-member "biblatex" "natbib=true"))
+     (let ((cmds '(("citet" . 1) ("citet*" . 1)
+		   ("Citet" . 1) ("Citet*" . 1)
+		   ("citep" . 2) ("citep*" . 2)
+		   ("Citep" . 2) ("Citep*" . 2)
+		   ("citealt" . 1) ("citealt*" . 1)
+		   ("Citealt" . 1) ("Citealt*" . 1)
+		   ("citealp" . 2) ("citealp*" . 2)
+		   ("Citealp" . 2) ("Citealp*" . 2))))
+       ;; Taken from natbib.el:
+       (apply
+	#'TeX-add-symbols
+	(mapcar
+	 (lambda (cmd)
+	   (cond
+	    ((= (cdr cmd) 1)
+	     ;; Just one optional argument, the post note
+	     (list
+	      (car cmd)
+	      '(TeX-arg-conditional TeX-arg-cite-note-p (["Postnote"]) nil)
+	      'TeX-arg-cite))
+	    ((= (cdr cmd) 2)
+	     ;; Pre and post notes
+	     (list
+	      (car cmd)
+	      '(TeX-arg-conditional TeX-arg-cite-note-p
+				    ([LaTeX-biblatex-natbib-note-args])
+				  nil)
+	      'TeX-arg-cite))))
+	 cmds))
+
+     ;; Fontification for compat macros do not go into `font-latex.el':
+     (when (and (featurep 'font-latex)
+		(eq TeX-install-font-lock 'font-latex-setup))
+       (font-latex-add-keywords '(("citet"        "*[{")
+				  ("Citet"        "*[{")
+				  ("citep"        "*[[{")
+				  ("Citep"        "*[[{")
+				  ("citealt"      "*[{")
+				  ("Citealt"      "*[{")
+				  ("citealp"      "*[[{")
+				  ("Citealp"      "*[[{"))
+				'biblatex))))
+
    (LaTeX-add-environments
     ;;; Bibliography commands
     ;; Bibliography Sections
--8<---------------cut here---------------end--------------->8---

Here also a small file I tested with:

--8<---------------cut here---------------start------------->8---
\documentclass{article}

\begin{filecontents}{biblatex-test.bib}
@book{lamp:94,
  author    = {Leslie Lamport},
  title     = {LaTeX - {A} Document Preparation System: User's Guide
               and Reference Manual, Second Edition},
  publisher = {Pearson / Prentice Hall},
  year      = {1994},
  isbn      = {978-0-201-52983-8},
  timestamp = {Fri, 08 Apr 2011 18:21:00 +0200},
}

@book{mitt:97,
  author    = {Michel Goossens and
               Sebastian Rahtz and
               Frank Mittelbach},
  title     = {The LaTeX Graphics Companion - Illustrating documents
               with TeX and PostScript},
  series    = {Addison-Wesley series on tools and techniques
               for computer typesetting},
  publisher = {Addison-Wesley},
  year      = {1997},
  isbn      = {978-0-201-85469-5},
}
\end{filecontents}

\usepackage[natbib,backend=bibtex,style=authoryear]{biblatex}

\bibliography{biblatex-test}

\begin{document}

\noindent \textbf{Textual}:\\
\citet{lamp:94}, \\
\citet[which see]{lamp:94},\\
\citet*{mitt:97}, \\
\citealt{mitt:97,lamp:94}, \\
\Citealt{mitt:97,lamp:94}

\noindent \textbf{Parenthesis}:\\
\citep{mitt:97}, \\
\citep[see][Page~5]{mitt:97}, \\
\citep*{mitt:97}, \\
\citealp{mitt:97}, \\
\Citealp{mitt:97}, \\
\citealp*{mitt:97}, \\
\Citealp*{mitt:97}

\end{document}
--8<---------------cut here---------------end--------------->8---

Best, Arash