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

From: Omar <[email-masked]>
Date: Thu, 05 Jul 2018 11:59:53 -0500

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.

Emacs  : GNU Emacs 25.3.2 (i686-pc-linux-gnu, GTK+ Version 3.18.9)
 of 2017-09-12
Package: 12.1.1

current state:
==============
(setq
 AUCTeX-date "2018-03-20"
 window-system 'x
 LaTeX-version "2e"
 TeX-style-path '("~/.emacs.d/auctex"
                  "/home/omar/.emacs.d/elpa/auctex-12.1.1/style" "/home/omar/.emacs.d/auctex/auto" "/home/omar/.emacs.d/auctex/style" "auto" "style")
 TeX-auto-save nil
 TeX-parse-self nil
 TeX-master t
 TeX-command-list '(("TeX"
                     "%(PDF)%(tex) %(file-line-error) %(extraopts) %`%S%(PDFout)%(mode)%' %t"
                     TeX-run-TeX nil
                     (plain-tex-mode ams-tex-mode
                      texinfo-mode)
                     :help "Run plain TeX")
                    ("LaTeX" "%`%l%(mode)%' %t"
                     TeX-run-TeX nil
                     (latex-mode doctex-mode) :help
                     "Run LaTeX")
                    ("Makeinfo"
                     "makeinfo %(extraopts) %t"
                     TeX-run-compile nil
                     (texinfo-mode) :help
                     "Run Makeinfo with Info output")
                    ("Makeinfo HTML"
                     "makeinfo %(extraopts) --html %t"
                     TeX-run-compile nil
                     (texinfo-mode) :help
                     "Run Makeinfo with HTML output")
                    ("AmSTeX"
                     "amstex %(PDFout) %(extraopts) %`%S%(mode)%' %t"
                     TeX-run-TeX nil (ams-tex-mode)
                     :help "Run AMSTeX")
                    ("ConTeXt"
                     "%(cntxcom) --once --texutil %(extraopts) %(execopts)%t"
                     TeX-run-TeX nil (context-mode)
                     :help "Run ConTeXt once")
                    ("ConTeXt Full"
                     "%(cntxcom) %(extraopts) %(execopts)%t"
                     TeX-run-TeX nil (context-mode)
                     :help
                     "Run ConTeXt until completion")
                    ("BibTeX" "bibtex %s"
                     TeX-run-BibTeX nil t :help
                     "Run BibTeX")
                    ("Biber" "biber %s" TeX-run-Biber
                     nil t :help "Run Biber")
                    ("View" "%V"
                     TeX-run-discard-or-function t t
                     :help "Run Viewer")
                    ("Print" "%p" TeX-run-command t t
                     :help "Print the file")
                    ("Queue" "%q" TeX-run-background
                     nil t :help
                     "View the printer queue" :visible
                     TeX-queue-command)
                    ("File" "%(o?)dvips %d -o %f "
                     TeX-run-dvips t t :help
                     "Generate PostScript file")
                    ("Dvips" "%(o?)dvips %d -o %f "
                     TeX-run-dvips nil t :help
                     "Convert DVI file to PostScript")
                    ("Dvipdfmx" "dvipdfmx %d"
                     TeX-run-dvipdfmx nil t :help
                     "Convert DVI file to PDF with dvipdfmx")
                    ("Ps2pdf" "ps2pdf %f"
                     TeX-run-ps2pdf nil t :help
                     "Convert PostScript file to PDF")
                    ("Glossaries" "makeglossaries %s"
                     TeX-run-command nil t :help
                     "Run makeglossaries to create glossary file")
                    ("Index" "makeindex %s"
                     TeX-run-index nil t :help
                     "Run makeindex to create index file")
                    ("upMendex" "upmendex %s"
                     TeX-run-index t t :help
                     "Run upmendex to create index file")
                    ("Xindy" "texindy %s"
                     TeX-run-command nil t :help
                     "Run xindy to create index file")
                    ("Check" "lacheck %s"
                     TeX-run-compile nil (latex-mode)
                     :help
                     "Check LaTeX file for correctness")
                    ("ChkTeX" "chktex -v6 %s"
                     TeX-run-compile nil (latex-mode)
                     :help
                     "Check LaTeX file for common mistakes")
                    ("Spell"
                     "(TeX-ispell-document \"\")"
                     TeX-run-function nil t :help
                     "Spell-check the document")
                    ("Clean" "TeX-clean"
                     TeX-run-function nil t :help
                     "Delete generated intermediate files")
                    ("Clean All" "(TeX-clean t)"
                     TeX-run-function nil t :help
                     "Delete generated intermediate and output files")
                    ("Other" "" TeX-run-command t t
                     :help "Run an arbitrary command")
                    )
 )