--- latex.el~ 2005-03-24 20:17:28.000000000 +0100 +++ latex.el 2005-04-02 11:58:14.000000000 +0200 @@ -104,7 +104,7 @@ (defun LaTeX-section (arg) "Insert a template for a LaTeX section. -Determinate the type of section to be inserted, by the argument ARG. +Determine the type of section to be inserted, by the argument ARG. If ARG is nil or missing, use the current level. If ARG is a list (selected by \\[universal-argument]), go downward one level. @@ -140,8 +140,6 @@ (region-end)) "")) (done-mark (make-marker))) - (when (zerop (length title)) - (LaTeX-newline)) (run-hooks 'LaTeX-section-hook) (LaTeX-newline) (if (marker-position done-mark) @@ -359,7 +357,7 @@ LaTeX-section-section: Insert LaTeX section command according to `name', `title', and `toc'. If `toc' is nil, no toc entry is -enserted. If `toc' or `title' are empty strings, `done-mark' will be +inserted. If `toc' or `title' are empty strings, `done-mark' will be placed at the point they should be inserted. LaTeX-section-label: Insert a label after the section command. @@ -450,25 +448,35 @@ (defun LaTeX-section-section () "Hook to insert LaTeX section command into the file. -Insert this hook into `LaTeX-section-hook' after those hooks which sets -the `name', `title', and `toc' variables, but before those hooks which -assumes the section already is inserted." - (insert TeX-esc name) - (cond ((null toc)) - ((zerop (length toc)) - (insert LaTeX-optop) - (set-marker done-mark (point)) - (insert LaTeX-optcl)) - (t - (insert LaTeX-optop toc LaTeX-optcl))) - (insert TeX-grop) - (if (zerop (length title)) - (set-marker done-mark (point))) - (insert title TeX-grcl) - (LaTeX-newline) - ;; If RefTeX is available, tell it that we've just made a new section - (and (fboundp 'reftex-notice-new-section) - (reftex-notice-new-section))) +Insert this hook into `LaTeX-section-hook' after those hooks that set +the `name', `title', and `toc' variables, but before those hooks that +assume that the section is already inserted." + ;; insert a new line if the current line and the previous line are + ;; not empty (except for whitespace), with one exception: do not + ;; insert a new line if the previous (or current, sigh) line starts + ;; an environment (i.e., starts with `[optional whitespace]\begin') + (unless (save-excursion + (re-search-backward + (concat "^\\s-*\n\\s-*\\|^\\s-*" (regexp-quote TeX-esc) + "begin") + (line-beginning-position 0) t)) + (LaTeX-newline)) + (insert TeX-esc name) + (cond ((null toc)) + ((zerop (length toc)) + (insert LaTeX-optop) + (set-marker done-mark (point)) + (insert LaTeX-optcl)) + (t + (insert LaTeX-optop toc LaTeX-optcl))) + (insert TeX-grop) + (if (zerop (length title)) + (set-marker done-mark (point))) + (insert title TeX-grcl) + (LaTeX-newline) + ;; If RefTeX is available, tell it that we've just made a new section + (and (fboundp 'reftex-notice-new-section) + (reftex-notice-new-section))) (defun LaTeX-section-label () "Hook to insert a label after the sectioning command.