Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

First of all I love LaTeX/MathJax; I use it a lot, but there are couple of things I hate about it. Sometimes the code becomes so messy that I almost can't read it without looking at the output, while I feel like the code could have been much cleaner.

For example, sometimes you have in a sentence many \left's and \right's. I don't understand why LaTeX wouldn't interpret ( as \left( and ) as \right)? That would make such code a lot cleaner.

Also, things like: \int_a^b \! f(x) \, \mathrm{d}x. I would like to type something as \int{x,a,b}{f(x)}. Instead of memorizing what to type to let things look good.

Are there maybe packages that would make me able to write "cleaner" LaTeX code?

share|improve this question
About \left and \right, one thing is also to make sure you are not overusing them. Also, you are free to put spaces and indent your code as you wish. If there are things which you often use in the same form, you can also define a \newcommand which may help improving readability. There must have been a couple of related questions here; for instance tex.stackexchange.com/questions/33154/…. –  Corentin Nov 2 '13 at 3:49
@Corentin How could you overuse left and right ? Could I define ( as a new command for \left( ? –  90intuition Nov 2 '13 at 3:56
Think about an expression such as One half $(\frac12)$ of vs. One half $\left(\frac12\right)$ of. You, or some other author, might find the first more pleasant to the average size of the surrounding text and the equation itself. Another example is having $\left(0,\frac1n\right]$, if the parenthesis has an auto \left either the expression becomes (0,\frac1n\right] or the bracket will also need an auto \right, but then we will have problem with another style such as $]0,\frac1n]$ instead of $\left]0,\frac1n\right]$. –  Carlos Eugenio Thompson Pinzón Nov 2 '13 at 4:15
@CarlosEugenioThompsonPinzón but if the case were that ( works exactly as \left( there will be no problem –  leo Nov 2 '13 at 4:35
@leo: No, then $]a,b]$ would result in $\right]a,b\right]$ rather than $\left]a,b\right]$. –  Werner Nov 2 '13 at 4:36

6 Answers 6

Perhaps the pertinent question first:

Are there maybe packages that would make me able to write "cleaner" LaTeX code?

While some packages exist, this is very user-specific. For example, while you enjoy an interface \int{x,a,b}{f(x)}*, I may enjoy an interface that is slightly more verbose, as in \integral[variable=x,lowerbound=a,upperbound=b]{f(x)}, since it's not clear from \int{x,a,b} which is the lower or upper bound, or the variable. To that end, the user (you) is the best person to influence the readability of the code.

It is best to define your macros to have semantic meaning, which dramatically improves readability, but also consistency. The latter part is important, because we often change our minds. Macro-izing your interface allows for hassle-free adaptation down the road.

If code indentation makes it clean (in my opinion, yes), then get into the habit of doing it, or use latexindent.

I don't understand why LaTeX wouldn't interpret ( as \left( and ) as \right)? That would make such code a lot cleaner.

