Subject: bug#32062: 12.1.1; LaTeX-outline-name can't handle long lines or braces in titles

From: Arash Esbati <[email-masked]>
Date: Wed, 06 Mar 2024 09:30:08 +0100

Omar <[email-masked]> writes:

> Hello,
>
> I noticed recently that imenu lists the names of section titles
> incorrectly under AucTeX, while it gives the titles I'd expect under
> Emacs' builtin tex-mode.el.
>
> I narrowed down the problem to LaTeX-outline-name which gives the
> wrong answer in two situations:
>
> 1. If the section (or subsection, etc.) title is very long, namely, if
> it ends in a column greater than fill-column plus 10, then
> LaTeX-outline-name reports the title as the first 20 characters in the
> line. This is particularly ugly since it means the title will start
> with "\section{" or somehting like that.
>
> 2. If the title contains a right brace, LaTeX-outline-name reports the
> title as ending at that brace.
>
> This small document illustrates both problems:
>
> \documentclass{article}
> \begin{document}
> \section{A title so utterly long that it exceeds the fill column by more than ten characters}
> \section{Braces \emph{screw} this one up}
> \end{document}
>
> If fill-column is set to a reasonable value, the first section is too
> long and gets listed in imenu as "\section{A title so ". The second
> one gets truncated at the right brace: "\section{Braces \emph{screw".
>
> The builtin tex-mode uses forward-sexp to allow balanced braces inside
> the section titles. In my personal configuration I've replace
> LaTeX-outline-name with:
>
> (defun LaTeX-outline-name ()
>   "Guess a name for the current header line."
>   (save-excursion
>     (search-forward "{" nil t)
>     (let ((beg (point)))
>       (forward-char -1)
>       (condition-case nil
>           (progn
>             (forward-sexp 1)
>             (forward-char -1))
>           (error (forward-sentence 1)))
>       (buffer-substring beg (point)))))
>
> This attempts to skip over the title using forward-sexp and in case of
> error uses forward-sentence instead. It seems to work on the files I
> noticed had a problem before, but I haven't done extensive testing.

(I'm going through old bug reports that unfortunately weren't resolved
at the time.)

Hi Omar,

first, my sincere apologies that you didn't hear from us in a timely
manner.  I'm not an Imenu user, but from a brief test on the file below,
I believe that `LaTeX-outline-name' is currently broken, actually.

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

\begin{document}

\section{A title so utterly long that it exceeds
  the fill column by more than ten characters}
\section{A title so utterly}
\section{Braces \emph{screw} this one up}
\section{A title so utterly long that it exceeds the fill column by
  more than ten characters}
\section{A Title so utterly long that it exceeds the fill column by more than ten characters}

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

Based on your code, I suggest to change the function to this:

--8<---------------cut here---------------start------------->8---
(defun LaTeX-outline-name ()
  "Guess a name for the current header line."
  (save-excursion
    (search-forward "{" nil t)
    (let ((beg (point)))
      (backward-char)
      (condition-case nil
          (with-syntax-table (TeX-search-syntax-table ?\{ ?\})
            (forward-sexp)
            (backward-char))
        (error (forward-sentence)))
      (replace-regexp-in-string "[\n\r][ ]+" " "
                                (buffer-substring beg (point))))))
--8<---------------cut here---------------end--------------->8---

What do you and others think?  Again, sorry for being late.

Best, Arash