17

Q: how can I get "proper" indentation of LaTeX itemize environments in auctex?

Here's where I'd like to be with an item in an itemize environment:

  • \item line is indented two spaces relative to the beginning of the environment
  • continuation lines in the item are indented an additional two spaces relative to the \item line

This is what I would like/expect to see:

\begin{itemize}
  \item Here's a really long item in a LaTeX itemize environment;
    note how the *initial* item line is indented two spaces, and the
    continuation lines are indented another two spaces.
\end{itemize}

One can adjust the initial indentation of the item with the LaTeX-item-indent variable, which defaults to -2. With this default, I get the undesirable behavior of the \item not being indented, but I do get the desired behavior of the continuation lines being offset by an additional two spaces:

\begin{itemize}
\item Here's a really long item in a LaTeX itemize environment;
  note how the *initial* item line is *NOT* indented two spaces,
  but the continuation lines are indented two spaces.
\end{itemize}

Setting LaTeX-item-indent to 0 gets me the desired indentation on the \item line (two spaces in), but does not get me the second half of the desired behavior of the continuation lines being offset by an additional two spaces:

\begin{itemize}
  \item Here's a really long item in a LaTeX itemize environment;
  note how the *initial* item line is indented two spaces, but the
  continuation lines are *NOT* indented an additional two spaces.
\end{itemize}

So: how does one get both desired behaviors:

  • initial indent of the \item line two spaces, and
  • continuation lines an additional two spaces indented?

(Note Related SO thread.)

Dan
  • 32,584
  • 6
  • 98
  • 168
  • 2
    I've been messing around with this exact issue for a couple of hours; your first method works if you also set `LaTeX-indent-level` to 4. Items will be indented to 4 - 2 = 2 and continuation lines will be indented to 4 = 2 + 2. However, this does mean that every other environment in the file will be indented to 4 (and not 2), which may or may not be desirable. I'd rather they be indented at 2 themselves, which is where I'm stuck. – sykora Nov 04 '14 at 19:54
  • Did you try customizing `LaTeX-indent-environment-list` and adding a custom function for indentation? The function `LaTeX-indent-tabular` might provide a reasonable starting point (or at least a reasonable example of customized indentation within an environment). I just stumbled across this variable/function, so I haven't had a chance to look into it myself. – zroth Nov 14 '18 at 14:32

1 Answers1

15

@sykora's comment about (setq LaTeX-item-indent -2 LaTeX-indent-level 4) is almost there, but it does mean we spill over to every other environment as well. So, for example, we would also have:

\begin{abstract}
    This indents to the 4th column, which is way too far!
\end{abstract}

The following function builds off an old (and seemingly broken?) code snippet from Tassilo Horn. It gets the indentation correct, including for nested environments. It works for itemize, enumerate, and description environments, to boot:

(defun LaTeX-indent-item ()
  "Provide proper indentation for LaTeX \"itemize\",\"enumerate\", and
\"description\" environments.

  \"\\item\" is indented `LaTeX-indent-level' spaces relative to
  the the beginning of the environment.

  Continuation lines are indented either twice
  `LaTeX-indent-level', or `LaTeX-indent-level-item-continuation'
  if the latter is bound."
  (save-match-data
    (let* ((offset LaTeX-indent-level)
           (contin (or (and (boundp 'LaTeX-indent-level-item-continuation)
                            LaTeX-indent-level-item-continuation)
                       (* 2 LaTeX-indent-level)))
           (re-beg "\\\\begin{")
           (re-end "\\\\end{")
           (re-env "\\(itemize\\|\\enumerate\\|description\\)")
           (indent (save-excursion
                     (when (looking-at (concat re-beg re-env "}"))
                       (end-of-line))
                     (LaTeX-find-matching-begin)
                     (current-column))))
      (cond ((looking-at (concat re-beg re-env "}"))
             (or (save-excursion
                   (beginning-of-line)
                   (ignore-errors
                     (LaTeX-find-matching-begin)
                     (+ (current-column)
                        (if (looking-at (concat re-beg re-env "}"))
                            contin
                          offset))))
                 indent))
             ((looking-at (concat re-end re-env "}"))
              indent)
            ((looking-at "\\\\item")
             (+ offset indent))
            (t
             (+ contin indent))))))

(defcustom LaTeX-indent-level-item-continuation 4
  "*Indentation of continuation lines for items in itemize-like
environments."
  :group 'LaTeX-indentation
  :type 'integer)

(eval-after-load "latex"
  '(setq LaTeX-indent-environment-list
         (nconc '(("itemize" LaTeX-indent-item)
                  ("enumerate" LaTeX-indent-item)
                  ("description" LaTeX-indent-item))
                LaTeX-indent-environment-list)))

