Subject: Indenting in tabulars and \&
From: Arash Esbati <[email-masked]>
To: auctex-bugs <[email-masked]>
Date: Tue, 07 Mar 2017 14:24:15 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi all,

please consider the following 2 examples and how they are filled: First
example works as expected, second one not because of \&:

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

\usepackage{longtable}

\begin{document}

Hit `C-c C-q C-e' inside env:
\begin{tabular}{ll}
  These lines will & appear \\
  at the end
  & of the table     
\end{tabular}
results in:
\begin{tabular}{ll}
  These lines will & appear \\
  at the end
                   & of the table
\end{tabular}

Hit `C-c C-q C-e' inside env:
\begin{tabular}{ll}
  These lines will appear \& 
  at the end \\
  of the &
  table
\end{tabular}
results in:
\begin{tabular}{ll}
  These lines will appear \& 
                             at the end \\
  of the &
           table
\end{tabular}

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

AUCTeX doesn't see the control symbol \&, it just takes the ampersand as
a column separator.  This issue is caused in the last part of the
function `LaTeX-indent-tabular':

--8<---------------cut here---------------start------------->8---
    (cond (...
	  (t
	   (+ 2
              (let ((any-col (save-excursion
                               (when (re-search-backward "\\\\\\\\\\|&" beg-pos t)
                                 (current-column)))))
                (if (and any-col (string= "&" (match-string 0)))
                    any-col
		  beg-col)))))))
--8<---------------cut here---------------end--------------->8---

I'm not aware of any way to fix this *only* by tweaking the regexp here:

    (re-search-backward "\\\\\\\\\\|&" beg-pos t)

Any idea?

Otherwise, I suggest the following change in `LaTeX-indent-tabular':

--8<---------------cut here---------------start------------->8---
    (cond (...
	  (t
	   (+ 2
              (let ((any-col (save-excursion
                               (when (re-search-backward "\\\\\\\\\\|[^\\]&" beg-pos t)
                                 (current-column)))))
                (if (and any-col (string= "&" (substring (match-string-no-properties 0) -1)))
                    (1+ any-col)
		  beg-col)))))))
--8<---------------cut here---------------end--------------->8---

Comments welcome.

Best, Arash