18

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.)

2
  • 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.
    – sykloid
    Commented Nov 4, 2014 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
    Commented Nov 14, 2018 at 14:32

1 Answer 1

16

@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}
13
  • 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
    Commented Nov 4, 2014 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?
    – sykloid
    Commented Nov 5, 2014 at 3:25
  • @sykora: good suggestion! Incorporated.
    – Dan
    Commented Nov 5, 2014 at 10:48
  • As a frequent TeXer, this is fantastic. It's really always bugged me! Thanks :) Commented Nov 19, 2014 at 15:28
  • What wrapping mode are you using here? I get every line flushed left. See i61.tinypic.com/eq8n7b.jpg
    – NVaughan
    Commented Nov 19, 2014 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.