I can't help but feel that there is a very simple setting I'm missing and this is the Rube Goldberg version. Still, it works, and it scratches an itch I've had for years.

EDIT: in response to @sykora's comment, I have revised the function to take out the hard coding. \items are now indented LaTeX-indent-level spaces. Continuation lines can take the value of a new variable, LaTeX-indent-level-item-continuation, or, if you don't want to bind the latter, twice the value of LaTeX-indent-level.

As it happens, binding and setting LaTeX-indent-level-item-continuation to 8 gives aesthetically-pleasing results. I might even switch to it:

\begin{itemize}
  \item Example with LaTeX-indent-level-item-continuation set to 8.
  \item Here's a really long item that will spill over onto the
        continuation line; text lines up pretty nicely this way!
        \begin{itemize} 
          \item And here's a sub-item, with the environment
                indented to the relevant continuation line.
        \end{itemize}
\end{itemize}
Dan
  • 32,584
  • 6
  • 98
  • 168
  • I spent a little time looking at it this morning, but needed to focus on something else. I think line 3060 of `latex.el` -- i.e., `(+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))` -- is contributing to the level of indent. – lawlist Nov 04 '14 at 21:35
  • I just took it for a test ride, and it seems to perform pretty well -- thanks! If possible, you could replace the hard-coded 2s with either `LaTeX-indent-level` or a new variable -- `LateX-item-continuation-indent`? – sykora Nov 05 '14 at 03:25
  • @sykora: good suggestion! Incorporated. – Dan Nov 05 '14 at 10:48
  • As a frequent TeXer, this is *fantastic*. It's really always bugged me! Thanks :) – Sean Allred Nov 19 '14 at 15:28
  • What wrapping mode are you using here? I get every line flushed left. See http://i61.tinypic.com/eq8n7b.jpg – NVaughan Nov 19 '14 at 22:21
  • @NVaughan: try `newline-and-indent` rather than `TeX-newline`. – Dan Nov 19 '14 at 23:08
  • @Dan: Doesn't work. I am using `visual-line-mode` and I just get a new line flushed left. Are you using `adaptative-wrap-mode`? – NVaughan Nov 20 '14 at 00:51
  • @NVaughan: I'm not using `adaptive-wrap-mode`, but I think I might be seeing the problem that you're having. Try `(setq TeX-newline-function #'LaTeX-newline)` (that, at any rate, cleared up the problem for me). – Dan Nov 20 '14 at 04:45
  • @Dan: No, I don't know what's going on, but it doesn't work. The only way I can obtain that result is by using `adaptative-wrap` (which I don't really like as it messes up the indentation of LaTeX environments). – NVaughan Nov 20 '14 at 15:54
  • @Dan Thank you for this. But is it just me, or does this break `fill-paragraph` in `\item`s? – Jason Morgan Jan 13 '16 at 19:23
  • @JasonMorgan: correct, `fill-paragraph` does not work here. Long story, but the innards of the fill function for latex are pretty ugly, and I haven't had the time to figure out how to fix it. – Dan Jan 14 '16 at 03:15
  • @Dan Thank you. That is good to know. Now I won't spend hours trying to figure out what's wrong with my `.emacs` file. – Jason Morgan Jan 15 '16 at 20:14
  • Rube Goldberg link seems to be not related to emacs – toogley Aug 09 '16 at 21:04