Sure you code might be "cleaner", but the output may not necessarily be more beautiful (if you consider input cleanliness to imply output beauty. As reference to this, see Spacing around \left and \right - \left and \right introduce more space than what would be considered "normal". Moreover, used in certain instances, it may even yield undesired results due to its ever-stretching nature. And finally, if it were to be considered always \left/\right, they'd have to be paired within the same group... and that doesn't bode well for equation that span multiple lines, or across alignments (say, in an array or other such environments).

Perhaps simpler here is an indication that it's best to use \left and \right sparingly, and rather use the "big"-equivalents where you have more control over the spacing and height.

Other references:

* You might use a definition as follows in your document preamble:

share|improve this answer
I'm not sure how I could make a macro such that \int{x,a,b}{f(x)} would print out as \int_a^b \! f(x) \, \mathrm{d}x –  90intuition Nov 2 '13 at 17:51
Here's a near variant: \def \scalingint #1,#2,#3,#4{\int_{#2}^{#3} \! #4 \, \mathrm{d}#1}. Use: \scalingint x,a,b,{f(x)}. TeX allows you to define arbitrary delimiters around arguments; LaTeX discourages it, but the functionality is still available. –  alexis Nov 2 '13 at 22:02
@90intuition: I've added a definition that works for \int{<var>,<lower>,<upper>}{<function>}. –  Werner Nov 3 '13 at 3:40

I assume you're aware that LaTeX/TeX is not the same as MathJaX.

It would certainly be possible to make the ( and ) characters "active" (to use TeX jargon) in math mode so that they automatically generate \left\lparen and \right\rparen, respectively. (Aside: one certainly wouldn't want to do this in text mode.) However, that would be a very poor idea in general. I can think of at least four reasons for making this claim.

  • If the material being enclosed by the parentheses isn't "large" -- e.g., $((u+v)(w+x))$ -- the \left( and \right) directives will not create larger parentheses even though there's a clear typographic case for making the outer parentheses (slightly) larger than the inner ones. All $\left(\left(u+v\right)\left(w+x\right)\right)$ succeeds in doing differently from $((u+v)(w+x))$ is to space the parentheses more widely apart (I've added the "abc" strings as "filler math" material):

    enter image description here

    To get (slightly) larger outer parentheses, something like \bigl( and \bigr) is required:

    enter image description here

  • Under many other circumstances, \left( and \right) will create parentheses that are too large from a typographical point of view. Take, say, the following expression:

    $\displaystyle \left( \sum_{i=1}^\infty (a_i+b_i) \right)^2 $

    which will produce this:

    enter image description here

    These parentheses are simply too big from a typographical/aesthetic point of view. It's much better to write

    $\displaystyle \biggl( \sum_{i=1}^\infty (a_i+b_i) \biggr)^2 $

    to create this look:

    enter image description here

    This is an example taken more or less straight from the TeXbook, by the way.

  • When typesetting inline math inside running text, typing $\left(a+b\right)^2$ will raise the exponent a bit more than if you typed $(a+b)^2$. (Try it!) If the text is set single-spaced, the extra vertical occupied by the exponent because of the use of the \left( and \right) directives may be just enough to make TeX insert more space between that line and the preceding line. This, in turn, will lead to very uneven-looking paragraphs, which is something to be avoided at (nearly) all cost -- assuming, or course, that good typography is an objective in this exercise.

  • If you're still not convinced, there are also cases of outright errors being produced if \left( ... \right) is used: Pairs of parentheses spanning (i) an explicit line break in a split equation or (ii) an explicit alignment point (&) in a structure such as align.

In short, it's not worth the trouble...

share|improve this answer
In short, it's not worth the trouble, unless you are using the nath package. –  Aditya Nov 2 '13 at 4:45
@Aditya - My understanding is that nath does not simply redefine ( and ) to act as \left\lparen and \right\rparen -- which was what I was addressing in my write-up. –  Mico Nov 2 '13 at 4:52
I know. But the nath package povides a very strong proof of concept that automatic scaling of delimiters can be implemented robustly. So, IMHO, it is worth the trouble to implement this. –  Aditya Nov 2 '13 at 4:53
Very nice. I think from a nonexpert point of view reading this, it should be made explicit that delimiters don't come in every size. This has bothered me a lot when I first started using those. Valid font size points are closer to each other when things are relatively small creating the illusion that delimiter stretches arbitrarily –  percusse Nov 2 '13 at 8:03
@90intuition - What's the basis for your assertion that in "your first 2 examples, it doesn't matter if I redefine ( and ) or not"? Once these symbols are redefined to act as \left\lparen and \right\rparen, say (note that one needs to load a package such as mathtools to get suitable definitions for \lparen and \rparen), one can't issue prefix commands such as \big or \bigg to override the autosizing operations undertaken by \left and \right. –  Mico Nov 4 '13 at 10:06

The nath package provides correct (see Mico's answer) automatic scaling of left and right parenthesis (including ability to break across lines).



  ( \sum_{i=1}^n (a_i + b_i) )^2

  f(x) = \wall ( \sum_{i = 1}^n \frac {1}{i}  \\
   (a_i + b_i)^2 ) \return



enter image description here

share|improve this answer
I really like this. I don't want to determine what looks good, I want to give latex some math, and let latex determine what is the best way to put this on paper. Thanks ! –  90intuition Nov 2 '13 at 4:59
@90intuition: Do keep in mind that nath is incompatible with amsmath (in particular, the align and gather environments). Go through the manuals of both nath and amsmath (and empheq) packages, and pick one that you like better. –  Aditya Nov 2 '13 at 5:01
in fact, it's not clear to me that these are necessarily the "correct" (i.e., "best") sizes. in the first example, the final superscript 2 is noticeably lower than the upper limit on the \sum, which isn't logical. i think an experienced math proofreader might question it. –  barbara beeton Nov 2 '13 at 10:08

One problem with making ( automatically behave as \left ( is that \left and \right must pair up; think about a half-open interval (with complicated expressions for the bounds), like [ <expr>, <expr> ); or a function explicitly defined as a set of cases, with a single brace for grouping (which can be expressed as \left \{ ... \right . ).

Mathematical notation is sufficiently open-ended that even if all these ways of pairing could be automatically detected, doing so would restrict authors from defining and using their own notation. So there needs to be a distinction between plain delimiters and automatically-sized (and paired) delimiters.

However, this doesn't answer the question: Why isn't it the default for delimiters to size automatically? E.g., why wasn't TeX designed so that ( behaves as the current \left (, with another operator giving the current default (e.g., \simple ()? That's harder to answer. But it is clear from reading the TeXbook that this kind of usability issues were thoroughly considered when Knuth was creating TeX, so I would expect that needing to explicitly request automatic sizing is ultimately more convenient than the other way around (or was: typographical customs may have changed after so many decades of desktop publishing).

share|improve this answer

On the risk of downvoting...

This post does not give a reason but introduces a workaround I use frequently.

I made a package that works with such active brackets like:


One then can use \brak{expression} to place brackets around the expression, \fun{f}{x} to specify function application and \funm{cos}{x} when the function should be placed in normal typography.

The advantage of using such commands is that the code becomes more semantically correct, for instance LaTeX can check that you didn't forget to close a bracket, or that the name of the function is placed before the brackets.

share|improve this answer
Seen from a programming perspective, I'd say your code does the opposite of what the OP was asking for: Rather than make all parentheses stretchable, they're being stretched in the context of explicitly defined macros. A separate comment: I can't help but wonder if you really want the extra whitespace between f (or cos) and (x). If you don't (and want to keep using your macros), you may want to consider loading the mleftright package and (re)defining your first macro as \newcommand{\brak}[1]{\ensuremath{\mleft(#1\mright)}}. –  Mico Nov 6 '13 at 6:11
I would recommend you to drop \ensuremath. Because it allows you to do for instance \fun{\cos}{x}=\brak{\frac{1}{2}}, which gives completely wrong spacing. –  tohecz Nov 8 '13 at 7:13

I've copy and pasted some code from this website, and put it together. This does seem to do what you are asking for, and also it seems to be able to avoid problems that mico warns for.

    \expandafter\@tempb\meaning\@tempa \relax
  \ht\Mathstrutbox@\ht\z@ \dp\Mathstrutbox@\dp\z@}
  \catcode`(\active \xdef({\left\string(}
  \catcode`)\active \xdef){\right\string)}
\mathcode`(="8000 \mathcode`)="8000
&f(x) = (\frac{(3-\frac{1}{x})^2}{(\frac4x-1)x}-1)x^2 \\
&\cos(\theta) \\
&\biggl( \sum_{i=1}^\infty (a_i+b_i) \biggr)^2  \\
&\bigl( (u+v)(w+x)\bigr)

enter image description here

share|improve this answer
Very nice: The code makes ( and ) act like \left( and \right) by default, while letting the user override this behavior via explicit sizing instructions (such as \big and \bigg). –  Mico Nov 9 '13 at 7:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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