10

When writing LaTeX using latex-mode, I often want to write "half-open intervals" in my text. For instance, [0,1), which refers to the set of all numbers x with 0 <= x < 1.

Unfortunately, these "unbalanced delimiters" seem to confuse the indentation parser, causing following text to be indented inappropriately, usually drifting to the right.

Here's an example document. The text below is the result after doing C-x h M-x indent-region. The correct behavior would be to have all of both paragraphs indented by 0 spaces.

\documentclass{article}
\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam volutpat
at dui et mattis. Here is a half open interval $[0,1)$.  Proin
  fringilla lectus nec mi tincidunt aliquet. Vestibulum fermentum eu
  neque id egestas. Here is another $[1,2)$.  Nullam et lectus
    convallis, placerat neque non, vehicula nisi.  Fusce imperdiet
    dignissim ante, eget fringilla nisi.

    Integer fermentum nunc eget purus molestie commodo. Integer
    tristique tincidunt odio, vel rutrum diam commodo quis. Fusce a
    aliquet quam. Cras suscipit est et nisl sollicitudin
    iaculis. Aliquam erat volutpat. Phasellus sapien arcu, aliquet ut
    hendrerit in, lobortis nec ante.

\end{document}

Currently I am working around this with an ugly hack: I define a LaTeX macro that expands to nothing, and write the "matching" delimiters inside an invocation of this macro. This gets the indenter back in sync.

\documentclass{article}
\newcommand{\ugh}[1]{}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam volutpat
at dui et mattis. Here is a half open interval $[0,1)$.  Proin
  fringilla lectus nec mi tincidunt aliquet. Vestibulum fermentum eu
  neque id egestas. \ugh{(]} Now on the next line we are back in sync.
Nullam et lectus convallis, placerat neque non, vehicula nisi.  Fusce
imperdiet dignissim ante, eget fringilla nisi.
\end{document}

But this is really unpleasant. Is there a better solution?

I'm using GNU Emacs 24.4.1.

giordano
  • 3,245
  • 13
  • 19
Nate Eldredge
  • 201
  • 1
  • 4
  • 3
    Not answering your question, but have you considered giving AUCTeX a try? It doesn't suffer from this little bug, for one. – giordano Aug 22 '15 at 22:42
  • @giordano: I've looked at it. Its default behavior is a lot heavier than what I want. I prefer to edit LaTeX like code, and I don't really want WYSIWYG-style in-editor rendering. Maybe it's possible to turn all of that off and have a text editor experience, but it still makes AUCTeX seem like overkill. – Nate Eldredge Aug 22 '15 at 23:06
  • 8
    Which WYSIWYG-like behavior are you referring to? Only such features are preview and fold-mode and none of them is active by default (you don't even need to load corresponding packages to use AUCTeX). The main difference between AUCTeX and vanilla *TeX modes is a far superior autocompletion mechanism when inserting macros and environments. If you value autocompletion AUCTeX is the way to go. In addition, in AUCTeX you always have to edit *TeX code, there are no other possibilities, I don't know why you had this wrong impression. – giordano Aug 23 '15 at 07:48

3 Answers3

8

Not an Emacs solution, but my LaTeX code writes such intervals as \halfopen{0}{1}, where

\newcommand\halfopen[2]{\ensuremath{[#1,#2)}}

This convention pushes the balancing issue to a place where it's unlikely to cause problems. And it's searchable :-)

giordano
  • 3,245
  • 13
  • 19
Norman Ramsey
  • 1,113
  • 6
  • 13
2

The indentation function latex-indent of latex-mode sets the syntax-table to tex-latex-indent-syntax-table and calls latex-find-indent which in turn calls latex-backward-sexp-1.

The syntax of ?\( and ?\) is set to "." which stands for "punctuation". So it is no longer a delimiter.

Therefore, expressions like [0,1) result into a scan-error in latex-backward-sexp-1 which leads to the unexpected behavior.

You can avoid this by adding the following to your init file:

(eval-after-load "tex-mode"
    '(progn (modify-syntax-entry ?\( "()" tex-latex-indent-syntax-table)
             (modify-syntax-entry ?\) ")(" tex-latex-indent-syntax-table)))

But, I do not know the negative consequences of this setting since I use auctex. So, expect the worst;-).

YoungFrog
  • 3,496
  • 15
  • 27
Tobias
  • 32,569
  • 1
  • 34
  • 75
0

\left[\left. 0,1\right)\right.

Don't miss the periods after left and right

  • Unfortunately this also triggers the Emacs bug. I think it just looks for `[` and `)` characters without noticing whether they are part of `\left[` etc. – Nate Eldredge Aug 08 '22 at 23:38